picasso circle transformation android

Solutions on MaxInterview for picasso circle transformation android by the best coders in the world

showing results for - "picasso circle transformation android"
Anna
01 May 2018
1import com.squareup.picasso.Transformation;
2
3public class CircleTransform implements Transformation {
4    @Override
5    public Bitmap transform(Bitmap source) {
6        int size = Math.min(source.getWidth(), source.getHeight());
7
8        int x = (source.getWidth() - size) / 2;
9        int y = (source.getHeight() - size) / 2;
10
11        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
12        if (squaredBitmap != source) {
13            source.recycle();
14        }
15
16        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
17
18        Canvas canvas = new Canvas(bitmap);
19        Paint paint = new Paint();
20        BitmapShader shader = new BitmapShader(squaredBitmap,
21                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
22        paint.setShader(shader);
23        paint.setAntiAlias(true);
24
25        float r = size / 2f;
26        canvas.drawCircle(r, r, r, paint);
27
28        squaredBitmap.recycle();
29        return bitmap;
30    }
31
32    @Override
33    public String key() {
34        return "circle";
35    }
36}