xml文件
1 210 17 18 26
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_0711 12 };13 14 ImageSwitcher switcher ; //声明ImageSwitcher对象,图片显示区域15 Gallery gallery; //声明Gallery对象,图片列表索引16 @Override17 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.LayoutParams32 // (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的adapter44 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 }