您的位置:首页 > 其它

简易的圆角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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: