关闭

简易的圆角ImageView实现

106人阅读 评论(0) 收藏 举报
分类:

这里实现一个简易的圆角ImageView

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:493次
    • 积分:50
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档