In my last blog I have described about how you can create a TextSwitcher. So in continuation of that in this blog I will help you in craeteing a ImageSwitcher. 
Firtsl of all you shoud know what ImageSwitcher is, ImageSwitcher a class which allows us to add some transition on the images through the way they appear on screen. In below example Frist I have created a main layout,In main layout I have added ImageSwitcher attributes. See the below example code it will clearly described you how to make ImageSwitcher.
Step(1)-MainActivity-
public class MainActivity extends Activity {
        ImageSwitcher imageSwitcher = null;
        Button btnNext;
        final int imageIds[] = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
        final int messageCount = imageIds.length;
        final int[] currentIndex = {-1};
        @Override
        public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnNext = (Button) findViewById(R.id.buttonNext);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            public View makeView() {
                ImageView imageView = new ImageView(getApplicationContext());
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
                return imageView;
            }
        });
        // Declare the animations and initialize them
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        imageSwitcher.setInAnimation(in);
        imageSwitcher.setOutAnimation(out);
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentIndex[0]++;
                if (currentIndex[0] == messageCount)
                    currentIndex[0] = 0;
                imageSwitcher.setImageResource(imageIds[currentIndex[0]]);
            }
        });
    }
}
Step(2)-main.xml-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />
    <Button
        android:id="@+id/buttonNext"
        android:layout_marginTop="150dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="NEXT"
        />
</LinearLayout>
                       
                    
0 Comment(s)