7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | *
11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | *
13 | * Unless required by applicable law or agreed to in writing, software
14 | * distributed under the License is distributed on an "AS IS" BASIS,
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 | * See the License for the specific language governing permissions and
17 | * limitations under the License.
18 | */
19 |
20 | import android.content.Context;
21 | import android.graphics.Bitmap;
22 |
23 | import androidx.annotation.NonNull;
24 |
25 | import com.bumptech.glide.Glide;
26 | import com.bumptech.glide.load.Transformation;
27 | import com.bumptech.glide.load.engine.Resource;
28 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
29 | import com.bumptech.glide.load.resource.bitmap.BitmapResource;
30 | import com.bumptech.glide.request.target.Target;
31 | import com.bumptech.glide.util.Util;
32 |
33 | import java.security.MessageDigest;
34 |
35 | public abstract class BitmapTransformation implements Transformation
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.Canvas;
22 | import android.graphics.Paint;
23 | import android.renderscript.RSRuntimeException;
24 |
25 | import androidx.annotation.NonNull;
26 |
27 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
28 |
29 | import java.security.MessageDigest;
30 |
31 | import jp.wasabeef.glide.transformations.internal.FastBlur;
32 | import jp.wasabeef.glide.transformations.internal.RSBlur;
33 |
34 | public class BlurTransformation extends BitmapTransformation {
35 |
36 | private static final int VERSION = 1;
37 | private static final String ID =
38 | "jp.wasabeef.glide.transformations.BlurTransformation." + VERSION;
39 |
40 | private static final int MAX_RADIUS = 25;
41 | private static final int DEFAULT_DOWN_SAMPLING = 1;
42 |
43 | private final int radius;
44 | private final int sampling;
45 |
46 | public BlurTransformation() {
47 | this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
48 | }
49 |
50 | public BlurTransformation(int radius) {
51 | this(radius, DEFAULT_DOWN_SAMPLING);
52 | }
53 |
54 | public BlurTransformation(int radius, int sampling) {
55 | this.radius = radius;
56 | this.sampling = sampling;
57 | }
58 |
59 | @Override
60 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
61 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
62 |
63 | int width = toTransform.getWidth();
64 | int height = toTransform.getHeight();
65 | int scaledWidth = width / sampling;
66 | int scaledHeight = height / sampling;
67 |
68 | Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
69 |
70 | setCanvasBitmapDensity(toTransform, bitmap);
71 |
72 | Canvas canvas = new Canvas(bitmap);
73 | canvas.scale(1 / (float) sampling, 1 / (float) sampling);
74 | Paint paint = new Paint();
75 | paint.setFlags(Paint.FILTER_BITMAP_FLAG);
76 | canvas.drawBitmap(toTransform, 0, 0, paint);
77 |
78 | try {
79 | bitmap = RSBlur.blur(context, bitmap, radius);
80 | } catch (RSRuntimeException e) {
81 | bitmap = FastBlur.blur(bitmap, radius, true);
82 | }
83 |
84 | return bitmap;
85 | }
86 |
87 | @Override
88 | public String toString() {
89 | return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
90 | }
91 |
92 | @Override
93 | public boolean equals(Object o) {
94 | return o instanceof BlurTransformation &&
95 | ((BlurTransformation) o).radius == radius &&
96 | ((BlurTransformation) o).sampling == sampling;
97 | }
98 |
99 | @Override
100 | public int hashCode() {
101 | return ID.hashCode() + radius * 1000 + sampling * 10;
102 | }
103 |
104 | @Override
105 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
106 | messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/ColorFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.Canvas;
22 | import android.graphics.Paint;
23 | import android.graphics.PorterDuff;
24 | import android.graphics.PorterDuffColorFilter;
25 |
26 | import androidx.annotation.NonNull;
27 |
28 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
29 |
30 | import java.security.MessageDigest;
31 |
32 | public class ColorFilterTransformation extends BitmapTransformation {
33 |
34 | private static final int VERSION = 1;
35 | private static final String ID =
36 | "jp.wasabeef.glide.transformations.ColorFilterTransformation." + VERSION;
37 |
38 | private final int color;
39 |
40 | public ColorFilterTransformation(int color) {
41 | this.color = color;
42 | }
43 |
44 | @Override
45 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
46 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
47 | int width = toTransform.getWidth();
48 | int height = toTransform.getHeight();
49 |
50 | Bitmap.Config config =
51 | toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
52 | Bitmap bitmap = pool.get(width, height, config);
53 |
54 | setCanvasBitmapDensity(toTransform, bitmap);
55 |
56 | Canvas canvas = new Canvas(bitmap);
57 | Paint paint = new Paint();
58 | paint.setAntiAlias(true);
59 | paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
60 | canvas.drawBitmap(toTransform, 0, 0, paint);
61 |
62 | return bitmap;
63 | }
64 |
65 | @Override
66 | public String toString() {
67 | return "ColorFilterTransformation(color=" + color + ")";
68 | }
69 |
70 | @Override
71 | public boolean equals(Object o) {
72 | return o instanceof ColorFilterTransformation &&
73 | ((ColorFilterTransformation) o).color == color;
74 | }
75 |
76 | @Override
77 | public int hashCode() {
78 | return ID.hashCode() + color * 10;
79 | }
80 |
81 | @Override
82 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
83 | messageDigest.update((ID + color).getBytes(CHARSET));
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/CropCircleTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 |
22 | import androidx.annotation.NonNull;
23 |
24 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
25 | import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
26 | import com.bumptech.glide.request.RequestOptions;
27 |
28 | import java.security.MessageDigest;
29 |
30 | /**
31 | * @deprecated Use {@link RequestOptions#circleCrop()}.
32 | */
33 | @Deprecated
34 | public class CropCircleTransformation extends BitmapTransformation {
35 |
36 | private static final int VERSION = 1;
37 | private static final String ID =
38 | "jp.wasabeef.glide.transformations.CropCircleTransformation." + VERSION;
39 |
40 | @Override
41 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
42 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
43 | return TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "CropCircleTransformation()";
49 | }
50 |
51 | @Override
52 | public boolean equals(Object o) {
53 | return o instanceof CropCircleTransformation;
54 | }
55 |
56 | @Override
57 | public int hashCode() {
58 | return ID.hashCode();
59 | }
60 |
61 | @Override
62 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
63 | messageDigest.update((ID).getBytes(CHARSET));
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/CropCircleWithBorderTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Canvas;
6 | import android.graphics.Color;
7 | import android.graphics.Paint;
8 |
9 | import androidx.annotation.ColorInt;
10 | import androidx.annotation.NonNull;
11 |
12 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
13 | import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
14 |
15 | import java.security.MessageDigest;
16 |
17 | import jp.wasabeef.glide.transformations.internal.Utils;
18 |
19 | /**
20 | * Copyright (C) 2020 Wasabeef
21 | *
22 | * Licensed under the Apache License, Version 2.0 (the "License");
23 | * you may not use this file except in compliance with the License.
24 | * You may obtain a copy of the License at
25 | *
26 | * http://www.apache.org/licenses/LICENSE-2.0
27 | *
28 | * Unless required by applicable law or agreed to in writing, software
29 | * distributed under the License is distributed on an "AS IS" BASIS,
30 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 | * See the License for the specific language governing permissions and
32 | * limitations under the License.
33 | */
34 |
35 | public class CropCircleWithBorderTransformation extends BitmapTransformation {
36 |
37 |
38 | private static final int VERSION = 1;
39 | private static final String ID = "jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation." + VERSION;
40 |
41 | private final int borderSize;
42 | private final int borderColor;
43 |
44 |
45 | public CropCircleWithBorderTransformation() {
46 | this.borderSize = Utils.toDp(4);
47 | this.borderColor = Color.BLACK;
48 | }
49 |
50 | public CropCircleWithBorderTransformation(int borderSize, @ColorInt int borderColor) {
51 | this.borderSize = borderSize;
52 | this.borderColor = borderColor;
53 | }
54 |
55 | @Override
56 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
57 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
58 |
59 | Bitmap bitmap = TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);
60 |
61 | setCanvasBitmapDensity(toTransform, bitmap);
62 |
63 | Paint paint = new Paint();
64 | paint.setColor(borderColor);
65 | paint.setStyle(Paint.Style.STROKE);
66 | paint.setStrokeWidth(borderSize);
67 | paint.setAntiAlias(true);
68 |
69 | Canvas canvas = new Canvas(bitmap);
70 | canvas.drawCircle(
71 | outWidth / 2f,
72 | outHeight / 2f,
73 | Math.max(outWidth, outHeight) / 2f - borderSize / 2f,
74 | paint
75 | );
76 |
77 | return bitmap;
78 | }
79 |
80 | @Override
81 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
82 | messageDigest.update((ID + borderSize + borderColor).getBytes(CHARSET));
83 | }
84 |
85 | @Override
86 | public boolean equals(Object o) {
87 | return o instanceof CropCircleWithBorderTransformation &&
88 | ((CropCircleWithBorderTransformation) o).borderSize == borderSize &&
89 | ((CropCircleWithBorderTransformation) o).borderColor == borderColor;
90 | }
91 |
92 | @Override
93 | public int hashCode() {
94 | return ID.hashCode() + borderSize * 100 + borderColor + 10;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/CropSquareTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 |
22 | import androidx.annotation.NonNull;
23 |
24 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
25 | import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
26 |
27 | import java.security.MessageDigest;
28 |
29 | public class CropSquareTransformation extends BitmapTransformation {
30 |
31 | private static final int VERSION = 1;
32 | private static final String ID =
33 | "jp.wasabeef.glide.transformations.CropSquareTransformation." + VERSION;
34 |
35 | private int size;
36 |
37 | @Override
38 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
39 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
40 | this.size = Math.max(outWidth, outHeight);
41 | return TransformationUtils.centerCrop(pool, toTransform, size, size);
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "CropSquareTransformation(size=" + size + ")";
47 | }
48 |
49 | @Override
50 | public boolean equals(Object o) {
51 | return o instanceof CropSquareTransformation && ((CropSquareTransformation) o).size == size;
52 | }
53 |
54 | @Override
55 | public int hashCode() {
56 | return ID.hashCode() + size * 10;
57 | }
58 |
59 | @Override
60 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
61 | messageDigest.update((ID + size).getBytes(CHARSET));
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/CropTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.Canvas;
22 | import android.graphics.RectF;
23 |
24 | import androidx.annotation.NonNull;
25 |
26 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
27 |
28 | import java.security.MessageDigest;
29 |
30 | public class CropTransformation extends BitmapTransformation {
31 |
32 | private static final int VERSION = 1;
33 | private static final String ID = "jp.wasabeef.glide.transformations.CropTransformation." + VERSION;
34 |
35 | public enum CropType {
36 | TOP,
37 | CENTER,
38 | BOTTOM
39 | }
40 |
41 | private int width;
42 | private int height;
43 |
44 | private CropType cropType = CropType.CENTER;
45 |
46 | public CropTransformation(int width, int height) {
47 | this(width, height, CropType.CENTER);
48 | }
49 |
50 | public CropTransformation(int width, int height, CropType cropType) {
51 | this.width = width;
52 | this.height = height;
53 | this.cropType = cropType;
54 | }
55 |
56 | @Override
57 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
58 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
59 |
60 | width = width == 0 ? toTransform.getWidth() : width;
61 | height = height == 0 ? toTransform.getHeight() : height;
62 |
63 | Bitmap.Config config =
64 | toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
65 | Bitmap bitmap = pool.get(width, height, config);
66 |
67 | bitmap.setHasAlpha(true);
68 |
69 | float scaleX = (float) width / toTransform.getWidth();
70 | float scaleY = (float) height / toTransform.getHeight();
71 | float scale = Math.max(scaleX, scaleY);
72 |
73 | float scaledWidth = scale * toTransform.getWidth();
74 | float scaledHeight = scale * toTransform.getHeight();
75 | float left = (width - scaledWidth) / 2;
76 | float top = getTop(scaledHeight);
77 | RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
78 |
79 | setCanvasBitmapDensity(toTransform, bitmap);
80 |
81 | Canvas canvas = new Canvas(bitmap);
82 | canvas.drawBitmap(toTransform, null, targetRect, null);
83 |
84 | return bitmap;
85 | }
86 |
87 | private float getTop(float scaledHeight) {
88 | switch (cropType) {
89 | case TOP:
90 | return 0;
91 | case CENTER:
92 | return (height - scaledHeight) / 2;
93 | case BOTTOM:
94 | return height - scaledHeight;
95 | default:
96 | return 0;
97 | }
98 | }
99 |
100 | @Override
101 | public String toString() {
102 | return "CropTransformation(width=" + width + ", height=" + height + ", cropType=" + cropType + ")";
103 | }
104 |
105 | @Override
106 | public boolean equals(Object o) {
107 | return o instanceof CropTransformation &&
108 | ((CropTransformation) o).width == width &&
109 | ((CropTransformation) o).height == height &&
110 | ((CropTransformation) o).cropType == cropType;
111 | }
112 |
113 | @Override
114 | public int hashCode() {
115 | return ID.hashCode() + width * 100000 + height * 1000 + cropType.ordinal() * 10;
116 | }
117 |
118 | @Override
119 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
120 | messageDigest.update((ID + width + height + cropType).getBytes(CHARSET));
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/GrayscaleTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.Canvas;
22 | import android.graphics.ColorMatrix;
23 | import android.graphics.ColorMatrixColorFilter;
24 | import android.graphics.Paint;
25 |
26 | import androidx.annotation.NonNull;
27 |
28 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
29 |
30 | import java.security.MessageDigest;
31 |
32 | public class GrayscaleTransformation extends BitmapTransformation {
33 |
34 | private static final int VERSION = 1;
35 | private static final String ID =
36 | "jp.wasabeef.glide.transformations.GrayscaleTransformation." + VERSION;
37 |
38 | @Override
39 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
40 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
41 | int width = toTransform.getWidth();
42 | int height = toTransform.getHeight();
43 |
44 | Bitmap.Config config =
45 | toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
46 | Bitmap bitmap = pool.get(width, height, config);
47 |
48 | setCanvasBitmapDensity(toTransform, bitmap);
49 |
50 | Canvas canvas = new Canvas(bitmap);
51 | ColorMatrix saturation = new ColorMatrix();
52 | saturation.setSaturation(0f);
53 | Paint paint = new Paint();
54 | paint.setColorFilter(new ColorMatrixColorFilter(saturation));
55 | canvas.drawBitmap(toTransform, 0, 0, paint);
56 |
57 | return bitmap;
58 | }
59 |
60 | @Override
61 | public String toString() {
62 | return "GrayscaleTransformation()";
63 | }
64 |
65 | @Override
66 | public boolean equals(Object o) {
67 | return o instanceof GrayscaleTransformation;
68 | }
69 |
70 | @Override
71 | public int hashCode() {
72 | return ID.hashCode();
73 | }
74 |
75 | @Override
76 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
77 | messageDigest.update((ID).getBytes(CHARSET));
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.Canvas;
22 | import android.graphics.Paint;
23 | import android.graphics.PorterDuff;
24 | import android.graphics.PorterDuffXfermode;
25 | import android.graphics.drawable.Drawable;
26 |
27 | import androidx.annotation.NonNull;
28 |
29 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
30 |
31 | import java.security.MessageDigest;
32 |
33 | public class MaskTransformation extends BitmapTransformation {
34 |
35 | private static final int VERSION = 1;
36 | private static final String ID =
37 | "jp.wasabeef.glide.transformations.MaskTransformation." + VERSION;
38 |
39 | private static final Paint paint = new Paint();
40 | private final int maskId;
41 |
42 | static {
43 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
44 | }
45 |
46 | /**
47 | * @param maskId If you change the mask file, please also rename the mask file, or Glide will get
48 | * the cache with the old mask. Because key() return the same values if using the
49 | * same make file name. If you have a good idea please tell us, thanks.
50 | */
51 | public MaskTransformation(int maskId) {
52 | this.maskId = maskId;
53 | }
54 |
55 | @Override
56 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
57 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
58 | int width = toTransform.getWidth();
59 | int height = toTransform.getHeight();
60 |
61 | Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
62 | bitmap.setHasAlpha(true);
63 |
64 | Drawable mask = context.getDrawable(maskId);
65 |
66 | setCanvasBitmapDensity(toTransform, bitmap);
67 |
68 | Canvas canvas = new Canvas(bitmap);
69 | mask.setBounds(0, 0, width, height);
70 | mask.draw(canvas);
71 | canvas.drawBitmap(toTransform, 0, 0, paint);
72 |
73 | return bitmap;
74 | }
75 |
76 | @Override
77 | public String toString() {
78 | return "MaskTransformation(maskId=" + maskId + ")";
79 | }
80 |
81 | @Override
82 | public boolean equals(Object o) {
83 | return o instanceof MaskTransformation &&
84 | ((MaskTransformation) o).maskId == maskId;
85 | }
86 |
87 | @Override
88 | public int hashCode() {
89 | return ID.hashCode() + maskId * 10;
90 | }
91 |
92 | @Override
93 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
94 | messageDigest.update((ID + maskId).getBytes(CHARSET));
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/RoundedCornersTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 | import android.graphics.BitmapShader;
22 | import android.graphics.Canvas;
23 | import android.graphics.Paint;
24 | import android.graphics.RectF;
25 | import android.graphics.Shader;
26 |
27 | import androidx.annotation.NonNull;
28 |
29 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
30 |
31 | import java.security.MessageDigest;
32 |
33 | public class RoundedCornersTransformation extends BitmapTransformation {
34 |
35 | private static final int VERSION = 1;
36 | private static final String ID = "jp.wasabeef.glide.transformations.RoundedCornersTransformation." + VERSION;
37 |
38 | public enum CornerType {
39 | ALL,
40 | TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
41 | TOP, BOTTOM, LEFT, RIGHT,
42 | OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
43 | DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
44 | }
45 |
46 | private final int radius;
47 | private final int diameter;
48 | private final int margin;
49 | private final CornerType cornerType;
50 |
51 | public RoundedCornersTransformation(int radius, int margin) {
52 | this(radius, margin, CornerType.ALL);
53 | }
54 |
55 | public RoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
56 | this.radius = radius;
57 | this.diameter = this.radius * 2;
58 | this.margin = margin;
59 | this.cornerType = cornerType;
60 | }
61 |
62 | @Override
63 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
64 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
65 | int width = toTransform.getWidth();
66 | int height = toTransform.getHeight();
67 |
68 | Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
69 | bitmap.setHasAlpha(true);
70 |
71 | setCanvasBitmapDensity(toTransform, bitmap);
72 |
73 | Canvas canvas = new Canvas(bitmap);
74 | Paint paint = new Paint();
75 | paint.setAntiAlias(true);
76 | paint.setShader(new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
77 | drawRoundRect(canvas, paint, width, height);
78 | return bitmap;
79 | }
80 |
81 | private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
82 | float right = width - margin;
83 | float bottom = height - margin;
84 |
85 | switch (cornerType) {
86 | case ALL:
87 | canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
88 | break;
89 | case TOP_LEFT:
90 | drawTopLeftRoundRect(canvas, paint, right, bottom);
91 | break;
92 | case TOP_RIGHT:
93 | drawTopRightRoundRect(canvas, paint, right, bottom);
94 | break;
95 | case BOTTOM_LEFT:
96 | drawBottomLeftRoundRect(canvas, paint, right, bottom);
97 | break;
98 | case BOTTOM_RIGHT:
99 | drawBottomRightRoundRect(canvas, paint, right, bottom);
100 | break;
101 | case TOP:
102 | drawTopRoundRect(canvas, paint, right, bottom);
103 | break;
104 | case BOTTOM:
105 | drawBottomRoundRect(canvas, paint, right, bottom);
106 | break;
107 | case LEFT:
108 | drawLeftRoundRect(canvas, paint, right, bottom);
109 | break;
110 | case RIGHT:
111 | drawRightRoundRect(canvas, paint, right, bottom);
112 | break;
113 | case OTHER_TOP_LEFT:
114 | drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
115 | break;
116 | case OTHER_TOP_RIGHT:
117 | drawOtherTopRightRoundRect(canvas, paint, right, bottom);
118 | break;
119 | case OTHER_BOTTOM_LEFT:
120 | drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
121 | break;
122 | case OTHER_BOTTOM_RIGHT:
123 | drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
124 | break;
125 | case DIAGONAL_FROM_TOP_LEFT:
126 | drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
127 | break;
128 | case DIAGONAL_FROM_TOP_RIGHT:
129 | drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
130 | break;
131 | default:
132 | canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
133 | break;
134 | }
135 | }
136 |
137 | private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
138 | canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
139 | radius, paint);
140 | canvas.drawRect(new RectF(margin, margin + radius, margin + radius, bottom), paint);
141 | canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
142 | }
143 |
144 | private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
145 | canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
146 | radius, paint);
147 | canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
148 | canvas.drawRect(new RectF(right - radius, margin + radius, right, bottom), paint);
149 | }
150 |
151 | private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
152 | canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
153 | radius, paint);
154 | canvas.drawRect(new RectF(margin, margin, margin + diameter, bottom - radius), paint);
155 | canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
156 | }
157 |
158 | private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
159 | canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
160 | radius, paint);
161 | canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
162 | canvas.drawRect(new RectF(right - radius, margin, right, bottom - radius), paint);
163 | }
164 |
165 | private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
166 | canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
167 | paint);
168 | canvas.drawRect(new RectF(margin, margin + radius, right, bottom), paint);
169 | }
170 |
171 | private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
172 | canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
173 | paint);
174 | canvas.drawRect(new RectF(margin, margin, right, bottom - radius), paint);
175 | }
176 |
177 | private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
178 | canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
179 | paint);
180 | canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
181 | }
182 |
183 | private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
184 | canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
185 | canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
186 | }
187 |
188 | private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
189 | canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
190 | paint);
191 | canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
192 | canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
193 | }
194 |
195 | private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
196 | canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
197 | paint);
198 | canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
199 | paint);
200 | canvas.drawRect(new RectF(margin + radius, margin, right, bottom - radius), paint);
201 | }
202 |
203 | private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
204 | canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
205 | paint);
206 | canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
207 | canvas.drawRect(new RectF(margin, margin + radius, right - radius, bottom), paint);
208 | }
209 |
210 | private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
211 | float bottom) {
212 | canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
213 | paint);
214 | canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
215 | paint);
216 | canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
217 | }
218 |
219 | private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
220 | float bottom) {
221 | canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
222 | radius, paint);
223 | canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
224 | radius, paint);
225 | canvas.drawRect(new RectF(margin, margin + radius, right - radius, bottom), paint);
226 | canvas.drawRect(new RectF(margin + radius, margin, right, bottom - radius), paint);
227 | }
228 |
229 | private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
230 | float bottom) {
231 | canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
232 | radius, paint);
233 | canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
234 | radius, paint);
235 | canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
236 | canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
237 | }
238 |
239 | @Override
240 | public String toString() {
241 | return "RoundedTransformation(radius=" + radius + ", margin=" + margin + ", diameter="
242 | + diameter + ", cornerType=" + cornerType.name() + ")";
243 | }
244 |
245 | @Override
246 | public boolean equals(Object o) {
247 | return o instanceof RoundedCornersTransformation &&
248 | ((RoundedCornersTransformation) o).radius == radius &&
249 | ((RoundedCornersTransformation) o).diameter == diameter &&
250 | ((RoundedCornersTransformation) o).margin == margin &&
251 | ((RoundedCornersTransformation) o).cornerType == cornerType;
252 | }
253 |
254 | @Override
255 | public int hashCode() {
256 | return ID.hashCode() + radius * 10000 + diameter * 1000 + margin * 100 + cornerType.ordinal() * 10;
257 | }
258 |
259 | @Override
260 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
261 | messageDigest.update((ID + radius + diameter + margin + cornerType).getBytes(CHARSET));
262 | }
263 | }
264 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/BrightnessFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageBrightnessFilter;
24 |
25 | /**
26 | * brightness value ranges from -1.0 to 1.0, with 0.0 as the normal level
27 | */
28 | public class BrightnessFilterTransformation extends GPUFilterTransformation {
29 |
30 | private static final int VERSION = 1;
31 | private static final String ID =
32 | "jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation." + VERSION;
33 |
34 | private final float brightness;
35 |
36 | public BrightnessFilterTransformation() {
37 | this(0.0f);
38 | }
39 |
40 | public BrightnessFilterTransformation(float brightness) {
41 | super(new GPUImageBrightnessFilter());
42 | this.brightness = brightness;
43 | GPUImageBrightnessFilter filter = getFilter();
44 | filter.setBrightness(this.brightness);
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "BrightnessFilterTransformation(brightness=" + brightness + ")";
50 | }
51 |
52 | @Override
53 | public boolean equals(Object o) {
54 | return o instanceof BrightnessFilterTransformation &&
55 | ((BrightnessFilterTransformation) o).brightness == brightness;
56 | }
57 |
58 | @Override
59 | public int hashCode() {
60 | return ID.hashCode() + (int) ((brightness + 1.0f) * 10);
61 | }
62 |
63 | @Override
64 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
65 | messageDigest.update((ID + brightness).getBytes(CHARSET));
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/ContrastFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageContrastFilter;
24 |
25 | /**
26 | * contrast value ranges from 0.0 to 4.0, with 1.0 as the normal level
27 | */
28 | public class ContrastFilterTransformation extends GPUFilterTransformation {
29 |
30 | private static final int VERSION = 1;
31 | private static final String ID =
32 | "jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation." + VERSION;
33 |
34 | private final float contrast;
35 |
36 | public ContrastFilterTransformation() {
37 | this(1.0f);
38 | }
39 |
40 | public ContrastFilterTransformation(float contrast) {
41 | super(new GPUImageContrastFilter());
42 | this.contrast = contrast;
43 | GPUImageContrastFilter filter = getFilter();
44 | filter.setContrast(this.contrast);
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "ContrastFilterTransformation(contrast=" + contrast + ")";
50 | }
51 |
52 | @Override
53 | public boolean equals(Object o) {
54 | return o instanceof ContrastFilterTransformation;
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | return ID.hashCode() + (int) (contrast * 10);
60 | }
61 |
62 | @Override
63 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
64 | messageDigest.update((ID + contrast).getBytes(CHARSET));
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/GPUFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.content.Context;
20 | import android.graphics.Bitmap;
21 |
22 | import androidx.annotation.NonNull;
23 |
24 | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
25 |
26 | import java.security.MessageDigest;
27 |
28 | import jp.co.cyberagent.android.gpuimage.GPUImage;
29 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
30 | import jp.wasabeef.glide.transformations.BitmapTransformation;
31 |
32 | public class GPUFilterTransformation extends BitmapTransformation {
33 |
34 | private static final int VERSION = 1;
35 | private static final String ID =
36 | "jp.wasabeef.glide.transformations.gpu.GPUFilterTransformation." + VERSION;
37 | private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
38 |
39 | private final GPUImageFilter gpuImageFilter;
40 |
41 | public GPUFilterTransformation(GPUImageFilter filter) {
42 | this.gpuImageFilter = filter;
43 | }
44 |
45 | @Override
46 | protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
47 | @NonNull Bitmap toTransform, int outWidth, int outHeight) {
48 | GPUImage gpuImage = new GPUImage(context);
49 | gpuImage.setImage(toTransform);
50 | gpuImage.setFilter(gpuImageFilter);
51 |
52 | return gpuImage.getBitmapWithFilterApplied();
53 | }
54 |
55 | @Override
56 | public String toString() {
57 | return getClass().getSimpleName();
58 | }
59 |
60 | @SuppressWarnings("unchecked")
61 | public
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorInvertFilter;
24 |
25 | /**
26 | * Invert all the colors in the image.
27 | */
28 | public class InvertFilterTransformation extends GPUFilterTransformation {
29 |
30 | private static final int VERSION = 1;
31 | private static final String ID =
32 | "jp.wasabeef.glide.transformations.gpu.InvertFilterTransformation." + VERSION;
33 |
34 | public InvertFilterTransformation() {
35 | super(new GPUImageColorInvertFilter());
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return "InvertFilterTransformation()";
41 | }
42 |
43 | @Override
44 | public boolean equals(Object o) {
45 | return o instanceof InvertFilterTransformation;
46 | }
47 |
48 | @Override
49 | public int hashCode() {
50 | return ID.hashCode();
51 | }
52 |
53 | @Override
54 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
55 | messageDigest.update((ID).getBytes(CHARSET));
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageKuwaharaFilter;
24 |
25 | /**
26 | * Kuwahara all the colors in the image.
27 | *
28 | * The radius to sample from when creating the brush-stroke effect, with a default of 25.
29 | * The larger the radius, the slower the filter.
30 | */
31 | public class KuwaharaFilterTransformation extends GPUFilterTransformation {
32 |
33 | private static final int VERSION = 1;
34 | private static final String ID =
35 | "jp.wasabeef.glide.transformations.gpu.KuwaharaFilterTransformation." + VERSION;
36 |
37 | private final int radius;
38 |
39 | public KuwaharaFilterTransformation() {
40 | this(25);
41 | }
42 |
43 | public KuwaharaFilterTransformation(int radius) {
44 | super(new GPUImageKuwaharaFilter());
45 | this.radius = radius;
46 | GPUImageKuwaharaFilter filter = getFilter();
47 | filter.setRadius(this.radius);
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "KuwaharaFilterTransformation(radius=" + radius + ")";
53 | }
54 |
55 | @Override
56 | public boolean equals(Object o) {
57 | return o instanceof KuwaharaFilterTransformation;
58 | }
59 |
60 | @Override
61 | public int hashCode() {
62 | return ID.hashCode() + radius * 10;
63 | }
64 |
65 | @Override
66 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
67 | messageDigest.update((ID + radius).getBytes(CHARSET));
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/PixelationFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImagePixelationFilter;
24 |
25 | /**
26 | * Applies a Pixelation effect to the image.
27 | *
28 | * The pixel with a default of 10.0.
29 | */
30 | public class PixelationFilterTransformation extends GPUFilterTransformation {
31 |
32 | private static final int VERSION = 1;
33 | private static final String ID =
34 | "jp.wasabeef.glide.transformations.gpu.PixelationFilterTransformation." + VERSION;
35 |
36 | private final float pixel;
37 |
38 | public PixelationFilterTransformation() {
39 | this(10f);
40 | }
41 |
42 | public PixelationFilterTransformation(float pixel) {
43 | super(new GPUImagePixelationFilter());
44 | this.pixel = pixel;
45 | GPUImagePixelationFilter filter = getFilter();
46 | filter.setPixel(this.pixel);
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "PixelationFilterTransformation(pixel=" + pixel + ")";
52 | }
53 |
54 | @Override
55 | public boolean equals(Object o) {
56 | return o instanceof PixelationFilterTransformation;
57 | }
58 |
59 | @Override
60 | public int hashCode() {
61 | return ID.hashCode() + (int) (pixel * 10);
62 | }
63 |
64 | @Override
65 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
66 | messageDigest.update((ID + pixel).getBytes(CHARSET));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SepiaFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageSepiaToneFilter;
24 |
25 | /**
26 | * Applies a simple sepia effect.
27 | *
28 | * The intensity with a default of 1.0.
29 | */
30 | public class SepiaFilterTransformation extends GPUFilterTransformation {
31 |
32 | private static final int VERSION = 1;
33 | private static final String ID =
34 | "jp.wasabeef.glide.transformations.gpu.SepiaFilterTransformation." + VERSION;
35 |
36 | private final float intensity;
37 |
38 | public SepiaFilterTransformation() {
39 | this(1.0f);
40 | }
41 |
42 | public SepiaFilterTransformation(float intensity) {
43 | super(new GPUImageSepiaToneFilter());
44 | this.intensity = intensity;
45 | GPUImageSepiaToneFilter filter = getFilter();
46 | filter.setIntensity(this.intensity);
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "SepiaFilterTransformation(intensity=" + intensity + ")";
52 | }
53 |
54 | @Override
55 | public boolean equals(Object o) {
56 | return o instanceof SepiaFilterTransformation;
57 | }
58 |
59 | @Override
60 | public int hashCode() {
61 | return ID.hashCode() + (int) (intensity * 10);
62 | }
63 |
64 | @Override
65 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
66 | messageDigest.update((ID + intensity).getBytes(CHARSET));
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SketchFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageSketchFilter;
24 |
25 | public class SketchFilterTransformation extends GPUFilterTransformation {
26 |
27 | private static final int VERSION = 1;
28 | private static final String ID =
29 | "jp.wasabeef.glide.transformations.gpu.SketchFilterTransformation." + VERSION;
30 |
31 | public SketchFilterTransformation() {
32 | super(new GPUImageSketchFilter());
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "SketchFilterTransformation()";
38 | }
39 |
40 | @Override
41 | public boolean equals(Object o) {
42 | return o instanceof SketchFilterTransformation;
43 | }
44 |
45 | @Override
46 | public int hashCode() {
47 | return ID.hashCode();
48 | }
49 |
50 | @Override
51 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
52 | messageDigest.update((ID).getBytes(CHARSET));
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SwirlFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.graphics.PointF;
20 |
21 | import androidx.annotation.NonNull;
22 |
23 | import java.security.MessageDigest;
24 |
25 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageSwirlFilter;
26 |
27 | /**
28 | * Creates a swirl distortion on the image.
29 | */
30 | public class SwirlFilterTransformation extends GPUFilterTransformation {
31 |
32 | private static final int VERSION = 1;
33 | private static final String ID =
34 | "jp.wasabeef.glide.transformations.gpu.SwirlFilterTransformation." + VERSION;
35 |
36 | private final float radius;
37 | private final float angle;
38 | private final PointF center;
39 |
40 | public SwirlFilterTransformation() {
41 | this(.5f, 1.0f, new PointF(0.5f, 0.5f));
42 | }
43 |
44 | /**
45 | * @param radius from 0.0 to 1.0, default 0.5
46 | * @param angle minimum 0.0, default 1.0
47 | * @param center default (0.5, 0.5)
48 | */
49 | public SwirlFilterTransformation(float radius, float angle, PointF center) {
50 | super(new GPUImageSwirlFilter());
51 | this.radius = radius;
52 | this.angle = angle;
53 | this.center = center;
54 | GPUImageSwirlFilter filter = getFilter();
55 | filter.setRadius(this.radius);
56 | filter.setAngle(this.angle);
57 | filter.setCenter(this.center);
58 | }
59 |
60 | @Override
61 | public String toString() {
62 | return "SwirlFilterTransformation(radius=" + radius + ",angle=" + angle + ",center="
63 | + center.toString() + ")";
64 | }
65 |
66 | @Override
67 | public boolean equals(Object o) {
68 | return o instanceof SwirlFilterTransformation &&
69 | ((SwirlFilterTransformation) o).radius == radius &&
70 | ((SwirlFilterTransformation) o).angle == radius &&
71 | ((SwirlFilterTransformation) o).center.equals(center.x, center.y);
72 | }
73 |
74 | @Override
75 | public int hashCode() {
76 | return ID.hashCode() + (int) (radius * 1000) + (int) (angle * 10) + center.hashCode();
77 | }
78 |
79 | @Override
80 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
81 | messageDigest.update((ID + radius + angle + center.hashCode()).getBytes(CHARSET));
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/ToonFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.security.MessageDigest;
22 |
23 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageToonFilter;
24 |
25 | /**
26 | * The threshold at which to apply the edges, default of 0.2.
27 | * The levels of quantization for the posterization of colors within the scene,
28 | * with a default of 10.0.
29 | */
30 | public class ToonFilterTransformation extends GPUFilterTransformation {
31 |
32 | private static final int VERSION = 1;
33 | private static final String ID =
34 | "jp.wasabeef.glide.transformations.gpu.ToonFilterTransformation." + VERSION;
35 |
36 | private final float threshold;
37 | private final float quantizationLevels;
38 |
39 | public ToonFilterTransformation() {
40 | this(.2f, 10.0f);
41 | }
42 |
43 | public ToonFilterTransformation(float threshold, float quantizationLevels) {
44 | super(new GPUImageToonFilter());
45 | this.threshold = threshold;
46 | this.quantizationLevels = quantizationLevels;
47 | GPUImageToonFilter filter = getFilter();
48 | filter.setThreshold(this.threshold);
49 | filter.setQuantizationLevels(this.quantizationLevels);
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return "ToonFilterTransformation(threshold=" + threshold + ",quantizationLevels="
55 | + quantizationLevels + ")";
56 | }
57 |
58 | @Override
59 | public boolean equals(Object o) {
60 | return o instanceof ToonFilterTransformation &&
61 | ((ToonFilterTransformation) o).threshold == threshold &&
62 | ((ToonFilterTransformation) o).quantizationLevels == quantizationLevels;
63 | }
64 |
65 | @Override
66 | public int hashCode() {
67 | return ID.hashCode() + (int) (threshold * 1000) + (int) (quantizationLevels * 10);
68 | }
69 |
70 | @Override
71 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
72 | messageDigest.update((ID + threshold + quantizationLevels).getBytes(CHARSET));
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/VignetteFilterTransformation.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.gpu;
2 |
3 | /**
4 | * Copyright (C) 2020 Wasabeef
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License.
17 | */
18 |
19 | import android.graphics.PointF;
20 |
21 | import androidx.annotation.NonNull;
22 |
23 | import java.security.MessageDigest;
24 | import java.util.Arrays;
25 |
26 | import jp.co.cyberagent.android.gpuimage.filter.GPUImageVignetteFilter;
27 |
28 | /**
29 | * Performs a vignetting effect, fading out the image at the edges
30 | * The directional intensity of the vignetting,
31 | * with a default of x = 0.5, y = 0.5, start = 0, end = 0.75
32 | */
33 | public class VignetteFilterTransformation extends GPUFilterTransformation {
34 |
35 | private static final int VERSION = 1;
36 | private static final String ID =
37 | "jp.wasabeef.glide.transformations.gpu.VignetteFilterTransformation." + VERSION;
38 |
39 | private final PointF center;
40 | private final float[] vignetteColor;
41 | private final float vignetteStart;
42 | private final float vignetteEnd;
43 |
44 | public VignetteFilterTransformation() {
45 | this(new PointF(0.5f, 0.5f), new float[]{0.0f, 0.0f, 0.0f}, 0.0f, 0.75f);
46 | }
47 |
48 | public VignetteFilterTransformation(PointF center, float[] color, float start, float end) {
49 | super(new GPUImageVignetteFilter());
50 | this.center = center;
51 | vignetteColor = color;
52 | vignetteStart = start;
53 | vignetteEnd = end;
54 | GPUImageVignetteFilter filter = getFilter();
55 | filter.setVignetteCenter(this.center);
56 | filter.setVignetteColor(vignetteColor);
57 | filter.setVignetteStart(vignetteStart);
58 | filter.setVignetteEnd(vignetteEnd);
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "VignetteFilterTransformation(center=" + center.toString() + ",color=" + Arrays.toString(
64 | vignetteColor) + ",start=" + vignetteStart + ",end=" + vignetteEnd + ")";
65 | }
66 |
67 | @Override
68 | public boolean equals(Object o) {
69 | return o instanceof VignetteFilterTransformation &&
70 | ((VignetteFilterTransformation) o).center.equals(center.x, center.y) &&
71 | Arrays.equals(((VignetteFilterTransformation) o).vignetteColor, vignetteColor) &&
72 | ((VignetteFilterTransformation) o).vignetteStart == vignetteStart &&
73 | ((VignetteFilterTransformation) o).vignetteEnd == vignetteEnd;
74 | }
75 |
76 | @Override
77 | public int hashCode() {
78 | return ID.hashCode() + center.hashCode() + Arrays.hashCode(vignetteColor) +
79 | (int) (vignetteStart * 100) + (int) (vignetteEnd * 10);
80 | }
81 |
82 | @Override
83 | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
84 | messageDigest.update((ID + center + Arrays.hashCode(vignetteColor) + vignetteStart + vignetteEnd).getBytes(CHARSET));
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/FastBlur.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.internal;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Copyright (C) 2020 Wasabeef
7 | *
8 | * Licensed under the Apache License, Version 2.0 (the "License");
9 | * you may not use this file except in compliance with the License.
10 | * You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing, software
15 | * distributed under the License is distributed on an "AS IS" BASIS,
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 | * See the License for the specific language governing permissions and
18 | * limitations under the License.
19 | */
20 |
21 | public class FastBlur {
22 |
23 | public static Bitmap blur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
24 |
25 | // Stack Blur v1.0 from
26 | // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
27 | //
28 | // Java Author: Mario Klingemann
16 | * Licensed under the Apache License, Version 2.0 (the "License");
17 | * you may not use this file except in compliance with the License.
18 | * You may obtain a copy of the License at
19 | *
20 | * http://www.apache.org/licenses/LICENSE-2.0
21 | *
22 | * Unless required by applicable law or agreed to in writing, software
23 | * distributed under the License is distributed on an "AS IS" BASIS,
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 | * See the License for the specific language governing permissions and
26 | * limitations under the License.
27 | */
28 |
29 | public class RSBlur {
30 |
31 | public static Bitmap blur(Context context, Bitmap bitmap, int radius) throws RSRuntimeException {
32 | RenderScript rs = null;
33 | Allocation input = null;
34 | Allocation output = null;
35 | ScriptIntrinsicBlur blur = null;
36 | try {
37 | rs = RenderScript.create(context);
38 | rs.setMessageHandler(new RenderScript.RSMessageHandler());
39 | input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
40 | Allocation.USAGE_SCRIPT);
41 | output = Allocation.createTyped(rs, input.getType());
42 | blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
43 |
44 | blur.setInput(input);
45 | blur.setRadius(radius);
46 | blur.forEach(output);
47 | output.copyTo(bitmap);
48 | } finally {
49 | if (rs != null) {
50 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
51 | RenderScript.releaseAllContexts();
52 | } else {
53 | rs.destroy();
54 | }
55 | }
56 | if (input != null) {
57 | input.destroy();
58 | }
59 | if (output != null) {
60 | output.destroy();
61 | }
62 | if (blur != null) {
63 | blur.destroy();
64 | }
65 | }
66 |
67 | return bitmap;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Utils.java:
--------------------------------------------------------------------------------
1 | package jp.wasabeef.glide.transformations.internal;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.graphics.drawable.Drawable;
6 | import android.os.Build;
7 |
8 | /**
9 | * Copyright (C) 2020 Wasabeef
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License");
12 | * you may not use this file except in compliance with the License.
13 | * You may obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | */
23 |
24 | public final class Utils {
25 |
26 | private Utils() {
27 | // Utility class.
28 | }
29 |
30 | public static int toDp(int px) {
31 | return px * (int) Resources.getSystem().getDisplayMetrics().density;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------