Showing posts with label gallery. Show all posts
Showing posts with label gallery. Show all posts

Saturday, 16 April 2011

Android - Image Switcher Example

This example demonstrates the use of an ImageSwitcher paired with a Gallery. ImageSwitcher is used to switch between two ImageViews seamlessly by playing an animation during the transition.

main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   
      <Gallery  
           android:id="@+id/gallery"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content" />  
        
      <ImageSwitcher  
           android:id="@+id/image_switcher"  
           android:layout_width="fill_parent"  
           android:layout_height="fill_parent" />  
 </LinearLayout>

SwitcherActivity.java
 package com.mobsandgeeks.ise;  
   
 import android.app.Activity;  
 import android.content.Context;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.view.animation.AnimationUtils;  
 import android.widget.AdapterView;  
 import android.widget.AdapterView.OnItemSelectedListener;  
 import android.widget.BaseAdapter;  
 import android.widget.Gallery;  
 import android.widget.ImageSwitcher;  
 import android.widget.ImageView;  
 import android.widget.ViewSwitcher;  
   
 public class SwitcherActivity extends Activity implements ViewSwitcher.ViewFactory, OnItemSelectedListener {  
        
      private Gallery gallery;  
      private ImageSwitcher imageSwitcher;  
        
      private ImageAdapter ia;   
   
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
   
           gallery = (Gallery) findViewById(R.id.gallery);  
           imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);  
             
           imageSwitcher.setFactory(this);  
           imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                     android.R.anim.fade_in));  
           imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
                     android.R.anim.fade_out));  
             
   
           ia = new ImageAdapter(this);  
           gallery.setAdapter(ia);  
             
           //Event listener
           gallery.setOnItemSelectedListener(this);  
      }  
        
      private class ImageAdapter extends BaseAdapter {  
           private Context context;  
             
           private ImageAdapter(Context context) {  
                this.context = context;  
           }  
             
           private int[] IMAGE_IDS = {  
                R.drawable.android, R.drawable.gingerbread, R.drawable.honeycomb,  
                R.drawable.lg_optimus, R.drawable.nexus_one, R.drawable.nexus_s,  
                R.drawable.oha  
           };  
   
           public int getCount() {  
                return IMAGE_IDS.length;  
           }  
   
           public Object getItem(int position) {  
                return IMAGE_IDS[position];  
           }  
   
           public long getItemId(int position) {  
                return position;  
           }  
   
           public View getView(int position, View convertView, ViewGroup parent) {  
                ImageView iv = new ImageView(context);  
                iv.setImageResource(IMAGE_IDS[position]);  
                iv.setLayoutParams(new Gallery.LayoutParams(150, 100));  
                return iv;  
           }  
      }  
   
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
           int imageResourceId = (Integer) ia.getItem(position);  
           imageSwitcher.setImageResource(imageResourceId);  
      }  
   
      public void onNothingSelected(AdapterView<?> parent) {  
      }  
   
      public View makeView() {  
           ImageView i = new ImageView(this);  
           i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
           return i;  
      }  
 }  

Source Code
You can download the source code from here.

Screenshot