我有一个函数,它将位图作为参数,并返回一个位图。
public Bitmap setRoundedCornes(Bitmap b, int l, int r, int t, int b)
在使用Picasso之前,我在应用程序中使用了最终的位图之前使用了这种方法。
现在我使用Picasso并且不确定如何应用这种方法。
有没有人有任何想法?
修改
我现在有:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) {
try {
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
// the float array passed to this function defines the x/y values of the corners
// it starts top-left and works clockwise
// so top-left-x, top-left-y, top-right-x etc
RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null);
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
rrs.resize(bitmap.getWidth(), bitmap.getHeight());
rrs.draw(canvas, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}catch(Exception e){
return bitmap;
}
}
和
public class MyTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
//your logic to transform goes here
return getRoundedCornerBitmap(source, 20, 20, 20, 20, 0, 0, 0, 0);
}
@Override
public String key() {
return "circle";
}
}
答案 0 :(得分:1)
创建一个实现Transformation
的转换类例如。
public class MyTransform 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,
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;
}
@Override
public String key() {
return "circle";
}
}
现在要应用此转换,您可以像这样使用毕加索
Picasso.with(getContext())
.load(url)
.transform(new MyTransform())
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// do something if its loaded successfully
}
@Override
public void onError() {
// do something if its not loaded successfully
}
});
答案 1 :(得分:0)
是的,你可以像这样应用转型:
public class CropSquareTransformation 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 result = Bitmap.createBitmap(source, x, y, size, size);
if (result != source) {
source.recycle();
}
return result;
}
@Override public String key() { return "square()"; }
}
参见Picasso文档