Search code examples
androidbitmappicasso

Custom bitmap transformation method with Picasso


I have a function which takes in a bitmap as a parameter, and return a bitmap.

public Bitmap setRoundedCornes(Bitmap b, int l, int r, int t, int b)

Before using Picasso, I used this method before my final bitmap was used in the app.

Now I'm using Picasso and am unsure how to apply this method.

Does anyone have any idea?

EDIT

I now have:

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;
    }
}

and

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";
    }
}

Solution

  • Create a class for transformation which implements Transformation

    for e.g.

    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";
        }
    }
    

    now to apply this transformation you can use picasso like this

    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
                            }
                        });