~dav-surf/cineol/android

« back to all changes in this revision

Viewing changes to src/es/leafsoft/gallery/GalleryView.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:
 
1
package es.leafsoft.gallery;
 
2
 
 
3
import android.content.Context;
 
4
import android.graphics.Typeface;
 
5
import android.graphics.drawable.BitmapDrawable;
 
6
import android.graphics.drawable.Drawable;
 
7
import android.graphics.drawable.GradientDrawable;
 
8
import android.graphics.drawable.GradientDrawable.Orientation;
 
9
import android.util.DisplayMetrics;
 
10
import android.util.Log;
 
11
import android.view.Gravity;
 
12
import android.view.KeyEvent;
 
13
import android.view.MotionEvent;
 
14
import android.view.View;
 
15
import android.view.ViewGroup;
 
16
import android.view.View.OnClickListener;
 
17
import android.view.View.OnKeyListener;
 
18
import android.widget.Button;
 
19
import android.widget.FrameLayout;
 
20
import android.widget.HorizontalScrollView;
 
21
import android.widget.ImageButton;
 
22
import android.widget.ImageView;
 
23
import android.widget.LinearLayout;
 
24
import android.widget.ProgressBar;
 
25
import android.widget.TextView;
 
26
import android.widget.Toast;
 
27
import android.widget.FrameLayout.LayoutParams;
 
28
import app.cineol.R;
 
29
 
 
30
public class GalleryView extends FrameLayout implements OnClickListener {
 
31
        
 
32
        private static final String TAG = "GalleryView";
 
33
        private static int[] BAR_COLOR = {0x77303030, 0x77000000};
 
34
 
 
35
        private Context context;
 
36
        private DisplayMetrics metrics = null;
 
37
 
 
38
        private GalleryScrollView       galleryScrollView = null;
 
39
        private View                            titlebar = null;
 
40
        private FrameLayout             toolbar = null;
 
41
        private ImageButton             nextImageButton = null;
 
42
        private ImageButton             previousImageButton = null;
 
43
        private TextView                        counterTextView = null;
 
44
 
 
45
        
 
46
        private String galleryTitle = "Gallery";
 
47
        
 
48
        private int numberOfPhotos;
 
49
        private boolean titlebarVisible;
 
50
        private boolean toolbarVisible;
 
51
 
 
52
        
 
53
        public GalleryView(Context context, DisplayMetrics metrics, String title, int initialPhoto, int numberOfPhotos, GalleryViewDataSource dataSource) {
 
54
                super(context);
 
55
                this.context = context;
 
56
                this.metrics = metrics;
 
57
                this.numberOfPhotos = numberOfPhotos;
 
58
                this.galleryTitle = title;
 
59
                
 
60
                this.initTitleBar();
 
61
                this.initToolBar();
 
62
                
 
63
                galleryScrollView = new GalleryScrollView(context, metrics, initialPhoto, numberOfPhotos, dataSource);
 
64
                galleryScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
 
65
                galleryScrollView.setVerticalFadingEdgeEnabled(false);
 
66
                galleryScrollView.setHorizontalFadingEdgeEnabled(false);
 
67
                galleryScrollView.setHorizontalScrollBarEnabled(false);
 
68
                
 
69
                this.addView(galleryScrollView);
 
70
                this.addView(titlebar);
 
71
                this.addView(toolbar);
 
72
                
 
73
                this.galleryScrollView.scrollToPhoto(initialPhoto, true);
 
74
        }
 
75
        
 
76
 
 
77
        public void onClick(View v) {
 
78
                if (v == this.nextImageButton)
 
79
                        this.galleryScrollView.scrollToPhoto(this.galleryScrollView.indexCurrentPhoto() + 1, true);
 
80
                
 
81
                else if (v == this.previousImageButton)
 
82
                        this.galleryScrollView.scrollToPhoto(this.galleryScrollView.indexCurrentPhoto() - 1, true);
 
83
        }
 
84
        
 
85
        private void initTitleBar() {
 
86
                titlebar = new TextView(context);
 
87
                titlebar.setLayoutParams(new LayoutParams(metrics.widthPixels, 40, Gravity.TOP));                               
 
88
                titlebar.setBackgroundDrawable(new GradientDrawable(Orientation.BOTTOM_TOP, BAR_COLOR));
 
89
                ((TextView) titlebar).setText(this.galleryTitle);
 
90
                ((TextView) titlebar).setTextSize(16);
 
91
                ((TextView) titlebar).setTextColor(0xFFFFFFFF);
 
92
                ((TextView) titlebar).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 
93
                ((TextView) titlebar).setGravity(Gravity.CENTER);
 
94
        }
 
95
        
 
96
        private void initToolBar() {
 
97
                toolbar = new FrameLayout(context);
 
98
                toolbar.setLayoutParams(new LayoutParams(metrics.widthPixels, 40, Gravity.BOTTOM));                             
 
99
                toolbar.setBackgroundDrawable(new GradientDrawable(Orientation.TOP_BOTTOM, BAR_COLOR));
 
100
                                
 
101
                nextImageButton = new ImageButton(context);
 
102
                nextImageButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT));
 
