PageRenderTime 43ms CodeModel.GetById 36ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://github.com/rashedulkabir/Android-Universal-Image-Loader
Java | 103 lines | 83 code | 17 blank | 3 comment | 3 complexity | 2462ba1b38e7240f67f35be01a31f19a MD5 | raw file
  1package com.nostra13.example.universalimageloader;
  2
  3import android.content.Intent;
  4import android.graphics.Bitmap;
  5import android.os.Bundle;
  6import android.view.View;
  7import android.view.ViewGroup;
  8import android.view.animation.Animation;
  9import android.view.animation.AnimationUtils;
 10import android.widget.AdapterView;
 11import android.widget.AdapterView.OnItemClickListener;
 12import android.widget.BaseAdapter;
 13import android.widget.GridView;
 14import android.widget.ImageView;
 15
 16import com.nostra13.universalimageloader.core.DisplayImageOptions;
 17import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
 18
 19/**
 20 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 21 */
 22public class ImageGridActivity extends BaseActivity {
 23
 24	private String[] imageUrls;
 25
 26	private DisplayImageOptions options;
 27
 28	@Override
 29	public void onCreate(Bundle savedInstanceState) {
 30		super.onCreate(savedInstanceState);
 31		setContentView(R.layout.ac_image_grid);
 32
 33		Bundle bundle = getIntent().getExtras();
 34		imageUrls = bundle.getStringArray(Extra.IMAGES);
 35
 36		options = new DisplayImageOptions.Builder()
 37			.showStubImage(R.drawable.stub_image)
 38			.showImageForEmptyUri(R.drawable.image_for_empty_url)
 39			.cacheInMemory()
 40			.cacheOnDisc()
 41			.build();
 42
 43		GridView gridView = (GridView) findViewById(R.id.gridview);
 44		gridView.setAdapter(new ImageAdapter());
 45		gridView.setOnItemClickListener(new OnItemClickListener() {
 46			@Override
 47			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 48				startImageGalleryActivity(position);
 49			}
 50		});
 51	}
 52
 53	@Override
 54	protected void onStop() {
 55		imageLoader.stop();
 56		super.onStop();
 57	}
 58
 59	private void startImageGalleryActivity(int position) {
 60		Intent intent = new Intent(this, ImagePagerActivity.class);
 61		intent.putExtra(Extra.IMAGES, imageUrls);
 62		intent.putExtra(Extra.IMAGE_POSITION, position);
 63		startActivity(intent);
 64	}
 65
 66	public class ImageAdapter extends BaseAdapter {
 67		@Override
 68		public int getCount() {
 69			return imageUrls.length;
 70		}
 71
 72		@Override
 73		public Object getItem(int position) {
 74			return null;
 75		}
 76
 77		@Override
 78		public long getItemId(int position) {
 79			return position;
 80		}
 81
 82		@Override
 83		public View getView(int position, View convertView, ViewGroup parent) {
 84			final ImageView imageView;
 85			if (convertView == null) {
 86				imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
 87			} else {
 88				imageView = (ImageView) convertView;
 89			}
 90
 91			imageLoader.displayImage(imageUrls[position], imageView, options, new SimpleImageLoadingListener() {
 92				@Override
 93				public void onLoadingComplete(Bitmap loadedImage) {
 94					Animation anim = AnimationUtils.loadAnimation(ImageGridActivity.this, R.anim.fade_in);
 95					imageView.setAnimation(anim);
 96					anim.start();
 97				}
 98			});
 99
100			return imageView;
101		}
102	}
103}