Android Picasso transformation not applied on each view show

1.3k Views Asked by At

I'm using Picasso :

Picasso.with(mContext)
                .load(url)
                .placeholder(fallback)
                .error(fallback)
                .transform(new CircleTransform(userStatus))
                .into(this);

And Transformation to apply stroke and roundness to the image:

public class CircleTransform implements Transformation {
        private int userStatus = UserProfile.STATUS_TYPE_NONE;

        public CircleTransform(int userStatus) {
            this.userStatus = userStatus;
        }

        @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);
            if (UserProfile.STATUS_TYPE_NONE != userStatus) {
                Paint paintStroke = new Paint();
                paintStroke.setAntiAlias(true);
                if (UserProfile.STATUS_TYPE_BRONZE == userStatus)
                    paintStroke.setColor(Colors.bronzeColor);
                else if (UserProfile.STATUS_TYPE_SILVER == userStatus)
                    paintStroke.setColor(Colors.silverColor);
                else if (UserProfile.STATUS_TYPE_GOLD == userStatus)
                    paintStroke.setColor(Colors.goldColor);
                else if (UserProfile.STATUS_TYPE_PLATINUM == userStatus)
                    paintStroke.setColor(Colors.platinumColor);
                else if (UserProfile.STATUS_TYPE_DIAMOND == userStatus)
                    paintStroke.setColor(Colors.diamondColor);

                paintStroke.setStyle(Paint.Style.STROKE);
                float stroke = size * CIRCLE_SIZE_IN_PERCENT;
                paintStroke.setStrokeWidth(stroke);
                canvas.drawCircle(r, r, r - (stroke / 2), paintStroke);
            }
            squaredBitmap.recycle();
            return bitmap;
        }

        @Override
        public String key() {
            return "circle";
        }
    }

the problem is that the public Bitmap transform(Bitmap source) is not getting called each time the image is loaded.

2

There are 2 best solutions below

1
SpyZip On

Ok I found the solution, the transformation key stays the same and it dose not apply a new one with the same key, so I changed

 @Override
        public String key() {
            return "circle";
        }

to:

@Override
        public String key() {
            return "circle" + value;
        }

value - should be unique parameter defining the instance of the ImageView. I hope it helps someone :)

1
Jake Wharton On

The userStatus variable changes the behavior of the transformation and thus must be included in the cache key. Otherwise Picasso thinks that any circle transformation can be replaced with another.

@Override
public String key() {
    return "circle" + userStatus;
}