public class CornerImageView extends ImageView{
private Paint mPaint = new Paint();
//圆角半径
private float rx = 0;
private float ry = 0;
public CornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
//画背景
if(getBackground() != null)
{
getBackground().setBounds(0, 0, getWidth(), getHeight());
}
//设置画笔
Bitmap bitmap = drawableToBitmap(getDrawable());
mPaint.setShader(new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP));
//画圆角矩形
canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), rx, ry, mPaint);
}
//设置圆角半径
public void setRoundRadius(float rx, float ry)
{
this.rx = rx;
this.ry = ry;
invalidate();
}
//drawable转bitmap
private Bitmap drawableToBitmap(Drawable drawable)
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();
}
}
android圆角图片的实现
最新推荐文章于 2021-05-29 06:58:24 发布