android - picasso 的圆角

标签 android picasso

Picasso 做圆角有合理的方法吗

  1. 不会显着减慢绘图速度
  2. 与硬件层一起工作
  3. 不为每个图像创建额外的位图
  4. 允许将下载的位图调整为目标 ImageView 的大小

picasso 关于圆角的大部分建议都建议使用转换,但我还没有看到一个示例不创建额外的位图作为转换的一部分。

这似乎是因为 Picasso 只使用位图,而制作圆角的技巧利用了这样一个事实,即您可以合理有效地动态绘制圆角(大多数解决方案使用类似于 http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ 的东西)。

用 Volley 做这个有点 hacky 但可能,只需将 ImageView 的类型更改为采用自定义可绘制对象的东西,从而绘制圆角。由于 Picasso 需要位图(至少,只有位图 -> 位图转换),所以这是不可能的,因为将可绘制对象转换为位图会在此过程中创建位图。

一个解决方案是在我自己的分支中修改 picasso,添加位图 -> 可绘制转换,但我想有更好的方法来解决这个问题。

我不想在 View 顶部绘制一个 9 面片来呈现圆角的外观。

最佳答案

  1. 这段代码对我来说很好用
    enter image description here

    Picasso.with(getApplicationContext())
            .load(sp.getString("image_url", ""))
            .transform(new RoundedTransformation(100, 0))
            .fit()
            .into(userProfileImg);
    

//这里是 make 的类

    public class RoundedTransformation implements
        com.squareup.picasso.Transformation {
    private final int radius;
    private final int margin; // dp

    // radius is corner radii in dp
    // margin is the board in dp
    public RoundedTransformation(final int radius, final int margin) {
        this.radius = radius;
        this.margin = margin;
    }

    @Override
    public Bitmap transform(final Bitmap source) {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP));

        Bitmap output = Bitmap.createBitmap(source.getWidth(),
                source.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        canvas.drawRoundRect(new RectF(margin, margin, source.getWidth()
                - margin, source.getHeight() - margin), radius, radius, paint);

        if (source != output) {
            source.recycle();
        }

        return output;
    }

    @Override
    public String key() {
        return "rounded";
    }
}

关于android - picasso 的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22363515/

相关文章:

android - MPAndroidChart 设置当前可见的 X 轴

android - RecyclerView如何实现OnClickListener?

android - Picasso : want to cache a image without displaying it . 以便我以后可以使用它

android - 从服务器获取图像 URL 时无法在 ImageView 中加载默认图像

android - Picasso 的 fit()、resize() 和 transform() 会在下载前缩小图像尺寸吗?

android - Picasso Android 不加载类型 "content://"的 Uri

java - 在 Webview 中加载 Flash,我迷路了

android - 如何在 OnDestroy (Android Firebase) 中删除此监听器?

android - 按下时自动更改按钮背景和文本外观(如 iOS)?

Android Picasso 处理方向改变