Android一些容易被忽略的类-RoundedBitmapDrawable

android 中提供了很多实用高效的类,但往往都会被大部分人忽略,例如今天说的RoundedBitmapDrawable,用来实现图片的圆角,大多情况多数人是拿一个第三方的直接使用。

最终效果

首先来看下原图和处理后效果,以及做一些扩展,如添加一个边框

《Android一些容易被忽略的类-RoundedBitmapDrawable》

《Android一些容易被忽略的类-RoundedBitmapDrawable》

实现过程

1.创建一个简单的圆角图片

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCornerRadius(100);

通过RoundedBitmapDrawableFactory 传递要转换bitmap 我就可以很简单的生成一个如下图的圆角图片

《Android一些容易被忽略的类-RoundedBitmapDrawable》

2.创建一个正圆形图片

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
roundedBitmapDrawable.setCircular(true);

可以看到我们仅仅只是改了一个属性就实现了如下图正圆形的转换,但你可能已经发现图像有一些变形,因为内部在接收到circular == true后先是对图像进行了转换为正方形的操作,这个操作是一个伸缩放操作,而不是裁剪,所以图像发生了变形,所以在使用setCircular时最好能保证你的原图时一个正方形的,如果不是的话,下面我们也会给出相应解决方案

《Android一些容易被忽略的类-RoundedBitmapDrawable》

3.创建一个正圆形带有边框的图片

private Drawable createRoundImageWithBorder(Bitmap bitmap){        
    //原图宽度
    int bitmapWidth = bitmap.getWidth();
    //原图高度
    int bitmapHeight = bitmap.getHeight();
    //边框宽度 pixel
    int borderWidthHalf = 20;

    //转换为正方形后的宽高
    int bitmapSquareWidth = Math.min(bitmapWidth,bitmapHeight);

    //最终图像的宽高
    int newBitmapSquareWidth = bitmapSquareWidth+borderWidthHalf;

    Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth,newBitmapSquareWidth,Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);
    int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
    int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

    //裁剪后图像,注意X,Y要除以2 来进行一个中心裁剪
    canvas.drawBitmap(bitmap, x/2, y/2, null);
    Paint borderPaint = new Paint();
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(borderWidthHalf);
    borderPaint.setColor(Color.WHITE);

    //添加边框
    canvas.drawCircle(canvas.getWidth()/2, canvas.getWidth()/2, newBitmapSquareWidth/2, borderPaint);

    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),roundedBitmap);
    roundedBitmapDrawable.setGravity(Gravity.CENTER);
    roundedBitmapDrawable.setCircular(true);
    return roundedBitmapDrawable;
}

我们自己进行对bitmap的裁剪来转换成正方形,就解决了上面提到的拉伸问题,再绘制边框就实现了一个如下带边框的正圆形图片

《Android一些容易被忽略的类-RoundedBitmapDrawable》

其他

RoundedBitmapDrawable 也可以直接设置转换过程的

  • setAlpha 透明度
  • setAntiAlias 抗锯齿
  • setDither 防抖动

这些操作,来更好的工作

到这个里我们就可以把项目中的圆角图片的控件更换一下,平时记得多留意一下系统提供的一些API,可能会帮我们节省不少时间。

    原文作者:李涛丶
    原文地址: https://www.jianshu.com/p/ed42d8c90a45
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