~dav-surf/cineol/android

« back to all changes in this revision

Viewing changes to src/es/leafsoft/utils/ImageUtils.java

  • Committer: dav.surf at gmail
  • Date: 2009-11-13 20:20:10 UTC
  • Revision ID: dav.surf@gmail.com-20091113202010-c8s7uh4ganj9vkru
android-trunk-130911

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
10
14
 
11
15
public class ImageUtils {
12
16
        
13
 
        
 
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();
 
22
 
 
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();
 
26
    
 
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();
 
32
    
 
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();
 
36
    
 
37
    private static final Paint SCALE_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG |
 
38
            Paint.FILTER_BITMAP_FLAG);
 
39
    
 
40
    private static volatile Matrix sScaleMatrix;
 
41
 
 
42
        
 
43
    static {
 
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);
 
47
 
 
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);
 
51
 
 
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);
 
56
 
 
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);
 
62
    }
 
63
        
 
64
    public static Bitmap createShadow(Bitmap bitmap, int width, int height) {
 
65
        if (bitmap == null) return null;
 
66
 
 
67
        final int bitmapWidth = bitmap.getWidth();
 
68
        final int bitmapHeight = bitmap.getHeight();
 
69
 
 
70
        final float scale = Math.min((float) width / (float) bitmapWidth,
 
71
                (float) height / (float) bitmapHeight);
 
72
 
 
73
        final int scaledWidth = (int) (bitmapWidth * scale);
 
74
        final int scaledHeight = (int) (bitmapHeight * scale);
 
75
 
 
76
        return createScaledBitmap(bitmap, scaledWidth, scaledHeight,
 
77
                SHADOW_RADIUS, false, SHADOW_PAINT);
 
78
    }
 
79
    
 
80
    private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
 
81
            float offset, boolean clipShadow, Paint paint) {
 
82
        
 
83
        Matrix m;
 
84
        synchronized (Bitmap.class) {
 
85
            m = sScaleMatrix;
 
86
            sScaleMatrix = null;
 
87
        }
 
88
 
 
89
        if (m == null) {
 
90
            m = new Matrix();
 
91
        }
 
92
 
 
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;
 
97
        m.setScale(sx, sy);
 
98
 
 
99
        Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);
 
100
 
 
101
        synchronized (Bitmap.class) {
 
102
            sScaleMatrix = m;
 
103
        }
 
104
 
 
105
        return b;
 
106
    }
 
107
    
 
108
    
 
109
    private static Bitmap createBitmap(Bitmap source, int x, int y, int width,
 
110
            int height, Matrix m, float offset, boolean clipShadow, Paint paint) {
 
111
 
 
112
        int scaledWidth = width;
 
113
        int scaledHeight = height;
 
114
 
 
115
        final Canvas canvas = new Canvas();
 
116
        canvas.translate(offset / 2.0f, offset / 2.0f);
 
117
 
 
118
        Bitmap bitmap;
 
119
 
 
120
        final Rect from = new Rect(x, y, x + width, y + height);
 
121
        final RectF to = new RectF(0, 0, width, height);
 
122
 
 
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);
 
127
            paint = null;
 
128
        } else {
 
129
            RectF mapped = new RectF();
 
130
            m.mapRect(mapped, to);
 
131
 
 
132
            scaledWidth = Math.round(mapped.width());
 
133
            scaledHeight = Math.round(mapped.height());
 
134
 
 
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);
 
139
            canvas.concat(m);
 
140
        }
 
141
 
 
142
        canvas.setBitmap(bitmap);
 
143
        canvas.drawRect(0.0f, 0.0f, width, height, paint);
 
144
        canvas.drawBitmap(source, from, to, SCALE_PAINT);
 
145
 
 
146
        return bitmap;
 
147
    }
14
148
        
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