103
                nextImageButton.setImageResource(R.drawable.toolbar_icon_right);
 
104
                nextImageButton.setBackgroundColor(0x00000000);
 
105
                nextImageButton.setOnClickListener(this);
 
106
 
 
107
                previousImageButton = new ImageButton(context);
 
108
                previousImageButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT));
 
109
                previousImageButton.setImageResource(R.drawable.toolbar_icon_left);
 
110
                previousImageButton.setBackgroundColor(0x00000000);
 
111
                previousImageButton.setOnClickListener(this);
 
112
 
 
113
                counterTextView = new TextView(context);
 
114
                counterTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
 
115
                counterTextView.setText("000 / 000");
 
116
                counterTextView.setTextSize(16);
 
117
                counterTextView.setTextColor(0xFFFFFFFF);
 
118
                counterTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 
119
                counterTextView.setGravity(Gravity.CENTER);
 
120
                
 
121
                toolbar.addView(previousImageButton);
 
122
                toolbar.addView(counterTextView);
 
123
                toolbar.addView(nextImageButton);
 
124
        }
 
125
        
 
126
        public void imageAtIndexDidLoad(Drawable image, int index) {
 
127
                this.galleryScrollView.imageAtIndexDidLoad(image, index);
 
128
        }
 
129
 
 
130
        
 
131
        // TODO
 
132
        public void showToolbar(boolean show) {
 
133
                if (show)
 
134
                        toolbar.setVisibility(VISIBLE);
 
135
                else                    
 
136
                        toolbar.setVisibility(INVISIBLE);
 
137
        }
 
138
 
 
139
        //TODO
 
140
        public void showTitlebar(boolean show) {
 
141
                if (show)
 
142
                        titlebar.setVisibility(VISIBLE);
 
143
                else                    
 
144
                        titlebar.setVisibility(INVISIBLE);      }
 
145
        
 
146
        //TODO
 
147
        public void showHUD(boolean show) {
 
148
                this.showToolbar(show);
 
149
                this.showTitlebar(show);
 
150
        }
 
151
        
 
152
        public boolean isHUDVisible() {
 
153
                return (toolbar.getVisibility() == VISIBLE && titlebar.getVisibility() == VISIBLE);
 
154
        }
 
155
        
 
156
        protected void willChangePhoto() {
 
157
                
 
158
        }
 
