1
package es.leafsoft.gallery;
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;
30
public class GalleryView extends FrameLayout implements OnClickListener {
32
private static final String TAG = "GalleryView";
33
private static int[] BAR_COLOR = {0x77303030, 0x77000000};
35
private Context context;
36
private DisplayMetrics metrics = null;
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;
46
private String galleryTitle = "Gallery";
48
private int numberOfPhotos;
49
private boolean titlebarVisible;
50
private boolean toolbarVisible;
53
public GalleryView(Context context, DisplayMetrics metrics, String title, int initialPhoto, int numberOfPhotos, GalleryViewDataSource dataSource) {
55
this.context = context;
56
this.metrics = metrics;
57
this.numberOfPhotos = numberOfPhotos;
58
this.galleryTitle = title;
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);
69
this.addView(galleryScrollView);
70
this.addView(titlebar);
71
this.addView(toolbar);
73
this.galleryScrollView.scrollToPhoto(initialPhoto, true);
77
public void onClick(View v) {
78
if (v == this.nextImageButton)
79
this.galleryScrollView.scrollToPhoto(this.galleryScrollView.indexCurrentPhoto() + 1, true);
81
else if (v == this.previousImageButton)
82
this.galleryScrollView.scrollToPhoto(this.galleryScrollView.indexCurrentPhoto() - 1, true);
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);
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));
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);
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);
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);
121
toolbar.addView(previousImageButton);
122
toolbar.addView(counterTextView);
123
toolbar.addView(nextImageButton);
126
public void imageAtIndexDidLoad(Drawable image, int index) {
127
this.galleryScrollView.imageAtIndexDidLoad(image, index);
132
public void showToolbar(boolean show) {
134
toolbar.setVisibility(VISIBLE);
136
toolbar.setVisibility(INVISIBLE);
140
public void showTitlebar(boolean show) {
142
titlebar.setVisibility(VISIBLE);
144
titlebar.setVisibility(INVISIBLE); }
147
public void showHUD(boolean show) {
148
this.showToolbar(show);
149
this.showTitlebar(show);
152
public boolean isHUDVisible() {
153
return (toolbar.getVisibility() == VISIBLE && titlebar.getVisibility() == VISIBLE);
156
protected void willChangePhoto() {
160
protected void didChangePhoto() {
161
int currentPhoto = this.galleryScrollView.indexCurrentPhoto();
163
// Cambiamos el contador.
164
String text = currentPhoto + 1 + " / " + this.numberOfPhotos;
165
this.counterTextView.setText(text);
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);
172
this.previousImageButton.setVisibility(VISIBLE);
174
if (currentPhoto == this.numberOfPhotos - 1)
175
this.nextImageButton.setVisibility(INVISIBLE);
177
this.nextImageButton.setVisibility(VISIBLE);
181
class GalleryScrollView extends HorizontalScrollView {
183
private static final String TAG = "GalleryScrollView";
185
private GalleryViewDataSource dataSource;
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;
196
private Context context;
197
private DisplayMetrics metrics = null;
198
private String loadingMessage = null;
199
private LinearLayout content;
200
private Toast counter;
202
private ImageView imageViewLeft;
203
private ImageView imageViewCenter;
204
private ImageView imageViewRight;
206
public GalleryScrollView(Context context, DisplayMetrics metrics, int initialPhoto, int numberOfPhotos, GalleryViewDataSource dataSource) {
209
this.context = context;
210
this.widthOfPage = metrics.widthPixels;
211
this.numberOfPhotos = numberOfPhotos;
212
this.dataSource = dataSource;
213
this.metrics = metrics;
215
content = new LinearLayout(context);
216
content.setLayoutParams(new LayoutParams(metrics.widthPixels * numberOfPhotos, LayoutParams.FILL_PARENT));
218
for (int i = 0; i < numberOfPhotos; i++)
219
content.addView(makeLoadingView(), i);
221
imageViewLeft = this.makeImageView();
222
imageViewCenter = this.makeImageView();
223
imageViewRight = this.makeImageView();
225
this.addView(content);
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);
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));
247
ProgressBar spinner = new ProgressBar(context);
248
view.addView(spinner, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
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));
262
public int getNumberOfPhotos() {
263
return this.numberOfPhotos;
266
public int indexCurrentPhoto() {
267
return this.actualPhoto;
270
public void scrollToPhoto(int indexOfPhoto, boolean animated) {
271
if (indexOfPhoto < 0)
273
else if (indexOfPhoto >= numberOfPhotos)
274
indexOfPhoto = numberOfPhotos - 1;
276
if (this.actualPhoto == indexOfPhoto)
279
this.willChangeImage();
281
this.actualPhoto = indexOfPhoto;
282
this.requestImage(actualPhoto - 1);
283
this.requestImage(actualPhoto);
284
this.requestImage(actualPhoto + 1);
287
this.scrollTo(this.actualPhoto * widthOfPage, 0);
289
this.smoothScrollTo(this.actualPhoto * widthOfPage, 0);
291
this.didChangeImage();
295
protected void willChangeImage() {
296
if (this.counter != null)
297
this.counter.cancel();
301
protected void didChangeImage() {
302
if (this.canShowCounter) {
303
CharSequence text = this.actualPhoto + " / " + this.numberOfPhotos;
304
int duration = Toast.LENGTH_SHORT;
306
counter = Toast.makeText(context, text, duration);
310
((GalleryView)this.getParent()).didChangePhoto();
313
public void imageAtIndexDidLoad(Drawable image, int index) {
314
if (index < 0 || index >= numberOfPhotos)
317
this.content.removeViewAt(index);
318
this.insertImageView(image, index);
322
private void insertImageView(Drawable image, int index) {
323
if (index < 0 || index >= numberOfPhotos)
326
if (index == this.actualPhoto - 1) {
327
int i = this.content.indexOfChild(imageViewLeft);
329
this.content.removeView(imageViewLeft);
330
this.content.addView(this.makeLoadingView(), i);
333
imageViewLeft.setImageDrawable(image);
334
this.content.addView(imageViewLeft, index);
336
else if (index == this.actualPhoto) {
337
int i = this.content.indexOfChild(imageViewCenter);
339
this.content.removeView(imageViewCenter);
340
this.content.addView(this.makeLoadingView(), i);
343
imageViewCenter.setImageDrawable(image);
344
this.content.addView(imageViewCenter, index);
346
else if (index == this.actualPhoto + 1) {
347
int i = this.content.indexOfChild(imageViewRight);
349
this.content.removeView(imageViewRight);
350
this.content.addView(this.makeLoadingView(), i);
353
imageViewRight.setImageDrawable(image);
354
this.content.addView(imageViewRight, index);
358
private void requestImage(int image) {
359
if (image < 0 || image >= numberOfPhotos)
362
if (dataSource != null) {
363
Drawable photo = dataSource.photoAtIndex(image);
366
dataSource.loadPhotoAtIndex(image);
368
this.content.removeViewAt(image);
369
this.insertImageView(photo, image);
375
public boolean onKeyDown(int keyCode, KeyEvent event) {
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:
389
public boolean onKeyUp(int keyCode, KeyEvent event) {
391
case KeyEvent.KEYCODE_DPAD_LEFT:
392
case KeyEvent.KEYCODE_DPAD_UP:
393
this.scrollToPhoto(actualPhoto - 1, true);
396
case KeyEvent.KEYCODE_DPAD_RIGHT:
397
case KeyEvent.KEYCODE_DPAD_DOWN:
398
this.scrollToPhoto(actualPhoto + 1, true);
402
case KeyEvent.KEYCODE_DPAD_CENTER:
403
GalleryView view = (GalleryView)this.getParent();
404
view.showHUD(!view.isHUDVisible());
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;
420
case MotionEvent.ACTION_MOVE:
421
this.scrollTo(this.getScrollX() + (this.previousPosX - (int)ev.getX()), 0);
423
this.previousPosX = (int)ev.getX();
426
case MotionEvent.ACTION_UP:
427
this.finalPosX = (int)ev.getX();
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);
437
GalleryView view = (GalleryView)this.getParent();
438
view.showHUD(!view.isHUDVisible());
b'\\ No newline at end of file'