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

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

Vs1OUJ.png


其诞生有3个原因

  • 1、之前使用的XferMode裁剪方案在P版本失效
  • 2、xfermode圆角裁剪方案在RecyclerView中使用,滑动时会出现EGL内存泄露问题(系统api未做好内存回收),使用GeneralRound,可以解决L版本上的机器
  • 3、希望可以快速将一个View装饰包装变成支持裁剪圆角的View
  • 4、不希望关闭硬件加速去绘制圆角,不希望使用有锯齿的clipPath API

GETTING STARTED

导入GeneralRoundLayout依赖
  • 1、在Project 的build.gradle中
allprojects {        repositories {            ...            maven { url 'https://jitpack.io' }        }    }
  • 2、在对应module中添加dependency
dependencies {            implementation 'com.github.minminaya:GenaralRoundLayout:1.0.0'    }
  • 3、在你想做裁剪的布局外层包裹
                             

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

GeneralRoundLayout设计初期是为了方便各种布局的扩展,因此可以使任何一个view支持圆角特性,你只需要重写几个方法

  • 1、让你的自定义view比如GeneralRoundImageView实现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文件中输入的时候自动提示)

                
  • 2、让GeneralRoundImageView实现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对象,本质上是裁剪view的helper类,让其初始化,并将view的实现分发到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、重写dispatchDraw方法,将实现类的方法包装super
    @Override    protected void dispatchDraw(Canvas canvas) {        generalRoundViewImpl.beforeDispatchDraw(canvas);        super.dispatchDraw(canvas);        generalRoundViewImpl.afterDispatchDraw(canvas);    }
  • 5、在你要使用的地方
 
  • 6、done

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

  • 1、P版本圆角失效问题,具体可见GcsSloop大神的rclayout,有给出为何失效和解决的方案
  • 2、由于xfermode方案会导致内存泄露,所以这里GeneralRoundLayout在L版本及以上不在使用其进行绘制,转而使用ViewOutlineProvider去进行圆角裁剪,当然,4.3和4.4泄露问题不能够解决,基于现在的18、19和20版本的是用户量,决定保证L版本以上不泄露即可
  • 3、为了兼容18、19和20的圆角可以生效,GeneralRoundViewImpl内部会进行版本去选择RoundViewPolicy

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

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

  • 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

更多相关文章

  1. 改善用户体验
  2. Android中GridView使用
  3. Android上鲜为人知的UI控件介绍和使用
  4. 如何使用命令行编译运行cocos2d-x的android工程
  5. 创建和使用Android(安卓)library工程
  6. [Android] 基于 Linux 命令行构建 Android(安卓)应用(三):构建流程
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. 将Android(安卓)Studio默认布局Constrain
  2. adb 的一些简单命令
  3. 【转】android 有关listview的adapter的
  4. Android(安卓)Studio下OpenCV及JNI开发
  5. 【Android(安卓)- 框架】之可悬浮列表Sti
  6. 使用内部(com.android.internal)和隐藏(@
  7. Android如何将程序打成jar包
  8. 笔记68--继续ListView和GridView1
  9. ViewPager And Fragment
  10. 仿照TapTap做的一个demo,运用了Rxjava+Re