159
        
 
160
        protected void didChangePhoto() {
 
161
                int currentPhoto = this.galleryScrollView.indexCurrentPhoto();
 
162
                
 
163
                // Cambiamos el contador.
 
164
                String text = currentPhoto + 1 + " / " + this.numberOfPhotos;
 
165
                this.counterTextView.setText(text);
 
166
                
 
167
                // Comprobamos si estamos al principio o al final de la galeria.
 
168
                // Si es asi deshabilitamos el boton que corresponda.
 
169
                if (currentPhoto == 0)
 
170
                        this.previousImageButton.setVisibility(INVISIBLE);
 
171
                else
 
172
                        this.previousImageButton.setVisibility(VISIBLE);
 
173
                        
 
174
                if (currentPhoto == this.numberOfPhotos - 1)
 
175
                        this.nextImageButton.setVisibility(INVISIBLE);
 
176
                else
 
177
                        this.nextImageButton.setVisibility(VISIBLE);                    
 
178
        }
 
179
}
 
180
 
 
181
class GalleryScrollView extends HorizontalScrollView {
 
182
        
 
183
        private static final String TAG = "GalleryScrollView";
 
184
        
 
185
        private GalleryViewDataSource dataSource;
 
186
        
 
187
        private int widthOfPage;
 
188
        private int numberOfPhotos;
 
189
        private int initialPosX;
 
190
        private int previousPosX;
 
191
        private int finalPosX;
 
192
        private int actualPhoto = -1;
 
193
        private boolean moved;
 
194
        private boolean canShowCounter = false;
 
195
        
 
196
        private Context context;
 
197
        private DisplayMetrics metrics = null;
 
198
        private String loadingMessage = null;
 
199
        private LinearLayout content;
 
200
        private Toast counter;
 
201
        
 
202
        private ImageView imageViewLeft;
 
203
        private ImageView imageViewCenter;
 
204
        private ImageView imageViewRight;
 
205
 
 
206
        public GalleryScrollView(Context context, DisplayMetrics metrics, int initialPhoto, int numberOfPhotos, GalleryViewDataSource dataSource) {
 
207
                super(context);
 
208
                
 
209
                this.context = context;
 
210
                this.widthOfPage = metrics.widthPixels;
 
211
                this.numberOfPhotos = numberOfPhotos;
 
212
                this.dataSource = dataSource;
 
213
                this.metrics = metrics;
 
214
                
 
215
                content = new LinearLayout(context);
 
216
                content.setLayoutParams(new LayoutParams(metrics.widthPixels * numberOfPhotos, LayoutParams.FILL_PARENT));
 
217
                        
 
218
                for (int i = 0; i < numberOfPhotos; i++)
 
219
            content.addView(makeLoadingView(), i);
 
220
                
 
221
                imageViewLeft   = this.makeImageView();
 
222
                imageViewCenter = this.makeImageView();
 
223
                imageViewRight  = this.makeImageView();
 
224
                
 
225
                this.addView(content);
 
226
        }
 
227
        
 
228
        
 
229
        private ImageView makeImageView() {
 
230
                ImageView imageView = new ImageView(context);
 
231
                imageView.setAdjustViewBounds(false);
 
232
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
 
233
                imageView.setLayoutParams(new LayoutParams(metrics.widthPixels, LayoutParams.FILL_PARENT));
 
234
                imageView.setPadding(4, 0, 4, 0);            
 
235
                
 
236
                return imageView;
 
237
        }
 
238
 
 
239
        
 
240
        private View makeLoadingView() {
 
241
                LinearLayout view = new LinearLayout(context);
 
242
                view.setFocusable(false);
 
243
                view.setOrientation(LinearLayout.VERTICAL);
 
244
                view.setGravity(Gravity.CENTER);
 
245
                view.setLayoutParams(new LayoutParams(metrics.widthPixels, LayoutParams.FILL_PARENT));
 
246
                                
 
247
                ProgressBar spinner = new ProgressBar(context);
 
248
                view.addView(spinner, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 
249
                
 
250
                if (this.loadingMessage != null) {
 
251
                        TextView label = new TextView(context);
 
252
                        label.setText(this.loadingMessage);
 
253
                        label.setTextSize(16);
 
254
                        label.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
 
255
                        label.setPadding(0, 8, 0, 0);
 
256
                        view.addView(label, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 
257
                }
 
258
                
 
259
                return view;
 
260
        }
 
261
        
 
262
        public int getNumberOfPhotos() {
 
263
                return this.numberOfPhotos;
 
264
        }
 
265
        
 
266
        public int indexCurrentPhoto() {
 
267
                return this.actualPhoto;
 
268
        }
 
269
        
 
270
        public void scrollToPhoto(int indexOfPhoto, boolean animated) {
 
271
                if (indexOfPhoto < 0)
 
272
                        indexOfPhoto = 0;
 
273
                else if (indexOfPhoto >= numberOfPhotos)
 
274
                        indexOfPhoto = numberOfPhotos - 1;
 
275
                
 
276
                if (this.actualPhoto == indexOfPhoto)
 
277
                        return;
 
278
                
 
279
                this.willChangeImage();
 
280
                
 
281
                this.actualPhoto = indexOfPhoto;
 
282
                this.requestImage(actualPhoto - 1);
 
283
                this.requestImage(actualPhoto);
 
284
                this.requestImage(actualPhoto + 1);
 
285
 
 
286
                if (!animated)
 
287
                        this.scrollTo(this.actualPhoto * widthOfPage, 0);
 
288
                else
 
289
                        this.smoothScrollTo(this.actualPhoto * widthOfPage, 0);
 
290
                
 
291
                this.didChangeImage();
 
292
        }
 
293
 
 
294
        // TODO
 
295
        protected void willChangeImage() {
 
296
                if (this.counter != null)
 
297
                        this.counter.cancel();
 
298
        }
 
299
        
 
300
        // TODO
 
301
        protected void didChangeImage() {
 
302
                if (this.canShowCounter) {                      
 
303
                        CharSequence text = this.actualPhoto + " / " + this.numberOfPhotos;
 
304
                        int duration = Toast.LENGTH_SHORT;
 
305
 
 
306
                        counter = Toast.makeText(context, text, duration);
 
307
                        counter.show();
 
308
                }
 
309
                
 
310
                ((GalleryView)this.getParent()).didChangePhoto();
 
311
        }
 
312
        
 
313
        public void imageAtIndexDidLoad(Drawable image, int index) {
 
314
                if (index < 0 || index >= numberOfPhotos)
 
315
                        return;
 
316
                
 
317
                this.content.removeViewAt(index);
 
318
                this.insertImageView(image, index);
 
319
        }
 
320
        
 
321
        
 
322
        private void insertImageView(Drawable image, int index) {
 
323
                if (index < 0 || index >= numberOfPhotos)
 
324
                        return;
 
325
                
 
326
                if (index == this.actualPhoto - 1) {
 
327
                        int i = this.content.indexOfChild(imageViewLeft);
 
328
                        if (i > -1) {
 
329
                                this.content.removeView(imageViewLeft);
 
330
                                this.content.addView(this.makeLoadingView(), i);
 
331
                        }
 
332
                        
 
333
                        imageViewLeft.setImageDrawable(image);
 
334
                        this.content.addView(imageViewLeft, index);
 
335
                }
 
336
                else if (index == this.actualPhoto) {
 
337
                        int i = this.content.indexOfChild(imageViewCenter);
 
338
                        if (i > -1) {
 
339
                                this.content.removeView(imageViewCenter);
 
340
                                this.content.addView(this.makeLoadingView(), i);
 
341
                        }
 
342
                        
 
343
                        imageViewCenter.setImageDrawable(image);
 
344
                        this.content.addView(imageViewCenter, index);
 
345
                }
 
346
                else if (index == this.actualPhoto + 1) {
 
347
                        int i = this.content.indexOfChild(imageViewRight);
 
348
                        if (i > -1) {
 
349
                                this.content.removeView(imageViewRight);
 
350
                                this.content.addView(this.makeLoadingView(), i);
 
351
                        }
 
352
                        
 
353
                        imageViewRight.setImageDrawable(image);
 
354
                        this.content.addView(imageViewRight, index);
 
355
                }
 
356
        }
 
357
        
 
358
        private void requestImage(int image) {
 
359
                if (image < 0 || image >= numberOfPhotos)
 
360
                        return;
 
361
                
 
362
                if (dataSource != null) {
 
363
                        Drawable photo = dataSource.photoAtIndex(image);
 
364
                        
 
365
                        if (photo == null)
 
366
                                dataSource.loadPhotoAtIndex(image);
 
367
                        else {
 
368
                                this.content.removeViewAt(image);
 
369
                                this.insertImageView(photo, image);
 
370
                        }
 
371
                }
 
372
        }
 
373
        
 
374
        @Override
 
375
        public boolean onKeyDown(int keyCode, KeyEvent event) {
 
376
                switch (keyCode) {
 
377
                        case KeyEvent.KEYCODE_DPAD_LEFT:
 
378
                        case KeyEvent.KEYCODE_DPAD_UP:
 
379
                        case KeyEvent.KEYCODE_DPAD_RIGHT:
 
380
                        case KeyEvent.KEYCODE_DPAD_DOWN:
 
381
                        case KeyEvent.KEYCODE_DPAD_CENTER:
 
382
                                return true;
 
383
                }
 
384
                
 
385
                return false;
 
386
        }
 
387
 
 
388
        @Override
 
389
        public boolean onKeyUp(int keyCode, KeyEvent event) {
 
390
                switch (keyCode) {
 
391
                        case KeyEvent.KEYCODE_DPAD_LEFT:
 
392
                        case KeyEvent.KEYCODE_DPAD_UP:
 
393
                                this.scrollToPhoto(actualPhoto - 1, true);
 
394
                                return true;
 
395
                                
 
396
                        case KeyEvent.KEYCODE_DPAD_RIGHT:
 
397
                        case KeyEvent.KEYCODE_DPAD_DOWN:
 
398
                                this.scrollToPhoto(actualPhoto + 1, true);
 
399
                                return true;
 
400
                                
 
401
                        // TODO
 
402
                        case KeyEvent.KEYCODE_DPAD_CENTER:
 
403
                                GalleryView view = (GalleryView)this.getParent();
 
404
                                view.showHUD(!view.isHUDVisible());
 
405
                                return true;
 
406
                }
 
407
                
 
408
                return false;
 
409
        }
 
410
 
 
411
 
 
412
        @Override
 
413
        public boolean onTouchEvent(MotionEvent ev) {
 
414
                switch (ev.getAction()) {
 
415
                        case MotionEvent.ACTION_DOWN:
 
416
                                this.initialPosX = (int)ev.getX();
 
417
                                this.previousPosX = this.initialPosX;
 
418
                                break;
 
419
                                
 
420
                        case MotionEvent.ACTION_MOVE:
 
421
                                this.scrollTo(this.getScrollX() + (this.previousPosX - (int)ev.getX()), 0);
 
422
                                this.moved = true;
 
423
                                this.previousPosX = (int)ev.getX();
 
424
                                break;
 
425
                                
 
426
                        case MotionEvent.ACTION_UP:
 
427
                                this.finalPosX = (int)ev.getX();
 
428
                                if (moved) {                    
 
429
                                        moved = false;
 
430
                                                        
 
431
                                        if (this.initialPosX > this.finalPosX)
 
432
                                                this.scrollToPhoto(actualPhoto + 1, true);
 
433
                                        else if (this.initialPosX < this.finalPosX)
 
434
                                                this.scrollToPhoto(actualPhoto - 1, true);
 
435
                                }
 
436
                                else {
 
437
                                        GalleryView view = (GalleryView)this.getParent();
 
438
                                        view.showHUD(!view.isHUDVisible());
 
439
                                }
 
440
                                break;
 
441
                }
 
442
                
 
443
                return true;
 
444
        }
 
445
}
 
 
b'\\ No newline at end of file'