Image Switcher View


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Gallery>

    <ImageSwitcher
        android:id="@+id/ImageSwitcher01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ImageSwitcher>

</LinearLayout>




ImageSwitcherView.java


package moor.android.switcher;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherView extends Activity implements ViewFactory {

    Integer pics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
            R.drawable.e };

    ImageSwitcher iSwitcher;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
        iSwitcher.setFactory(this);
        iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        Gallery gallery = (Gallery) findViewById(R.id.Gallery01);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                iSwitcher.setImageResource(pics[arg2]);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        private Context ctx;

        public ImageAdapter(Context c) {
            ctx = c;
        }

        @Override
        public int getCount() {

            return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            ImageView iView = new ImageView(ctx);
            iView.setImageResource(pics[arg0]);
            iView.setScaleType(ImageView.ScaleType.FIT_XY);
            iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
            return iView;
        }

    }

    @Override
    public View makeView() {
        ImageView iView = new ImageView(this);
        iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        iView.setBackgroundColor(0xFF000000);
        return iView;
    }
}




Screen shot :






8 comments:

  1. to nice.. its realy helpfull for me ....
    i need help if possible for you i am new for iPhone app development give me some suggestion how i am starting iPhone app development....

    ReplyDelete
  2. to nice.. its realy helpfull for me ....
    i need help if possible for you i am new for iPhone app development give me some suggestion how i am starting iPhone app development....

    ReplyDelete
    Replies
    1. thanks for your feedback....iphone is very easy to learn......
      be in touch going to launch new blog for iphone thanks....

      Delete
  3. when will u post the iphone tutorials???? I am eagerly waiting for it....

    ReplyDelete
  4. hi very nice tutorial..
    i just wanted to know whether we can insert the left and right gesture effect to switch images back and forth??
    i am talking about the big image below the row of small images can we insert gesture effect in that area to change images.
    i am new to android development so help will be highly appreciated..

    ReplyDelete
  5. Respect! I have lost count of the examples I have downloaded and tried to get working to do this - I must be well into double figures.This is the first example that actually works. No errors in Eclipse. worked first time in Kindle Fire emulator.

    ReplyDelete
  6. how to use an imageswitcher using timer or with time !!!

    ReplyDelete