ImageShow.java revision 02f778af1f8a2250d91da450f82c30572de96062
1
2package com.android.gallery3d.filtershow.imageshow;
3
4import com.android.gallery3d.filtershow.FilterShowActivity;
5import com.android.gallery3d.filtershow.HistoryAdapter;
6import com.android.gallery3d.filtershow.cache.ImageLoader;
7import com.android.gallery3d.filtershow.presets.ImagePreset;
8import com.android.gallery3d.filtershow.ui.SliderListener;
9import com.android.gallery3d.filtershow.ui.SliderController;
10import com.android.gallery3d.R;
11import com.android.gallery3d.R.id;
12import com.android.gallery3d.R.layout;
13
14import android.content.Context;
15import android.graphics.Bitmap;
16import android.graphics.Canvas;
17import android.graphics.Paint;
18import android.graphics.Rect;
19import android.net.Uri;
20import android.os.Handler;
21import android.os.Message;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.MotionEvent;
25import android.view.View;
26import android.view.View.MeasureSpec;
27import android.widget.ArrayAdapter;
28
29public class ImageShow extends View implements SliderListener {
30
31    private static final String LOGTAG = "ImageShow";
32
33    protected Paint mPaint = new Paint();
34    private static int mTextSize = 24;
35    private static int mTextPadding = 20;
36
37    protected ImagePreset mImagePreset = null;
38    protected ImageLoader mImageLoader = null;
39
40    private Bitmap mBackgroundImage = null;
41    protected Bitmap mForegroundImage = null;
42    protected Bitmap mFilteredImage = null;
43
44    protected SliderController mSliderController = new SliderController();
45
46    private HistoryAdapter mAdapter = null;
47
48    protected Rect mImageBounds = null;
49    protected float mImageRotation = 0;
50    protected float mImageRotationZoomFactor = 0;
51
52    private boolean mShowControls = false;
53    private boolean mShowOriginal = false;
54    private String mToast = null;
55    private boolean mShowToast = false;
56    private boolean mImportantToast = false;
57
58    private Handler mHandler = new Handler();
59
60    public void onNewValue(int value) {
61        getImagePreset().setParameter(value);
62        mImageLoader.resetImageForPreset(getImagePreset(), this);
63        invalidate();
64    }
65
66    public ImageShow(Context context, AttributeSet attrs) {
67        super(context, attrs);
68        mSliderController.setListener(this);
69        mAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row,
70                R.id.rowTextView);
71    }
72
73    public ImageShow(Context context) {
74        super(context);
75        mSliderController.setListener(this);
76        mAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row,
77                R.id.rowTextView);
78    }
79
80    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
81        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
82        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
83        setMeasuredDimension(parentWidth, parentHeight);
84        mSliderController.setWidth(parentWidth);
85        mSliderController.setHeight(parentHeight);
86    }
87
88    public void setAdapter(HistoryAdapter adapter) {
89        mAdapter = adapter;
90    }
91
92    public void showToast(String text) {
93        showToast(text, false);
94    }
95
96    public void showToast(String text, boolean important) {
97        mToast = text;
98        mShowToast = true;
99        mImportantToast = important;
100        invalidate();
101
102        mHandler.postDelayed(new Runnable() {
103            @Override
104            public void run() {
105                mShowToast = false;
106                invalidate();
107            }
108        }, 400);
109    }
110
111    public ImagePreset getImagePreset() {
112        return mImagePreset;
113    }
114
115    public Bitmap getOriginalFrontBitmap() {
116        if (mImageLoader != null) {
117            return mImageLoader.getOriginalBitmapLarge();
118        }
119        return null;
120    }
121
122    public void drawToast(Canvas canvas) {
123        if (mShowToast && mToast != null) {
124            Paint paint = new Paint();
125            paint.setTextSize(128);
126            float textWidth = paint.measureText(mToast);
127            int toastX = (int) ((getWidth() - textWidth) / 2.0f);
128            int toastY = (int) (getHeight() / 3.0f);
129
130            paint.setARGB(255, 0, 0, 0);
131            canvas.drawText(mToast, toastX - 2, toastY - 2, paint);
132            canvas.drawText(mToast, toastX - 2, toastY, paint);
133            canvas.drawText(mToast, toastX, toastY - 2, paint);
134            canvas.drawText(mToast, toastX + 2, toastY + 2, paint);
135            canvas.drawText(mToast, toastX + 2, toastY, paint);
136            canvas.drawText(mToast, toastX, toastY + 2, paint);
137            if (mImportantToast) {
138                paint.setARGB(255, 200, 0, 0);
139            } else {
140                paint.setARGB(255, 255, 255, 255);
141            }
142            canvas.drawText(mToast, toastX, toastY, paint);
143        }
144    }
145
146    public void onDraw(Canvas canvas) {
147        if (mBackgroundImage == null) {
148            mBackgroundImage = mImageLoader.getBackgroundBitmap(getResources());
149        }
150        if (mBackgroundImage != null) {
151            Rect s = new Rect(0, 0, mBackgroundImage.getWidth(),
152                    mBackgroundImage.getHeight());
153            Rect d = new Rect(0, 0, getWidth(), getHeight());
154            canvas.drawBitmap(mBackgroundImage, s, d, mPaint);
155        }
156
157        Bitmap filteredImage = null;
158        if (mImageLoader != null) {
159            filteredImage = mImageLoader.getImageForPreset(this,
160                    getImagePreset(), showHires());
161//            Log.v(LOGTAG, "getImageForPreset " + getImagePreset() + " is: " + filteredImage);
162        }
163
164        if (filteredImage == null) {
165            // if no image for the current preset, use the previous one
166            filteredImage = mFilteredImage;
167        } else {
168            mFilteredImage = filteredImage;
169        }
170
171        if (mShowOriginal || mFilteredImage == null) {
172            mFilteredImage = mForegroundImage;
173        }
174
175        if (mFilteredImage != null) {
176            Rect s = new Rect(0, 0, mFilteredImage.getWidth(),
177                    mFilteredImage.getHeight());
178            float ratio = mFilteredImage.getWidth()
179                    / (float) mFilteredImage.getHeight();
180            float w = getWidth();
181            float h = w / ratio;
182            float ty = (getHeight() - h) / 2.0f;
183            float tx = 0;
184            // t = 0;
185            if (ratio < 1.0f) { // portrait image
186                h = getHeight();
187                w = h * ratio;
188                tx = (getWidth() - w) / 2.0f;
189                ty = 0;
190            }
191            Rect d = new Rect((int) tx, (int) ty, (int) (w + tx),
192                    (int) (h + ty));
193            mImageBounds = d;
194
195            canvas.drawBitmap(mFilteredImage, s, d, mPaint);
196        }
197
198        if (showTitle() && getImagePreset() != null) {
199            mPaint.setARGB(200, 0, 0, 0);
200            mPaint.setTextSize(mTextSize);
201
202            Rect textRect = new Rect(0, 0, getWidth(), mTextSize + mTextPadding);
203            canvas.drawRect(textRect, mPaint);
204            mPaint.setARGB(255, 200, 200, 200);
205            canvas.drawText(getImagePreset().name(), mTextPadding,
206                    10 + mTextPadding, mPaint);
207        }
208        mPaint.setARGB(255, 150, 150, 150);
209        mPaint.setStrokeWidth(4);
210        canvas.drawLine(0, 0, getWidth(), 0, mPaint);
211
212        if (showControls()) {
213            mSliderController.onDraw(canvas);
214        }
215
216        drawToast(canvas);
217    }
218
219    public void setShowControls(boolean value) {
220        mShowControls = value;
221    }
222
223    public boolean showControls() {
224        return mShowControls;
225    }
226
227    public boolean showHires() {
228        return true;
229    }
230
231    public boolean showTitle() {
232        return true;
233    }
234
235    public void setImagePreset(ImagePreset preset) {
236        setImagePreset(preset, true);
237    }
238
239    public void setImagePreset(ImagePreset preset, boolean addToHistory) {
240        mImagePreset = preset;
241        if (getImagePreset() != null) {
242//            Log.v(LOGTAG, "add " + getImagePreset().name() + " " + getImagePreset());
243            if (addToHistory) {
244                mAdapter.insert(getImagePreset(), 0);
245            }
246            getImagePreset().setEndpoint(this);
247            updateImage();
248        }
249//        Log.v(LOGTAG, "invalidate from setImagePreset");
250        invalidate();
251    }
252
253    public void setImageLoader(ImageLoader loader) {
254        mImageLoader = loader;
255        if (mImageLoader != null) {
256            mImageLoader.addListener(this);
257        }
258    }
259
260    public void updateImage() {
261        mForegroundImage = getOriginalFrontBitmap();
262        /*
263         * if (mImageLoader != null) {
264         * mImageLoader.resetImageForPreset(getImagePreset(), this); }
265         */
266
267        /*
268         * if (mForegroundImage != null) { Bitmap filteredImage =
269         * mForegroundImage.copy(mConfig, true);
270         * getImagePreset().apply(filteredImage); invalidate(); }
271         */
272    }
273
274    public void updateFilteredImage(Bitmap bitmap) {
275        mFilteredImage = bitmap;
276        // Log.v(LOGTAG, "invalidate from updateFilteredImage");
277        // invalidate();
278    }
279
280    public void saveImage(FilterShowActivity filterShowActivity) {
281        mImageLoader.saveImage(getImagePreset(), filterShowActivity);
282    }
283
284    public boolean onTouchEvent(MotionEvent event) {
285        super.onTouchEvent(event);
286        mSliderController.onTouchEvent(event);
287        // Log.v(LOGTAG, "invalidate from onTouchEvent");
288        invalidate();
289        return true;
290    }
291
292    // listview stuff
293
294    public ArrayAdapter getListAdapter() {
295        return mAdapter;
296    }
297
298    public void onItemClick(int position) {
299        Log.v(LOGTAG, "Click on item " + position);
300        Log.v(LOGTAG, "item " + position + " is " + mAdapter.getItem(position));
301        setImagePreset(new ImagePreset(mAdapter.getItem(position)), false); // we
302                                                                            // need
303                                                                            // a
304                                                                            // copy
305                                                                            // from
306                                                                            // the
307                                                                            // history
308        mAdapter.setCurrentPreset(position);
309    }
310
311    public void showOriginal(boolean show) {
312        mShowOriginal = show;
313        invalidate();
314    }
315
316    public float getImageRotation() {
317        return mImageRotation;
318    }
319
320    public float getImageRotationZoomFactor() {
321        return mImageRotationZoomFactor;
322    }
323
324    public void setImageRotation(float imageRotation,
325            float imageRotationZoomFactor) {
326        if (imageRotation != mImageRotation) {
327            invalidate();
328        }
329        mImageRotation = imageRotation;
330        mImageRotationZoomFactor = imageRotationZoomFactor;
331    }
332}
333