imageswitcher继承自viewswitcher,使用ImageSwither只需要两步:
1.为ImageSwither提供一个ViewFactory,该ViewFactory生成的View必须是ImageView.
2.需要切换图片时,只要调用ImageSwither的setImageResource,setImageDrawable,setImageURI方法更换图片即可。
下面是一个gallery,上面是个imageSwither,在gallery里每选择一张图片就会显示到Swither上,并有渐入渐出动画效果。
ImageSwitcher + GridView
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.view.ViewGroup; 5 import android.widget.AdapterView; 6 import android.widget.AdapterView.OnItemClickListener; 7 import android.widget.BaseAdapter; 8 import android.widget.FrameLayout.LayoutParams; 9 import android.widget.GridView; 10 import android.widget.ImageSwitcher; 11 import android.widget.ImageView; 12 import android.widget.ViewSwitcher.ViewFactory; 13 14 public class ImageSwitcherTest extends Activity { 15 private GridView gridView; 16 private ImageSwitcher switcher; 17 18 private int[] mImage = new int[] { R.drawable.p0, R.drawable.p13, 19 R.drawable.p16, R.drawable.p2, R.drawable.p4, R.drawable.p8 }; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.layout_imageswitcher); 26 switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher2); 27 28 switcher.setFactory(new ViewFactory() { 29 30 @Override 31 public View makeView() { 32 // TODO Auto-generated method stub 33 34 ImageView imageView = new ImageView(ImageSwitcherTest.this); 35 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 36 /*imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 37 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));*/ 38 return imageView; 39 } 40 }); 41 42 ImageAdapter adapter = new ImageAdapter(); 43 gridView = (GridView) findViewById(R.id.gridView2); 44 gridView.setAdapter(adapter); 45 46 gridView.setOnItemClickListener(new OnItemClickListener() { 47 48 @Override 49 public void onItemClick(AdapterView<?> parent, View view, 50 int position, long id) { 51 // TODO Auto-generated method stub 52 switcher.setImageResource(mImage[position]); 53 } 54 }); 55 56 } 57 58 class ImageAdapter extends BaseAdapter { 59 60 @Override 61 public int getCount() { 62 // TODO Auto-generated method stub 63 return mImage.length; 64 } 65 66 @Override 67 public Object getItem(int position) { 68 // TODO Auto-generated method stub 69 return mImage[position]; 70 } 71 72 @Override 73 public long getItemId(int position) { 74 // TODO Auto-generated method stub 75 return position; 76 } 77 78 @Override 79 public View getView(int position, View convertView, ViewGroup parent) { 80 // TODO Auto-generated method stub 81 ImageView imageView = new ImageView(ImageSwitcherTest.this); 82 imageView.setImageResource(mImage[position]); 83 return imageView; 84 } 85 86 } 87 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <GridView 8 android:id="@+id/gridView2" 9 android:layout_width="match_parent" 10 android:layout_height="254dp" 11 android:numColumns="3" 12 android:horizontalSpacing="pt" 13 android:verticalSpacing="2pt" 14 android:gravity="center" 15 > 16 </GridView> 17 18 <ImageSwitcher 19 android:id="@+id/imageSwitcher2" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:layout_weight="0.94" 23 android:layout_gravity="center_horizontal" 24 android:inAnimation="@android:anim/fade_in" 25 android:outAnimation="@android:anim/fade_out" 26 > 27 </ImageSwitcher> 28 29 </LinearLayout>