3
3
import android.graphics.Bitmap;
4
4
import android.graphics.Canvas;
5
5
import android.graphics.ColorMatrixColorFilter;
6
import android.graphics.LinearGradient;
7
import android.graphics.Matrix;
6
8
import android.graphics.Paint;
7
9
import android.graphics.PorterDuff;
8
10
import android.graphics.PorterDuffXfermode;
9
11
import android.graphics.Rect;
12
import android.graphics.RectF;
13
import android.graphics.Shader;
11
15
public class ImageUtils {
17
private static final float EDGE_START = 0.0f;
18
private static final float EDGE_END = 4.0f;
19
private static final int EDGE_COLOR_START = 0x7F000000;
20
private static final int EDGE_COLOR_END = 0x00000000;
21
private static final Paint EDGE_PAINT = new Paint();
23
private static final int END_EDGE_COLOR_START = 0x00000000;
24
private static final int END_EDGE_COLOR_END = 0x4F000000;
25
private static final Paint END_EDGE_PAINT = new Paint();
27
private static final float FOLD_START = 5.0f;
28
private static final float FOLD_END = 13.0f;
29
private static final int FOLD_COLOR_START = 0x00000000;
30
private static final int FOLD_COLOR_END = 0x26000000;
31
private static final Paint FOLD_PAINT = new Paint();
33
private static final float SHADOW_RADIUS = 12.0f;
34
private static final int SHADOW_COLOR = 0x99000000;
35
private static final Paint SHADOW_PAINT = new Paint();
37
private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG |
38
Paint.FILTER_BITMAP_FLAG);
40
private static volatile Matrix sScaleMatrix;
44
Shader shader = new LinearGradient(EDGE_START, 0.0f, EDGE_END, 0.0f, EDGE_COLOR_START,
45
EDGE_COLOR_END, Shader.TileMode.CLAMP);
46
EDGE_PAINT.setShader(shader);
48
shader = new LinearGradient(EDGE_START, 0.0f, EDGE_END, 0.0f, END_EDGE_COLOR_START,
49
END_EDGE_COLOR_END, Shader.TileMode.CLAMP);
50
END_EDGE_PAINT.setShader(shader);
52
shader = new LinearGradient(FOLD_START, 0.0f, FOLD_END, 0.0f, new int[] {
53
FOLD_COLOR_START, FOLD_COLOR_END, FOLD_COLOR_START
54
}, new float[] { 0.0f, 0.5f, 1.0f }, Shader.TileMode.CLAMP);
55
FOLD_PAINT.setShader(shader);
57
SHADOW_PAINT.setShadowLayer(SHADOW_RADIUS / 2.0f, 0.0f, 0.0f, SHADOW_COLOR);
58
SHADOW_PAINT.setAntiAlias(true);
59
SHADOW_PAINT.setFilterBitmap(true);
60
SHADOW_PAINT.setColor(0xFF000000);
61
SHADOW_PAINT.setStyle(Paint.Style.FILL);
64
public static Bitmap createShadow(Bitmap bitmap, int width, int height) {
65
if (bitmap == null) return null;
67
final int bitmapWidth = bitmap.getWidth();
68
final int bitmapHeight = bitmap.getHeight();
70
final float scale = Math.min((float) width / (float) bitmapWidth,
71
(float) height / (float) bitmapHeight);
73
final int scaledWidth = (int) (bitmapWidth * scale);
74
final int scaledHeight = (int) (bitmapHeight * scale);
76
return createScaledBitmap(bitmap, scaledWidth, scaledHeight,
77
SHADOW_RADIUS, false, SHADOW_PAINT);
80
private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
81
float offset, boolean clipShadow, Paint paint) {
84
synchronized (Bitmap.class) {
93
final int width = src.getWidth();
94
final int height = src.getHeight();
95
final float sx = dstWidth / (float) width;
96
final float sy = dstHeight / (float) height;
99
Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);
101
synchronized (Bitmap.class) {
109
private static Bitmap createBitmap(Bitmap source, int x, int y, int width,
110
int height, Matrix m, float offset, boolean clipShadow, Paint paint) {
112
int scaledWidth = width;
113
int scaledHeight = height;
115
final Canvas canvas = new Canvas();
116
canvas.translate(offset / 2.0f, offset / 2.0f);
120
final Rect from = new Rect(x, y, x + width, y + height);
121
final RectF to = new RectF(0, 0, width, height);
123
if (m == null || m.isIdentity()) {
124
bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
125
scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset),
126
Bitmap.Config.ARGB_8888);
129
RectF mapped = new RectF();
130
m.mapRect(mapped, to);
132
scaledWidth = Math.round(mapped.width());
133
scaledHeight = Math.round(mapped.height());
135
bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
136
scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset),
137
Bitmap.Config.ARGB_8888);
138
canvas.translate(-mapped.left, -mapped.top);
142
canvas.setBitmap(bitmap);
143
canvas.drawRect(0.0f, 0.0f, width, height, paint);
144
canvas.drawBitmap(source, from, to, SCALE_PAINT);
15
149
public static Bitmap toMask(final Bitmap source, final int color, boolean invert, float[] coefficients) {
16
150
//cache the values we know we will need