两种实现CircleImageView的方式

使用Paint.setXferMode

参考链接: 转自博客,请点击此处

要想事项CircleImageView,分成两部分:

  • 创建圆形bitmap
  • 使用canvas画bitmap

创建圆形bitmap

private Bitmap createRoundConerImage(Bitmap source)
{
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    //创建一个相同大小的bitmap对象
    Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
    //封装成Canvas对象
    Canvas canvas = new Canvas(target);
    RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
    //通过canvas画圆形,变为圆形画布
    canvas.drawRoundRect(rect, mRadius, mRadius, paint);
    //设置画笔的叠加方式
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    //将原图片画在画布上
    canvas.drawBitmap(source, 0, 0, paint);
    //取出bitmap对象
    return target;
}

画生成的圆形bitmap对象

在onDraw(Canvas canvas)中添加:

canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);

使用Shader

参考链接:http://blog.csdn.net/lmj623565791/article/details/41967509

  • 将drawable变为bitmap
  • 将bitmap通过BitmapShader关联到Paint
  • 通过paint对象去在canvas对象上drawCircle

将drawable对象变为bitmap

public Bitmap drawable2bitmap(Drawable drawable){
    if(drawable == null){
        retrurn null;
    }
    if(drawable instanceof BitampDrawable){
        retrun ((BitampDrawble)drawable).getBitmap();
    }
    int w = drawble.getIntrisicWidth();
    int h = drawble.getIntrisicHeight();
    Bitmap newBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    drawable.setBounds(0,0,w,h);
    drawable.draw(Canvas);
    return newBitamp;
}

将bitmapshader和要展示的bitmap进行关联

mMatrix.setScale(scale,scale);//设置放大的倍数
mBitmapShader.setLocalMatrix(mMatrix);

BitmapShader mBitmapShader = new BitmapShader(newBitmap,TileMode.CLAMP,TileMode.CLAMP);
mBitmapPaint.setShader(mBitmapShader);

再进行画圆

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值