高级UIAndroid 自定义viewAndroid Other

Android通用圆角布局(兼容Android P)

2019-06-09  本文已影响29人  minminaya

Android通用圆角布局(兼容Android P)

Android通用圆角布局,可以解决Android P版本xfermode方案裁剪黑边问题和xfermode在列表view中使用滑动时EGL内存泄露问题

Vs1OUJ.png

其诞生有3个原因


GETTING STARTED

导入GeneralRoundLayout依赖
allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }


dependencies {
            implementation 'com.github.minminaya:GenaralRoundLayout:1.0.0'
    }


 <com.minminaya.widget.GeneralRoundFrameLayout
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            app:corner_radius="30dp">

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent" />
        </com.minminaya.widget.GeneralRoundFrameLayout>
        

给自定义view加上圆角裁剪特性

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);
    }

    @Override
    public void setCornerRadius(float cornerRadius) {
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(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);
    }

    @Override
    public void setCornerRadius(float cornerRadius) {
        generalRoundViewImpl.setCornerRadius(cornerRadius);
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        generalRoundViewImpl.onLayout(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);
    }

}

    @Override
    protected void dispatchDraw(Canvas canvas) {
        generalRoundViewImpl.beforeDispatchDraw(canvas);
        super.dispatchDraw(canvas);
        generalRoundViewImpl.afterDispatchDraw(canvas);
    }

 <com.minminaya.genaral.custom.GeneralRoundImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:src="@color/colorPrimaryDark"
            app:corner_radius="60dp" />

如何同时解决xfermode内存泄露和Android P圆角失效问题

什么?,你想快速集成,但又不想要那么多代码?(L版本及以上)

具体可以参考GeneralRoundView21Policy类实现,其实本质上只有几行代码,但是为了写的优雅嘛啊哈,你懂的


    @Override
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        setClipToOutline(true);
        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, mContainer.width, mContainer.height, mCornerRadius);
            }
        });
    }
    

GenaralRoundLayout
上一篇 下一篇

猜你喜欢

热点阅读