gpt4 book ai didi

android - picasso 的圆角

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:32:18 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com