xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/launch_linear" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 android:background="@color/halftransparent" 8 android:orientation="vertical" 9 > 10 <ImageSwitcher 11 android:id="@+id/switcher" 12 android:layout_width="320dp" 13 android:layout_height="400dp" 14 android:background="@color/pink" 15 android:gravity="center" 16 /> 17 18 <Gallery 19 android:id="@+id/gallery" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:background="@color/blue" 23 android:layout_marginTop="10dip" 24 android:unselectedAlpha="0.6" 25 android:spacing="2pt"/> 26 </LinearLayout>
Java文件
1 public class ChangeTheme extends Activity { 2 //定义图片资源 3 private int[] images = new int[]{ 4 R.drawable.paper_01, 5 R.drawable.paper_02, 6 R.drawable.paper_03, 7 R.drawable.paper_04, 8 R.drawable.paper_05, 9 R.drawable.paper_06, 10 R.drawable.paper_07 11 12 }; 13 14 ImageSwitcher switcher ; //声明ImageSwitcher对象,图片显示区域 15 Gallery gallery; //声明Gallery对象,图片列表索引 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.theme_activity); 20 21 //获取对象 22 switcher = (ImageSwitcher)this.findViewById(R.id.switcher); 23 gallery = (Gallery)this.findViewById(R.id.gallery); 24 25 26 switcher.setFactory(new ViewFactory(){ 27 public View makeView() { 28 ImageView imageView = new ImageView(ChangeTheme.this); 29 imageView.setBackgroundColor(0x00ff00); 30 imageView.setScaleType(ImageView.ScaleType.CENTER); 31 // imageView.setLayoutParams(new ImageSwitcher.LayoutParams 32 // (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 33 34 imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 35 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 36 return imageView; 37 } 38 }); 39 //设置切换动画效果 40 switcher.setInAnimation(this, android.R.anim.slide_in_left); 41 switcher.setOutAnimation(this, android.R.anim.fade_out); 42 43 //创建Gallery的adapter 44 BaseAdapter adapter = new BaseAdapter(){ 45 public int getCount() { 46 return images.length; 47 } 48 public Object getItem(int position) { 49 return images[position]; 50 } 51 public long getItemId(int position) { 52 return position; 53 } 54 //该方法返回的view代表每个列表项 55 56 public View getView(int position, View convertView, ViewGroup parent) { 57 ImageView imageView = new ImageView(ChangeTheme.this); 58 imageView.setImageResource(images[position%images.length]); 59 imageView.setScaleType(ScaleType.FIT_XY); 60 imageView.setLayoutParams(new Gallery.LayoutParams(75, 100)); 61 //设置自定义属性 62 /*TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery); 63 imageView.setBackgroundResource(typeArray.getResourceId( 64 R.styleable.Gallery_android_galleryItemBackground, 0));*/ 65 return imageView; 66 } 67 }; 68 69 gallery.setAdapter(adapter); 70 //Gallery便表现选择时间 71 gallery.setOnItemSelectedListener(new OnItemSelectedListener(){ 72 public void onItemSelected(AdapterView<?> parent, View v, int position,long id) { 73 switcher.setImageResource(images[position % images.length]); 74 } 75 public void onNothingSelected(AdapterView<?> arg0) {} 76 }); 77 } 78 79 }