Sunday, June 3, 2012

Android ImageSwitcher Example




STEP 1: Creating a new project from File->New->Android Project with ImageSwitcherExample Activity Name

STEP 2: Copy Image files to res/drawable Folder

STEP 3: In res/layout/main.xm layout add ImageSwitcher and Gallery View as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageSwitcher 
        android:layout_width="fill_parent"  
        android:id="@+id/imgswitcher" 
        android:layout_height="fill_parent"  
        android:layout_alignParentTop="true" 
        android:layout_alignParentLeft="true" />    
    <Gallery android:id="@+id/galleryview" 
        android:layout_width="fill_parent"  
        android:layout_height="60dip" 
        android:background="#55000000"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentBottom="true" 
        android:gravity="center_vertical"  
        android:spacing="16dip" /> 
</RelativeLayout>

STEP 4: Create an ImageAdapter which extends BaseAdapter as:
package com.imrankhanandroid.Imageswitcherexp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	private Context context;  
    public static Integer[] imageIDs={  
            R.drawable.a01,R.drawable.a02, 
            R.drawable.a03,R.drawable.a04, 
            R.drawable.a05,R.drawable.a06, 
            R.drawable.a07,R.drawable.a08, 
            R.drawable.a09,R.drawable.a010, 
            R.drawable.a011,R.drawable.a012, 
            R.drawable.a013
    };  
    public ImageAdapter(Context context){  
        this.context=context;  
    }  
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return imageIDs.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View 
			convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView image=new ImageView(context);  
        image.setImageResource(imageIDs[position]);  
        image.setAdjustViewBounds(true);  
        image.setLayoutParams(new Gallery.LayoutParams(120,120));  
        image.setScaleType(ImageView.ScaleType.FIT_CENTER);  
        return image;  
	}

}

STEP 5: In ImageSwitcherExampleActivity implements ViewFactory and OnItemSelectedListener. as:
package com.imrankhanandroid.Imageswitcherexp;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherExampleActivity extends Activity 
    implements ViewFactory,OnItemSelectedListener {
    private ImageSwitcher mSwitcher;  
    private Gallery mGallery;  
    private int selectedTag = 0;  
    private int upX, downX;  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.
        LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        setTitle("ImageSwitcher Example");  
        mSwitcher = (ImageSwitcher) findViewById(R.id.imgswitcher);  
        mSwitcher.setFactory(ImageSwitcherExampleActivity.this);  
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_in));  
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_out));  
        mSwitcher.setOnTouchListener(touchlistener);  
        mGallery = (Gallery) findViewById(R.id.galleryview);  
        mGallery.setAdapter(new ImageAdapter(ImageSwitcherExampleActivity.this));  
        mGallery.setOnItemSelectedListener(this);  
       
    }
    OnTouchListener touchlistener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if(event.getAction()==MotionEvent.ACTION_DOWN){
				downX=(int)event.getX();//Get Pressed Coordinates x
				return true;
			}
			else{
				if(event.getAction()==MotionEvent.ACTION_UP){
					upX=(int)event.getX();//get Released Coordinates x
					if(upX-downX>100){ //From left to right drag
						  //if this is first drag to to tail
						if(mGallery.getSelectedItemPosition()==0){
							selectedTag=mGallery.getCount()-1;
						}
						else
						{
							selectedTag=mGallery.getSelectedItemPosition()-1;
						}
					}
					else{
						  if(downX-upX>100){ //From right to left drag
							  if (mGallery.getSelectedItemPosition() == (mGallery  
			                            .getCount() - 1))  
			                        selectedTag = 0;  
			                    else  
			                        selectedTag = mGallery.getSelectedItemPosition() + 1;
						  }
					}
					return true;
				}
			}
			return false;
		} 
    	
    };
	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		ImageView iv=new ImageView(this);
		iv.setBackgroundColor(0xFF000000);
		iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
		iv.setLayoutParams(new ImageSwitcher.LayoutParams(  
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
		return iv;
	}

	@Override
	public void onItemSelected(AdapterView arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		 mSwitcher.setImageResource(ImageAdapter.imageIDs[arg2]);  
	     selectedTag = arg2;  
	}

	@Override
	public void onNothingSelected(AdapterView arg0) {
		// TODO Auto-generated method stub
		
	}
}

Download Source Code: ImageSwitcherExample









0 comments:

Post a Comment

 
Powered by Blogger