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

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

Android通用圓角布局,可以解決Android P版本xfermode方案裁剪黑邊問(wèn)題和xfermode在列表view中使用滑動(dòng)時(shí)EGL內(nèi)存泄露問(wèn)題


其誕生有3個(gè)原因

  • 1、之前使用的XferMode裁剪方案在P版本失效
  • 2、xfermode圓角裁剪方案在RecyclerView中使用,滑動(dòng)時(shí)會(huì)出現(xiàn)EGL內(nèi)存泄露問(wèn)題(系統(tǒng)api未做好內(nèi)存回收),使用GeneralRound,可以解決L版本上的機(jī)器
  • 3、希望可以快速將一個(gè)View裝飾包裝變成支持裁剪圓角的View
  • 4、不希望關(guān)閉硬件加速去繪制圓角,不希望使用有鋸齒的clipPath API

GETTING STARTED

導(dǎo)入GeneralRoundLayout依賴(lài)
  • 1、在Project 的build.gradle中
allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

  • 2、在對(duì)應(yīng)module中添加dependency

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

  • 3、在你想做裁剪的布局外層包裹

 <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設(shè)計(jì)初期是為了方便各種布局的擴(kuò)展,因此可以使任何一個(gè)view支持圓角特性,你只需要重寫(xiě)幾個(gè)方法

  • 1、讓你的自定義view比如GeneralRoundImageView實(shí)現(xiàn)IRoundView接口

interface IRoundView {
    fun setCornerRadius(cornerRadius: Float)
    fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)
}

  • 2、定義attrs屬性

在你的attrs的文件中,定義declare-styleable屬性(為了可以在xml文件中輸入的時(shí)候自動(dòng)提示)


<declare-styleable name="GeneralRoundImageView">
        <attr name="corner_radius" />
    </declare-styleable>
    
  • 2、讓GeneralRoundImageView實(shí)現(xiàn)IRoundView接口的方法

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

}

  • 3、在GeneralRoundImageView中定義GeneralRoundViewImpl對(duì)象,本質(zhì)上是裁剪view的helper類(lèi),讓其初始化,并將view的實(shí)現(xiàn)分發(fā)到GeneralRoundViewImpl

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

}

  • 4、重寫(xiě)dispatchDraw方法,將實(shí)現(xiàn)類(lèi)的方法包裝super
    @Override
    protected void dispatchDraw(Canvas canvas) {
        generalRoundViewImpl.beforeDispatchDraw(canvas);
        super.dispatchDraw(canvas);
        generalRoundViewImpl.afterDispatchDraw(canvas);
    }

  • 5、在你要使用的地方
 <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" />

  • 6、done

如何同時(shí)解決xfermode內(nèi)存泄露和Android P圓角失效問(wèn)題

  • 1、P版本圓角失效問(wèn)題,具體可見(jiàn)GcsSloop大神的rclayout,有給出為何失效和解決的方案
  • 2、由于xfermode方案會(huì)導(dǎo)致內(nèi)存泄露,所以這里GeneralRoundLayout在L版本及以上不在使用其進(jìn)行繪制,轉(zhuǎn)而使用ViewOutlineProvider去進(jìn)行圓角裁剪,當(dāng)然,4.3和4.4泄露問(wèn)題不能夠解決,基于現(xiàn)在的18、19和20版本的是用戶量,決定保證L版本以上不泄露即可
  • 3、為了兼容18、19和20的圓角可以生效,GeneralRoundViewImpl內(nèi)部會(huì)進(jìn)行版本去選擇RoundViewPolicy

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

具體可以參考GeneralRoundView21Policy類(lèi)實(shí)現(xiàn),其實(shí)本質(zhì)上只有幾行代碼,但是為了寫(xiě)的優(yōu)雅嘛啊哈,你懂的

  • 1、在你自定義view的dispatchDraw方法中直接使用ViewOutlineProvider

    @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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容