OSDN Git Service

New UI...
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / imageshow / ImageShow.java
1
2 package com.android.gallery3d.filtershow.imageshow;
3
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.graphics.Canvas;
7 import android.graphics.Paint;
8 import android.graphics.Rect;
9 import android.os.Handler;
10 import android.util.AttributeSet;
11 import android.view.MotionEvent;
12 import android.view.View;
13 import android.widget.ArrayAdapter;
14 import android.widget.SeekBar;
15 import android.widget.SeekBar.OnSeekBarChangeListener;
16
17 import com.android.gallery3d.R;
18 import com.android.gallery3d.filtershow.FilterShowActivity;
19 import com.android.gallery3d.filtershow.HistoryAdapter;
20 import com.android.gallery3d.filtershow.ImageStateAdapter;
21 import com.android.gallery3d.filtershow.PanelController;
22 import com.android.gallery3d.filtershow.cache.ImageLoader;
23 import com.android.gallery3d.filtershow.filters.ImageFilter;
24 import com.android.gallery3d.filtershow.presets.ImagePreset;
25 import com.android.gallery3d.filtershow.ui.SliderController;
26 import com.android.gallery3d.filtershow.ui.SliderListener;
27
28 import java.io.File;
29
30 public class ImageShow extends View implements SliderListener, OnSeekBarChangeListener {
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 mHistoryAdapter = null;
49     private ImageStateAdapter mImageStateAdapter = null;
50
51     protected Rect mImageBounds = null;
52     protected float mImageRotation = 0;
53     protected float mImageRotationZoomFactor = 0;
54
55     private boolean mShowControls = false;
56     private boolean mShowOriginal = false;
57     private String mToast = null;
58     private boolean mShowToast = false;
59     private boolean mImportantToast = false;
60
61     protected float mTouchX = 0;
62     protected float mTouchY = 0;
63
64     private SeekBar mSeekBar = null;
65     private PanelController mController = null;
66
67     private final Handler mHandler = new Handler();
68
69     public void select() {
70         if (getCurrentFilter() != null) {
71             int parameter = getCurrentFilter().getParameter();
72             updateSeekBar(parameter);
73         }
74     }
75
76     public void updateSeekBar(int parameter) {
77         if (mSeekBar == null) {
78             return;
79         }
80         int progress = parameter + 100;
81         mSeekBar.setProgress(progress);
82         if (getPanelController() != null) {
83             getPanelController().onNewValue(parameter);
84         }
85     }
86
87     public void unselect() {
88
89     }
90
91     public void resetParameter() {
92         onNewValue(0);
93         mSliderController.reset();
94     }
95
96     public void setPanelController(PanelController controller) {
97         mController = controller;
98     }
99
100     public PanelController getPanelController() {
101         return mController;
102     }
103
104     @Override
105     public void onNewValue(int value) {
106         if (getCurrentFilter() != null) {
107             getCurrentFilter().setParameter(value);
108         }
109         if (getImagePreset() != null) {
110             mImageLoader.resetImageForPreset(getImagePreset(), this);
111             getImagePreset().fillImageStateAdapter(mImageStateAdapter);
112         }
113         if (getPanelController() != null) {
114             getPanelController().onNewValue(value);
115         }
116         updateSeekBar(value);
117         invalidate();
118     }
119
120     @Override
121     public void onTouchDown(float x, float y) {
122         mTouchX = x;
123         mTouchY = y;
124         invalidate();
125     }
126
127     @Override
128     public void onTouchUp() {
129     }
130
131     public ImageShow(Context context, AttributeSet attrs) {
132         super(context, attrs);
133         mSliderController.setListener(this);
134         mHistoryAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row,
135                 R.id.rowTextView);
136         mImageStateAdapter = new ImageStateAdapter(context,
137                 R.layout.filtershow_imagestate_row);
138     }
139
140     public ImageShow(Context context) {
141         super(context);
142         mSliderController.setListener(this);
143         mHistoryAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row,
144                 R.id.rowTextView);
145     }
146
147     @Override
148     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
149         int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
150         int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
151         setMeasuredDimension(parentWidth, parentHeight);
152         mSliderController.setWidth(parentWidth);
153         mSliderController.setHeight(parentHeight);
154     }
155
156     public void setSeekBar(SeekBar seekBar) {
157         mSeekBar = seekBar;
158         mSeekBar.setOnSeekBarChangeListener(this);
159     }
160
161     public void setCurrentFilter(ImageFilter filter) {
162         mCurrentFilter = filter;
163     }
164
165     public ImageFilter getCurrentFilter() {
166         return mCurrentFilter;
167     }
168
169     public void setAdapter(HistoryAdapter adapter) {
170         mHistoryAdapter = adapter;
171     }
172
173     public void showToast(String text) {
174         showToast(text, false);
175     }
176
177     public void showToast(String text, boolean important) {
178         mToast = text;
179         mShowToast = true;
180         mImportantToast = important;
181         invalidate();
182
183         mHandler.postDelayed(new Runnable() {
184             @Override
185             public void run() {
186                 mShowToast = false;
187                 invalidate();
188             }
189         }, 400);
190     }
191
192     public Rect getImageBounds() {
193         return mImageBounds;
194     }
195
196     public ImagePreset getImagePreset() {
197         return mImagePreset;
198     }
199
200     public Bitmap getOriginalFrontBitmap() {
201         if (mImageLoader != null) {
202             return mImageLoader.getOriginalBitmapLarge();
203         }
204         return null;
205     }
206
207     public void drawToast(Canvas canvas) {
208         if (mShowToast && mToast != null) {
209             Paint paint = new Paint();
210             paint.setTextSize(128);
211             float textWidth = paint.measureText(mToast);
212             int toastX = (int) ((getWidth() - textWidth) / 2.0f);
213             int toastY = (int) (getHeight() / 3.0f);
214
215             paint.setARGB(255, 0, 0, 0);
216             canvas.drawText(mToast, toastX - 2, toastY - 2, paint);
217             canvas.drawText(mToast, toastX - 2, toastY, paint);
218             canvas.drawText(mToast, toastX, toastY - 2, paint);
219             canvas.drawText(mToast, toastX + 2, toastY + 2, paint);
220             canvas.drawText(mToast, toastX + 2, toastY, paint);
221             canvas.drawText(mToast, toastX, toastY + 2, paint);
222             if (mImportantToast) {
223                 paint.setARGB(255, 200, 0, 0);
224             } else {
225                 paint.setARGB(255, 255, 255, 255);
226             }
227             canvas.drawText(mToast, toastX, toastY, paint);
228         }
229     }
230
231     @Override
232     public void onDraw(Canvas canvas) {
233         drawBackground(canvas);
234         getFilteredImage();
235         drawImage(canvas, mFilteredImage);
236
237         if (showTitle() && getImagePreset() != null) {
238             mPaint.setARGB(200, 0, 0, 0);
239             mPaint.setTextSize(mTextSize);
240
241             Rect textRect = new Rect(0, 0, getWidth(), mTextSize + mTextPadding);
242             canvas.drawRect(textRect, mPaint);
243             mPaint.setARGB(255, 200, 200, 200);
244             canvas.drawText(getImagePreset().name(), mTextPadding,
245                     10 + mTextPadding, mPaint);
246         }
247         mPaint.setARGB(255, 150, 150, 150);
248         mPaint.setStrokeWidth(4);
249         canvas.drawLine(0, 0, getWidth(), 0, mPaint);
250
251         if (showControls()) {
252             mSliderController.onDraw(canvas);
253         }
254
255         drawToast(canvas);
256     }
257
258     public void getFilteredImage() {
259         Bitmap filteredImage = null;
260         if (mImageLoader != null) {
261             filteredImage = mImageLoader.getImageForPreset(this,
262                     getImagePreset(), showHires());
263         }
264
265         if (filteredImage == null) {
266             // if no image for the current preset, use the previous one
267             filteredImage = mFilteredImage;
268         } else {
269             mFilteredImage = filteredImage;
270         }
271
272         if (mShowOriginal || mFilteredImage == null) {
273             mFilteredImage = mForegroundImage;
274         }
275     }
276
277     public void drawImage(Canvas canvas, Bitmap image) {
278         if (image != null) {
279             Rect s = new Rect(0, 0, image.getWidth(),
280                     image.getHeight());
281             float ratio = image.getWidth()
282                     / (float) image.getHeight();
283             float w = getWidth();
284             float h = w / ratio;
285             float ty = (getHeight() - h) / 2.0f;
286             float tx = 0;
287             // t = 0;
288             if (ratio < 1.0f) { // portrait image
289                 h = getHeight();
290                 w = h * ratio;
291                 tx = (getWidth() - w) / 2.0f;
292                 ty = 0;
293             }
294             Rect d = new Rect((int) tx, (int) ty, (int) (w + tx),
295                     (int) (h + ty));
296             mImageBounds = d;
297
298             canvas.drawBitmap(image, s, d, mPaint);
299         }
300     }
301
302     public void drawBackground(Canvas canvas) {
303         if (mBackgroundImage == null) {
304             mBackgroundImage = mImageLoader.getBackgroundBitmap(getResources());
305         }
306         if (mBackgroundImage != null) {
307             Rect s = new Rect(0, 0, mBackgroundImage.getWidth(),
308                     mBackgroundImage.getHeight());
309             Rect d = new Rect(0, 0, getWidth(), getHeight());
310             canvas.drawBitmap(mBackgroundImage, s, d, mPaint);
311         }
312     }
313
314     public ImageShow setShowControls(boolean value) {
315         mShowControls = value;
316         if (mShowControls) {
317             if (mSeekBar != null) {
318                 mSeekBar.setVisibility(View.VISIBLE);
319             }
320         } else {
321             if (mSeekBar != null) {
322                 mSeekBar.setVisibility(View.INVISIBLE);
323             }
324         }
325         return this;
326     }
327
328     public boolean showControls() {
329         return mShowControls;
330     }
331
332     public boolean showHires() {
333         return true;
334     }
335
336     public boolean showTitle() {
337         return false;
338     }
339
340     public void setImagePreset(ImagePreset preset) {
341         setImagePreset(preset, true);
342     }
343
344     public void setImagePreset(ImagePreset preset, boolean addToHistory) {
345         mImagePreset = preset;
346         if (getImagePreset() != null) {
347             if (addToHistory) {
348                 mHistoryAdapter.insert(getImagePreset(), 0);
349             }
350             getImagePreset().setEndpoint(this);
351             updateImage();
352         }
353         mImagePreset.fillImageStateAdapter(mImageStateAdapter);
354         invalidate();
355     }
356
357     public void setImageLoader(ImageLoader loader) {
358         mImageLoader = loader;
359         if (mImageLoader != null) {
360             mImageLoader.addListener(this);
361         }
362     }
363
364     public void updateImage() {
365         mForegroundImage = getOriginalFrontBitmap();
366         /*
367          * if (mImageLoader != null) {
368          * mImageLoader.resetImageForPreset(getImagePreset(), this); }
369          */
370
371         /*
372          * if (mForegroundImage != null) { Bitmap filteredImage =
373          * mForegroundImage.copy(mConfig, true);
374          * getImagePreset().apply(filteredImage); invalidate(); }
375          */
376     }
377
378     public void updateFilteredImage(Bitmap bitmap) {
379         mFilteredImage = bitmap;
380     }
381
382     public void saveImage(FilterShowActivity filterShowActivity, File file) {
383         mImageLoader.saveImage(getImagePreset(), filterShowActivity, file);
384     }
385
386     @Override
387     public boolean onTouchEvent(MotionEvent event) {
388         super.onTouchEvent(event);
389         mSliderController.onTouchEvent(event);
390         invalidate();
391         return true;
392     }
393
394     // listview stuff
395
396     public ArrayAdapter getHistoryAdapter() {
397         return mHistoryAdapter;
398     }
399
400     public ArrayAdapter getImageStateAdapter() {
401         return mImageStateAdapter;
402     }
403
404     public void onItemClick(int position) {
405         setImagePreset(new ImagePreset(mHistoryAdapter.getItem(position)), false);
406         // we need a copy from the history
407         mHistoryAdapter.setCurrentPreset(position);
408     }
409
410     public void showOriginal(boolean show) {
411         mShowOriginal = show;
412         invalidate();
413     }
414
415     public float getImageRotation() {
416         return mImageRotation;
417     }
418
419     public float getImageRotationZoomFactor() {
420         return mImageRotationZoomFactor;
421     }
422
423     public void setImageRotation(float imageRotation,
424             float imageRotationZoomFactor) {
425         if (imageRotation != mImageRotation) {
426             invalidate();
427         }
428         mImageRotation = imageRotation;
429         mImageRotationZoomFactor = imageRotationZoomFactor;
430     }
431
432     @Override
433     public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
434         onNewValue(progress - 100);
435     }
436
437     @Override
438     public void onStartTrackingTouch(SeekBar arg0) {
439         // TODO Auto-generated method stub
440
441     }
442
443     @Override
444     public void onStopTrackingTouch(SeekBar arg0) {
445         // TODO Auto-generated method stub
446
447     }
448 }