自定义圆角或圆形drawable

/**
 * 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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值