java - 不裁剪图像的 picasso 圆变换

标签 java android canvas bitmap picasso

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

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

这是结果:

enter image description here

最佳答案

使用这个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

关于java - 不裁剪图像的 picasso 圆变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45996891/

相关文章:

java - 在Android项目中使用HTML解析器

java - 任何处理 REST 客户端服务器列表的框架?

javascript - Android WebView 中不显示 Twitter 视频

javascript - 动力学JS : Fading in a fill image when applying it to a polygon shape?

javascript - Canvas 背景使 anchor 不可点击

javascript - HTML5 Canvas - 用剪辑删除后重新绘制新圆圈

java - 另一个 Xpath 困境

java - 在 Excel 工作表中写入内容

android - 我的 Android 单元测试从未在 Eclipse 中完成

java - addPrefencesFromResource 导致 ClassCastException