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
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
great example. Do you know a way to pass the imageIDs from another intent to the ImageSwitcher? That way the images that would be displayed could be set dependent on which intent is calling it?
ReplyDeleteBeen stuck on this for a while....
thanks so much!!!
ReplyDeletethx grt work
ReplyDelete