简易的圆角ImageView实现

来源:互联网 发布:windows平板手势 编辑:程序博客网 时间:2024/05/01 21:23

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