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