android:用毕加索创建圆形图像 [英] android: create circular image with picasso
问题描述
有人问过这个问题,并且对我正在使用的 Picasso 版本做出了承诺:如何使用 Picasso 将圆形位图发送到 ImageView?我是毕加索的新手,我唯一使用过的是
The question had been asked and there had been a promise made for the very version of Picasso that I am using: How do I send a circular bitmap to an ImageView using Picasso? I am new to Picasso and only thing I have used is
Picasso.with(context).load(url).resize(w, h).into(imageview);
我已经找到了 https://gist.github.com/julianshen/5829333 但我不确定如何以一种不尴尬的方式将它与上面的行结合起来.
I have already found https://gist.github.com/julianshen/5829333 but I am not sure how to combine it with the line above in a non-awkward way.
推荐答案
先研究一下,因为有可用的答案.无论如何,请关注此链接并仔细阅读以了解如何使用它.
Research a bit before as there are answers available. Anyhow, follow This Link and read it carefully to know how to use it.
试试这个:
import com.squareup.picasso.Transformation;
public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
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,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle";
}
}
然后简单地应用它:
Picasso.with(activity).load(mayorShipImageLink).transform(new CircleTransform()).into(ImageView);
这篇关于android:用毕加索创建圆形图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!