Android中使用Bitmap类将矩形图片转为圆形的方法

来源:互联网 发布:尼古丁的好处 知乎 编辑:程序博客网 时间:2024/05/18 03:40

一般要做正圆形图片,只能是正方形的基础上才能实现,否则就变成椭圆了,下面说说如何使长方形的图片生成正圆形图片

public class CircleTransform implements Transformation {@Overridepublic Bitmap transform(Bitmap source) {    int size = Math.min(source.getWidth(), source.getHeight());    int x = (source.getWidth() - size) / 2;    int y = (source.getHeight() - size) / 2;    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);    if (squaredBitmap != source) {        source.recycle();    }    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig() != null            ? source.getConfig() : Bitmap.Config.ARGB_8888);    Canvas canvas = new Canvas(bitmap);    Paint paint = new Paint();    BitmapShader shader = new BitmapShader(squaredBitmap,            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);    paint.setShader(shader);    paint.setAntiAlias(true);    float r = size / 2f;    canvas.drawCircle(r, r, r, paint);    squaredBitmap.recycle();    return bitmap;}@Overridepublic String key() {    return "circle";}}

0 0
原创粉丝点击