Picasso 做圆角有合理的方法吗
- 不会显着减慢绘图速度
- 与硬件层一起工作
- 不为每个图像创建额外的位图
- 允许将下载的位图调整为目标 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 面片来呈现圆角的外观。
最佳答案
这段代码对我来说很好用
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/