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