imageswitcher继承自viewswitcher,使用ImageSwither只需要两步:

1.为ImageSwither提供一个ViewFactory,该ViewFactory生成的View必须是ImageView.

2.需要切换图片时,只要调用ImageSwither的setImageResource,setImageDrawable,setImageURI方法更换图片即可。

下面是一个gallery,上面是个imageSwither,在gallery里每选择一张图片就会显示到Swither上,并有渐入渐出动画效果。

初识ImageSwither-LMLPHP

ImageSwitcher + GridView

初识ImageSwither-LMLPHP

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherTest extends Activity {
private GridView gridView;
private ImageSwitcher switcher; private int[] mImage = new int[] { R.drawable.p0, R.drawable.p13,
R.drawable.p16, R.drawable.p2, R.drawable.p4, R.drawable.p8 }; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_imageswitcher);
switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher2); switcher.setFactory(new ViewFactory() { @Override
public View makeView() {
// TODO Auto-generated method stub ImageView imageView = new ImageView(ImageSwitcherTest.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
/*imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));*/
return imageView;
}
}); ImageAdapter adapter = new ImageAdapter();
gridView = (GridView) findViewById(R.id.gridView2);
gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switcher.setImageResource(mImage[position]);
}
}); } class ImageAdapter extends BaseAdapter { @Override
public int getCount() {
// TODO Auto-generated method stub
return mImage.length;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mImage[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 imageView = new ImageView(ImageSwitcherTest.this);
imageView.setImageResource(mImage[position]);
return imageView;
} }
}
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <GridView
android:id="@+id/gridView2"
android:layout_width="match_parent"
android:layout_height="254dp"
android:numColumns="3"
android:horizontalSpacing="pt"
android:verticalSpacing="2pt"
android:gravity="center"
>
</GridView> <ImageSwitcher
android:id="@+id/imageSwitcher2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.94"
android:layout_gravity="center_horizontal"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
>
</ImageSwitcher> </LinearLayout>
05-21 10:35