PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/UniversalImageLoaderExample/src/com/nostra13/example/universalimageloader/ImagePagerActivity.java

https://github.com/rashedulkabir/Android-Universal-Image-Loader
Java | 147 lines | 116 code | 27 blank | 4 comment | 1 complexity | 36a2a6f2704271d5ddfca28ca5c0f6ea MD5 | raw file
  1package com.nostra13.example.universalimageloader;
  2
  3import android.graphics.Bitmap;
  4import android.os.Bundle;
  5import android.os.Parcelable;
  6import android.support.v4.view.PagerAdapter;
  7import android.support.v4.view.ViewPager;
  8import android.view.LayoutInflater;
  9import android.view.View;
 10import android.view.animation.Animation;
 11import android.view.animation.AnimationUtils;
 12import android.widget.ImageView;
 13import android.widget.ProgressBar;
 14import android.widget.Toast;
 15
 16import com.nostra13.universalimageloader.core.DisplayImageOptions;
 17import com.nostra13.universalimageloader.core.assist.FailReason;
 18import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
 19import com.nostra13.universalimageloader.core.assist.ImageScaleType;
 20
 21/**
 22 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 23 */
 24public class ImagePagerActivity extends BaseActivity {
 25
 26	private ViewPager pager;
 27
 28	private DisplayImageOptions options;
 29
 30	public void onCreate(Bundle savedInstanceState) {
 31		super.onCreate(savedInstanceState);
 32
 33		setContentView(R.layout.ac_image_pager);
 34		Bundle bundle = getIntent().getExtras();
 35		String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
 36		int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
 37
 38		options = new DisplayImageOptions.Builder()
 39			.showImageForEmptyUri(R.drawable.image_for_empty_url)
 40			.cacheOnDisc()
 41			.imageScaleType(ImageScaleType.EXACT)
 42			.build();
 43
 44		pager = (ViewPager) findViewById(R.id.pager);
 45		pager.setAdapter(new ImagePagerAdapter(imageUrls));
 46		pager.setCurrentItem(pagerPosition);
 47	}
 48
 49	@Override
 50	protected void onStop() {
 51		imageLoader.stop();
 52		super.onStop();
 53	}
 54
 55	private class ImagePagerAdapter extends PagerAdapter {
 56
 57		private String[] images;
 58		private LayoutInflater inflater;
 59
 60		ImagePagerAdapter(String[] images) {
 61			this.images = images;
 62			inflater = getLayoutInflater();
 63		}
 64
 65		@Override
 66		public void destroyItem(View container, int position, Object object) {
 67			((ViewPager) container).removeView((View) object);
 68		}
 69
 70		@Override
 71		public void finishUpdate(View container) {
 72		}
 73
 74		@Override
 75		public int getCount() {
 76			return images.length;
 77		}
 78
 79		@Override
 80		public Object instantiateItem(View view, int position) {
 81			final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
 82			final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
 83			final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
 84
 85			imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {
 86				@Override
 87				public void onLoadingStarted() {
 88					spinner.setVisibility(View.VISIBLE);
 89				}
 90
 91				@Override
 92				public void onLoadingFailed(FailReason failReason) {
 93					String message = null;
 94					switch (failReason) {
 95						case IO_ERROR:
 96							message = "Input/Output error";
 97							break;
 98						case OUT_OF_MEMORY:
 99							message = "Out Of Memory error";
100							break;
101						case UNKNOWN:
102							message = "Unknown error";
103							break;
104					}
105					Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();
106
107					spinner.setVisibility(View.GONE);
108					imageView.setImageResource(android.R.drawable.ic_delete);
109				}
110
111				@Override
112				public void onLoadingComplete(Bitmap loadedImage) {
113					spinner.setVisibility(View.GONE);
114					Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
115					imageView.setAnimation(anim);
116					anim.start();
117				}
118
119				@Override
120				public void onLoadingCancelled() {
121					// Do nothing
122				}
123			});
124
125			((ViewPager) view).addView(imageLayout, 0);
126			return imageLayout;
127		}
128
129		@Override
130		public boolean isViewFromObject(View view, Object object) {
131			return view.equals(object);
132		}
133
134		@Override
135		public void restoreState(Parcelable state, ClassLoader loader) {
136		}
137
138		@Override
139		public Parcelable saveState() {
140			return null;
141		}
142
143		@Override
144		public void startUpdate(View container) {
145		}
146	}
147}