xml布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.example.android.gallery.MainActivity" > 7 8 <Gallery 9 android:id="@+id/gallery" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content"/> 12 <ImageSwitcher 13 android:id="@+id/is" 14 android:layout_width="match_parent" 15 android:layout_height="0dp" 16 android:layout_weight="1" 17 /> 18 </LinearLayout>
自定义适配器:
1 package com.example.android.gallery; 2 3 import android.content.Context; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.Gallery; 8 import android.widget.ImageView; 9 import android.widget.ImageView.ScaleType; 10 11 public class MyAdapter extends BaseAdapter { 12 private int[] res; 13 private Context context; 14 15 public MyAdapter(int[] res, Context context) { 16 this.res = res; 17 this.context = context; 18 } 19 20 @Override 21 public int getCount() { 22 // TODO Auto-generated method stub 23 return Integer.MAX_VALUE;//设置数量为整型的最大值,使图片循环播放 24 } 25 26 @Override 27 public Object getItem(int position) { 28 // TODO Auto-generated method stub 29 return res[position]; 30 } 31 32 @Override 33 public long getItemId(int position) { 34 // TODO Auto-generated method stub 35 return position; 36 } 37 38 @Override 39 public View getView(int position, View convertView, ViewGroup parent) { 40 ImageView image = new ImageView(context); 41 image.setBackgroundResource(res[position%res.length]);//设置背景图片无限循环 42 image.setLayoutParams(new Gallery.LayoutParams(400, 300));//设置缩略图的大小 43 image.setScaleType(ScaleType.FIT_XY);//设置按比例缩放 44 return image; 45 } 46 47 }
MainActivity类:
1 package com.example.android.gallery; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationUtils; 10 import android.widget.AdapterView; 11 import android.widget.AdapterView.OnItemSelectedListener; 12 import android.widget.Gallery; 13 import android.widget.ImageSwitcher; 14 import android.widget.ImageView; 15 import android.widget.ImageView.ScaleType; 16 import android.widget.ViewSwitcher.ViewFactory; 17 /** 18 * ImageSwitcher:和ImageView的功能类似,都适用与显示图片, 19 * 区别:ImageSwitcher的效果更炫,它可以指定图片切换的动画效果 20 * 21 * ImageSwticher可以粗略理解为ImageView的选择器,需要设置ViewFactory, 22 * 一般来说,会把ViewFactory的makeView()方法,返回ImageView。 23 * @author Administrator 24 * 25 */ 26 public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{ 27 //准备数据源 28 private int[] res = {R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4, 29 R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8, 30 R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12}; 31 private Gallery gallery; 32 private ImageSwitcher is; 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 gallery = (Gallery) findViewById(R.id.gallery); 38 //自定义适配器 39 MyAdapter adapter = new MyAdapter(res,this); 40 //为gallery设置适配器 41 gallery.setAdapter(adapter); 42 //为gallery设置监听器,用来监听Gallery选中的图片 43 gallery.setOnItemSelectedListener(this); 44 45 is = (ImageSwitcher) findViewById(R.id.is); 46 is.setFactory(this);//为imageSwitcher设置视图工厂 47 //为imageSwitcher设置动画效果 48 is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 49 is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 50 } 51 @Override 52 public View makeView() { 53 // TODO Auto-generated method stub 54 ImageView image = new ImageView(this); 55 image.setScaleType(ScaleType.FIT_CENTER);//使图片按比例缩放并居中 56 return image; 57 } 58 @Override 59 public void onItemSelected(AdapterView<?> parent, View view, int position, 60 long id) { 61 // TODO Auto-generated method stub 62 is.setBackgroundResource(res[position%res.length]); 63 } 64 @Override 65 public void onNothingSelected(AdapterView<?> parent) { 66 // TODO Auto-generated method stub 67 68 } 69 }