Image switcher which help to Switch between images in android.
public class ImageswitcherActivity extends Activity implements
ViewFactory
{
}
Here ViewFactory is the interface used in Image Switcher and also the interface provides the so many properties for images transaction like FADE IN FADE OUT etc...
The following Code illustrate how can we implement Image Switcher in android..
package a.b;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
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.ViewFactory;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageswitcherActivity extends Activity implements
ViewFactory {
// A reference to the images contained into the drawable folder
private
Integer[] m_ImageIds = {
R.drawable.ttttt,
R.drawable.tttttt,
R.drawable.gg,R.drawable.imagemmm,R.drawable.imagesh,R.drawable.imageww,
R.drawable.ff,R.drawable.ffff,R.drawable.fgg
};
// The ImageSwitcher
ImageSwitcher m_ImageSwitcher;
// The Handler used for manage the Runnable that switch the images
Handler m_Handler = new Handler();
// The index of the current image
int m_imageIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// assign the ImageSwitcher
m_ImageSwitcher = (ImageSwitcher)findViewById(R.id.img);
m_ImageSwitcher.setFactory(this);
// Create the Fade in animation
Animation fadeIn = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
fadeIn.setDuration(4000);
// Create the Fade out animation
Animation fadeOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
fadeOut.setDuration(4000);
// Assign the two animations to the ImageSwitcher
m_ImageSwitcher.setInAnimation(fadeIn);
m_ImageSwitcher.setOutAnimation(fadeOut);
// Start the Runnable
m_Handler.post(m_UpdateTimeTask);
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
/*
* Set the image to show into the ImageSwitcher, then post his execution after 5 seconds
*/
Runnable m_UpdateTimeTask = new Runnable() {
public void run() {
// Set the Image to show
m_ImageSwitcher.setImageResource(m_ImageIds[m_imageIndex]);
// Increment the index
m_imageIndex++;
// if necessary restart from the first image
if(m_imageIndex > (m_ImageIds.length-1)){
m_imageIndex = 0;
}
// Set the execution after 5 seconds
m_Handler.postDelayed(this, (5 * 1000));
}
};
}
Xml file which describe how can we Implement switcher in 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" >
<ImageSwitcher android:layout_margin="10dp"
android:layout_width="fill_parent"
android:id="@+id/img" android:layout_height="212dp">
</ImageSwitcher>
</LinearLayout>
DOWNLOAD THE FULL CODE