summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/imageshow/ImageDraw.java
blob: 795a24781e19af3ba5d9070cc73be5d50d116f0e (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

package com.android.gallery3d.filtershow.imageshow;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.editors.EditorDraw;
import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation;
import com.android.gallery3d.filtershow.filters.ImageFilterDraw;

public class ImageDraw extends ImageShow {

    private static final String LOGTAG = "ImageDraw";
    private int mCurrentColor = Color.RED;
    final static float INITAL_STROKE_RADIUS = 40;
    private float mCurrentSize = INITAL_STROKE_RADIUS;
    private byte mType = 0;
    private FilterDrawRepresentation mFRep;
    private EditorDraw mEditorDraw;
    private long mTimeout;
    private Paint mCheckerdPaint = makeCheckedPaint();
    private Paint mShadowPaint = new Paint();
    private Paint mIconPaint = new Paint();
    private Paint mBorderPaint = new Paint();
    private Handler mHandler;
    private FilterDrawRepresentation.StrokeData mTmpStrokData =
            new FilterDrawRepresentation.StrokeData();
    private Bitmap mBitmap;
    private float mDisplayRound;
    private float mDisplayBorder;
    private int DISPLAY_TIME = 500;
    private Matrix mRotateToScreen = new Matrix();
    private Matrix mToOrig;
    private int mBorderColor;
    private int mBorderShadowSize;
    private NinePatchDrawable mShadow;

    Runnable mUpdateRunnable = new Runnable() {
        @Override
        public void run() {
           invalidate();
        }
    };


    public ImageDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        resetParameter();
        setupConstants(context);
        setupTimer();
    }

    public ImageDraw(Context context) {
        super(context);
        resetParameter();
        setupConstants(context);
        setupTimer();
    }

    private void setupConstants(Context context){
        Resources res = context.getResources();
        mDisplayRound = res.getDimensionPixelSize(R.dimen.draw_rect_round);
        mDisplayBorder = res.getDimensionPixelSize(R.dimen.draw_rect_border);
        mBorderShadowSize = res.getDimensionPixelSize(R.dimen.draw_rect_shadow);
        float edge = res.getDimensionPixelSize(R.dimen.draw_rect_border_edge);

        mBorderColor = res.getColor(R.color.draw_rect_border);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(edge);
        mShadowPaint.setStyle(Paint.Style.FILL);
        mShadowPaint.setColor(Color.BLACK);
        mShadowPaint.setShadowLayer(mBorderShadowSize,mBorderShadowSize,
                mBorderShadowSize,Color.BLACK);
        mShadow = (NinePatchDrawable) res.getDrawable(R.drawable.geometry_shadow);
    }

    public void setEditor(EditorDraw editorDraw) {
        mEditorDraw = editorDraw;
    }

    public void setFilterDrawRepresentation(FilterDrawRepresentation fr) {
        mFRep = fr;
        mTmpStrokData =
                new FilterDrawRepresentation.StrokeData();
    }

    public Drawable getIcon(Context context) {

        return null;
    }

    @Override
    public void resetParameter() {
        if (mFRep != null) {
            mFRep.clear();
        }
    }

    public void setColor(int color) {
        mCurrentColor = color;
    }

    public void setSize(int size) {
        mCurrentSize = size;
    }

    public void setStyle(byte style) {
        mType = (byte) (style % ImageFilterDraw.NUMBER_OF_STYLES);
    }

    public int getStyle() {
        return mType;
    }

    public int getSize() {
        return (int) mCurrentSize;
    }

    float[] mTmpPoint = new float[2]; // so we do not malloc
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1) {
            boolean ret = super.onTouchEvent(event);
            if (mFRep.getCurrentDrawing() != null) {
                mFRep.clearCurrentSection();
                mEditorDraw.commitLocalRepresentation();
            }
            return ret;
        }
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            if (mFRep.getCurrentDrawing() == null) {
                return super.onTouchEvent(event);
            }
        }

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            calcScreenMapping();
            mTmpPoint[0] = event.getX();
            mTmpPoint[1] = event.getY();
            mToOrig.mapPoints(mTmpPoint);
            mFRep.startNewSection( mTmpPoint[0], mTmpPoint[1]);
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {

            int historySize = event.getHistorySize();
            for (int h = 0; h < historySize; h++) {
                int p = 0;
                {
                    mTmpPoint[0] = event.getHistoricalX(p, h);
                    mTmpPoint[1] = event.getHistoricalY(p, h);
                    mToOrig.mapPoints(mTmpPoint);
                    mFRep.addPoint(mTmpPoint[0], mTmpPoint[1]);
                }
            }
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            mTmpPoint[0] = event.getX();
            mTmpPoint[1] = event.getY();
            mToOrig.mapPoints(mTmpPoint);
            mFRep.endSection(mTmpPoint[0], mTmpPoint[1]);
        }
        mEditorDraw.commitLocalRepresentation();
        invalidate();
        return true;
    }

    private void calcScreenMapping() {
        mToOrig = getScreenToImageMatrix(true);
        mToOrig.invert(mRotateToScreen);
    }

    private static Paint makeCheckedPaint(){
        int[] colors = new int[16 * 16];
        for (int i = 0; i < colors.length; i++) {
            int y = i / (16 * 8);
            int x = (i / 8) % 2;
            colors[i] = (x == y) ? 0xFF777777 : 0xFF222222;
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888);
        BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        Paint p = new Paint();
        p.setShader(bs);
        return p;
    }

    private void setupTimer() {
        mHandler = new Handler(getActivity().getMainLooper());
    }

    private void scheduleWakeup(int delay) {
        mHandler.removeCallbacks(mUpdateRunnable);
        mHandler.postDelayed(mUpdateRunnable, delay);
    }

    public Bitmap getBrush(int brushid) {
        Bitmap bitmap;
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
        bitmap = BitmapFactory.decodeResource(getActivity().getResources(), brushid, opt);
        bitmap = bitmap.extractAlpha();

        return bitmap;
    }

    public Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) {
        Matrix m = new Matrix();
        m.setScale(dstWidth / (float) src.getWidth(), dstHeight / (float) src.getHeight());
        Bitmap result = Bitmap.createBitmap(dstWidth, dstHeight, src.getConfig());
        Canvas canvas = new Canvas(result);

        Paint paint = new Paint();
        paint.setFilterBitmap(filter);
        canvas.drawBitmap(src, m, paint);

        return result;

    }

    public void displayDrawLook() {
        if (mFRep == null) {
            return;
        }
        int color = mTmpStrokData.mColor;
        byte type = mTmpStrokData.mType;
        float radius = mTmpStrokData.mRadius;
        mFRep.fillStrokeParameters(mTmpStrokData);

        if (radius != mTmpStrokData.mRadius) {
            mTimeout = DISPLAY_TIME + System.currentTimeMillis();
            scheduleWakeup(DISPLAY_TIME);
        }
    }

    public void drawLook(Canvas canvas) {
        if (mFRep == null) {
            return;
        }
        int cw = canvas.getWidth();
        int ch = canvas.getHeight();
        int centerx = cw / 2;
        int centery = ch / 2;

//        mFRep.fillStrokeParameters(mTmpStrokData);
        mIconPaint.setAntiAlias(true);
        mIconPaint.setStyle(Paint.Style.STROKE);
        float rad = mRotateToScreen.mapRadius(mTmpStrokData.mRadius);

        RectF rec = new RectF();
        rec.set(centerx - rad,
                centery - rad,
                centerx + rad,
                centery + rad);
        mIconPaint.setColor(Color.BLACK);
        mIconPaint.setStrokeWidth(5);
        canvas.drawArc(rec, 0, 360, true, mIconPaint);
        mIconPaint.setColor(Color.WHITE);
        mIconPaint.setStrokeWidth(3);
        canvas.drawArc(rec, 0, 360, true, mIconPaint);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        calcScreenMapping();
        if (System.currentTimeMillis() < mTimeout) {
            drawLook(canvas);
        }
    }

}