/**
* Created by guowenhui on 2016/7/6.圆角图片
*/
public class RoundRecDrawable extends Drawable {
//核心方法
private Paint paint;
private RectF rect;
private Bitmap bitmap;
int width;
private boolean ifShowRound;
public RoundRecDrawable(Bitmap bitmap, boolean ifShowRound) {
this.bitmap = bitmap;
this.ifShowRound = ifShowRound;
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//形状,着色器,画一个图片 tileX tileY方向上图片不够如何展示 clamp是延伸的意思
paint = new Paint();
paint.setAntiAlias(true);//抗锯齿
paint.setShader(bitmapShader);
rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());//初始化区域
width = Math.min(bitmap.getWidth(), bitmap.getHeight());
}
@Override
public void draw(Canvas canvas) {
if (ifShowRound) {
canvas.drawCircle(rect.left + width / 2, rect.top + width / 2, width / 2, paint);
} else {
canvas.drawRoundRect(rect, 30, 30, paint);//float rx、float ry方向上圆的半径 一致为圆弧 圆角矩形
}
}
//透明度
@Override
public void setAlpha(int i) {
paint.setAlpha(i);
}
//颜色滤镜
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getIntrinsicHeight() {
return bitmap.getHeight();
}
@Override
public int getIntrinsicWidth() {
return bitmap.getWidth();
}
//核心方法
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
rect = new RectF(left, top, right, bottom);
}
@Override
public int getOpacity() {
return 0;
}
}
自定义圆角或圆形drawable
最新推荐文章于 2022-10-31 23:34:27 发布