简易的圆角ImageView实现

这里实现一个简易的圆角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));
	}
}

阅读更多
个人分类: Android
上一篇SQLite 数据批量操作
下一篇php连接mssql 安装篇(连载一)
想对作者说点什么? 我来说一句

android 简易圆角ImageView

2015年04月28日 1KB 下载

没有更多推荐了,返回首页

关闭
关闭