毕加索圈变换,无需裁剪图像 [英] Picasso Circle Transformation without Cropping Image

查看:44
本文介绍了毕加索圈变换,无需裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不裁剪图像的情况下进行圆环变换?我已经尝试了2天,但我一直失败.这是我到目前为止最接近的图像,但是如您所见,图像不在圆圈内居中.

How do I make Circle Transformation without cropping the image? I've been trying for 2 days and I keep failing. This is the closest I've come so far, but as you can see, the image is not centered inside the circle.

@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    Bitmap squaredBitmap = Bitmap.createScaledBitmap(source,size-50, size-50,true);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size
            , source.getConfig());

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

这是结果:

推荐答案

使用此CircleImageView:

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image1"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/menu1"
    app:border_width="2dp"
    app:border_color="@color/white"
/>

有关更多信息: https://github.com/hdodenhof/CircleImageView

这篇关于毕加索圈变换,无需裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