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版本的是用戶(hù)量脖岛,決定保證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)系作者
  • 序言:七十年代末溜宽,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子质帅,更是在濱河造成了極大的恐慌适揉,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件煤惩,死亡現(xiàn)場(chǎng)離奇詭異嫉嘀,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)魄揉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)剪侮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人洛退,你說(shuō)我怎么就攤上這事瓣俯『斓” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵降铸,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我摇零,道長(zhǎng)推掸,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任驻仅,我火速辦了婚禮谅畅,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘噪服。我一直安慰自己毡泻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布粘优。 她就那樣靜靜地躺著仇味,像睡著了一般。 火紅的嫁衣襯著肌膚如雪雹顺。 梳的紋絲不亂的頭發(fā)上丹墨,一...
    開(kāi)封第一講書(shū)人閱讀 51,462評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音嬉愧,去河邊找鬼贩挣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛没酣,可吹牛的內(nèi)容都是我干的王财。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼裕便,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼绒净!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起闪金,我...
    開(kāi)封第一講書(shū)人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疯溺,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后哎垦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體囱嫩,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年漏设,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了墨闲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡郑口,死狀恐怖鸳碧,靈堂內(nèi)的尸體忽然破棺而出盾鳞,到底是詐尸還是另有隱情,我是刑警寧澤瞻离,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布腾仅,位于F島的核電站,受9級(jí)特大地震影響套利,放射性物質(zhì)發(fā)生泄漏推励。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一肉迫、第九天 我趴在偏房一處隱蔽的房頂上張望验辞。 院中可真熱鬧,春花似錦喊衫、人聲如沸跌造。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)壳贪。三九已至,卻和暖如春寝杖,著一層夾襖步出監(jiān)牢的瞬間撑碴,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工朝墩, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留醉拓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓收苏,卻偏偏與公主長(zhǎng)得像亿卤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鹿霸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354