简易的圆角ImageView实现
2015-07-08 10:23
274 查看
这里实现一个简易的圆角ImageViewpublic class RoundCornerImageView extends ImageView {
RectF rect;
Paint paint;
Path clipPath;
int dp1;
int dp3;
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);
int textColor = a.getColor(R.styleable.RoundedImageView_border_color, 0XFFFFFFFF);
dp1 = DensityUtil.dip2px(context, 1);
dp3 = DensityUtil.dip2px(context, 3);
rect = new RectF();
clipPath = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(dp1);
paint.setColor(textColor);
paint.setStyle(Style.STROKE);
closeHardwareAccelarated();
a.recycle();
}
@SuppressLint("NewApi")
private void closeHardwareAccelarated() {
if (VERSION.SDK_INT >= 11) {
setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
}
}
@Override
public void onDraw(Canvas canvas) {
long a = System.currentTimeMillis();
rect.set(0, 0, getWidth(), getHeight());
clipPath.addRoundRect(rect, dp3, dp3, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
canvas.drawRoundRect(rect, dp3, dp3, paint);
Util.LogE("CornerImage", "CornerImage:" + (System.currentTimeMillis() - a));
}
}
RectF rect;
Paint paint;
Path clipPath;
int dp1;
int dp3;
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);
int textColor = a.getColor(R.styleable.RoundedImageView_border_color, 0XFFFFFFFF);
dp1 = DensityUtil.dip2px(context, 1);
dp3 = DensityUtil.dip2px(context, 3);
rect = new RectF();
clipPath = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(dp1);
paint.setColor(textColor);
paint.setStyle(Style.STROKE);
closeHardwareAccelarated();
a.recycle();
}
@SuppressLint("NewApi")
private void closeHardwareAccelarated() {
if (VERSION.SDK_INT >= 11) {
setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
}
}
@Override
public void onDraw(Canvas canvas) {
long a = System.currentTimeMillis();
rect.set(0, 0, getWidth(), getHeight());
clipPath.addRoundRect(rect, dp3, dp3, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
canvas.drawRoundRect(rect, dp3, dp3, paint);
Util.LogE("CornerImage", "CornerImage:" + (System.currentTimeMillis() - a));
}
}
相关文章推荐
- jquery的setinterval使用方法
- [Qt] Qt时间 - QDateTime
- Number of Digit One
- v机器学习中的算法(2)-支持向量机(SVM)基础
- 用strings命令查看kafka-log内容
- 【Oracle学习笔记-5--】集合操作之union,intersect和minus操作
- git服务器搭建
- Red Hat (Cent OS)安装tmux工具
- Nginx服务器中HTTP 301跳转到带www的域名的方法
- java使用默认线程池踩过的坑(一)
- java中编码问题的处理方案
- 一位大牛的JAVA学习资料
- 深入了解SQL注入绕过waf和过滤机制
- C#以排列组合中的“组合”方式遍历某个文件夹下的所有文件
- 适配器模式原理及实例介绍
- CSDN有不少bug啊
- 黑马程序员——Java基础---String
- 深入理解DIP、IoC、DI以及IoC容器
- Python根据区号生成手机号码的方法
- hadoop生态系统