目的:

1.实现自定义ReleativeLayout圆角化

实现:

1.在res目录中新建attrs.xml文件,自定义属性如下。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundBgRelativeLayout">
<attr name="borderRadius" format="dimension"/><!-- 半径-->
<attr name="src" format="reference"/><!-- 图片资源-->
</declare-styleable>
</resources>

2.新建自定义Layout继承RelativeLayout,重写构造方法。

public class RoundBgRelativeLayout extends RelativeLayout {

    /**
* 圆角大小
*/
private int mRadius; /**背景图片*/
private Bitmap mSrc; public RoundBgRelativeLayout(Context context) {
this(context,null);
} public RoundBgRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
} public RoundBgRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
TypedArray arr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundBgRelativeLayout, defStyleAttr, 0);
int indexCount = arr.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int index = arr.getIndex(i);
switch (index){
case R.styleable.RoundBgRelativeLayout_src:
mSrc = BitmapFactory.decodeResource(getResources(), arr.getResourceId(index, 0));
break;
case R.styleable.RoundBgRelativeLayout_borderRadius:
mRadius= (int) arr.getDimension(index,20);
break;
default:
break;
} }
arr.recycle();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mSrc != null){
int width = getMeasuredWidth();//测量宽度
int height = getMeasuredHeight();//测量高度
mSrc = Bitmap.createScaledBitmap(mSrc,width,height,false);
canvas.drawBitmap(createRoundImage(mSrc,width,height),0,0,null);//绘制圆角背景
}
super.onDraw(canvas);
} private Bitmap createRoundImage(Bitmap mSrc, int width, int height) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap target = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rectF = new RectF(0,0,width,height); //绘制圆角矩形
canvas.drawRoundRect(rectF,mRadius,mRadius,paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//该模式可以让两个重叠,并取交集
//绘制图片
canvas.drawBitmap(mSrc,0,0,paint); return target;
} public void setBgResource(int r){
this.mSrc = BitmapFactory.decodeResource(getResources(),r);
invalidate();
}
}

实现原理:

主要靠PorterDuff.Mode.SRC_IN 这种模式,第一个图绘制为圆角矩形,第二个图绘制是个BItmap,两者取交集,就实现了圆形图片的效果。

PorterDuff.Mode 16种效果图,如下。

最终实现:

参考

1.鸿神:http://blog.csdn.net/lmj623565791/article/details/24555655

2.https://blog.csdn.net/fhkatuz674/article/details/39271581

最新文章

  1. Spring Boot自定义错误页面,Whitelabel Error Page处理方式
  2. Shape comparison language
  3. 手机中点击链接或button按钮出现黄色边框的解决办法
  4. Linux软件的安装与卸载
  5. html的textarea控制字数小案例
  6. G - Not so Mobile
  7. 深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
  8. Science论文&quot;Clustering by fast search and find of density peaks&quot;学习笔记
  9. DRM in Android
  10. C#实现SOAP调用WebService
  11. Obj-C的hello,world 2
  12. 移动app的一些心得
  13. Selenium Grid2
  14. css实现网格背景
  15. mongodb副本集中其中一个节点宕机无法重启的问题
  16. [二十四]JavaIO之PrintWriter
  17. 【MySQL】MySQL视图创建、查询。
  18. ssd制作数据和训练
  19. Spring Boot + Spring Cloud 实现权限管理系统 (集成 Shiro 框架)
  20. Java学习笔记26(异常)

热门文章

  1. javascript中创建新节点的方法 标签: javascript 2016-12-25 11:38 55人阅读 评论(0)
  2. 43.mapping的理解
  3. STM32 HAL库的定时器中断回调函数跟串口中断回调函数
  4. CGLib与JDK的动态代理
  5. POJ 1021 人品题
  6. NEFU 115
  7. 检測wifi是否须要portal验证 公共场所wifi验证
  8. C++开发人脸性别识别教程(19)——界面美化
  9. C语言播放声音最简单的两种方法
  10. get post 的区别