summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/citra/citra_emu/utils/PicassoRoundedCornersTransformation.java
blob: 892b463877ed0b72ae938cd5f96e2457725b220f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.citra.citra_emu.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;

public class PicassoRoundedCornersTransformation implements Transformation {
    @Override
    public Bitmap transform(Bitmap icon) {
        final int width = icon.getWidth();
        final int height = icon.getHeight();
        final Rect rect = new Rect(0, 0, width, height);
        final int size = Math.min(width, height);
        final int x = (width - size) / 2;
        final int y = (height - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(icon, x, y, size, size);
        if (squaredBitmap != icon) {
            icon.recycle();
        }

        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(shader);

        canvas.drawRoundRect(new RectF(rect), 10, 10, paint);

        squaredBitmap.recycle();

        return output;
    }

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