android圆角图片的实现

来源:互联网 发布:炼数成金教程软件测试 编辑:程序博客网 时间:2024/05/16 01:36
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();    }}
0 0
原创粉丝点击