Android通用圆角布局,可以解决Android P版本xfermode方案裁剪黑边问题和xfermode在列表view中使用滑动时EGL内存泄露问题
allprojects {repositories {...maven { url '' }}}
dependencies {implementation 'com.github.minminaya:GenaralRoundLayout:1.0.0'}
<com.minminaya.widget.GeneralRoundFrameLayoutandroid:layout_width="200dp"android:layout_height="200dp"android:layout_gravity="center"app:corner_radius="30dp"><Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorAccent" /></com.minminaya.widget.GeneralRoundFrameLayout>
GeneralRoundLayout设计初期是为了方便各种布局的扩展,因此可以使任何一个view支持圆角特性,你只需要重写几个方法
interface IRoundView {fun setCornerRadius(cornerRadius: Float)fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)
}
在你的attrs的文件中,定义declare-styleable属性(为了可以在xml文件中输入的时候自动提示)
<declare-styleable name="GeneralRoundImageView"><attr name="corner_radius" /></declare-styleable>
public class GeneralRoundImageView extends AppCompatImageView implements IRoundView {public GeneralRoundImageView(Context context) {this(context, null);}public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic void setCornerRadius(float cornerRadius) {}@Overridepublic void onLayout(boolean changed, int left, int top, int right, int bottom) {Layout(changed, left, top, right, bottom);}}
public class GeneralRoundImageView extends AppCompatImageView implements IRoundView {private GeneralRoundViewImpl generalRoundViewImpl;public GeneralRoundImageView(Context context) {this(context, null);}public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init(this, context, attrs);}public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(this, context, attrs);}@Overridepublic void setCornerRadius(float cornerRadius) {generalRoundViewImpl.setCornerRadius(cornerRadius);}@Overridepublic void onLayout(boolean changed, int left, int top, int right, int bottom) {Layout(changed, left, top, right, bottom);Layout(changed, left, top, right, bottom);}private void init(GeneralRoundImageView view, Context context, AttributeSet attrs) {generalRoundViewImpl = new GeneralRoundViewImpl(view,context,attrs,R.styleable.GeneralRoundImageView,R.styleable.GeneralRoundImageView_corner_radius);}}
@Overrideprotected void dispatchDraw(Canvas canvas) {generalRoundViewImpl.beforeDispatchDraw(canvas);super.dispatchDraw(canvas);generalRoundViewImpl.afterDispatchDraw(canvas);}
<aral.custom.GeneralRoundImageViewandroid:layout_width="200dp"android:layout_height="200dp"android:layout_gravity="center"android:layout_marginTop="20dp"android:src="@color/colorPrimaryDark"app:corner_radius="60dp" />
具体可以参考GeneralRoundView21Policy类实现,其实本质上只有几行代码,但是为了写的优雅嘛啊哈,你懂的
@Override@TargetApi(Build.VERSION_CODES.LOLLIPOP)protected void dispatchDraw(Canvas canvas) {super.dispatchDraw(canvas);setClipToOutline(true);setOutlineProvider(new ViewOutlineProvider() {@Overridepublic void getOutline(View view, Outline outline) {outline.setRoundRect(0, 0, mContainer.width, mContainer.height, mCornerRadius);}});}
本文发布于:2024-02-04 06:24:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170700981453077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |