public class MainActivity extends Activity {private Gallery gallery;private LayoutInflater inflater;private ImageSwitcher imageSwitcher;private int res[] = new int[] { R.drawable.ic_launcher, R.drawable.an01,R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,R.drawable.an03 };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);/** * 系统中有没有合适的适配器 */gallery = (Gallery) findViewById(R.id.gallery1);// 获取ImageView控件imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);// 初始化一次工厂即可imageSwitcher.setFactory(new ImageSwitcher.ViewFactory() {// 设置图片@Overridepublic View makeView() {ImageView imageView = new ImageView(MainActivity.this);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageView;}});// 加入动画imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));imageSwitcher.setOutAnimation(MainActivity.this,android.R.anim.fade_out);List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();for (int i = 0; i < res.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("imageView", res[i]);data.add(map);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,R.layout.activity_cell, new String[] { "imageView" },new int[] { R.id.imageView1 });gallery.setAdapter(simpleAdapter);// 注册事件gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 获取图片资源int imageResId = res[position % res.length];// 设置imageSwithcer组件资源imageSwitcher.setImageResource(imageResId);}});}/*public class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE;}@Overridepublic Object getItem(int position) {return res[position % res.length];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = inflater.inflate(R.layout.activity_gallery, null);ImageView iv = (ImageView) v.findViewById(R.id.imageView1);int imageResId = res[position % res.length];System.out.println(position + "-------" + imageResId);iv.setImageResource(imageResId);return v;}}*/}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="0dp" />    <ImageSwitcher        android:id="@+id/imageSwitcher1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </ImageSwitcher></LinearLayout>


activity_cell.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" />    </LinearLayout>


activity_gallery.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/an01" /></LinearLayout>


更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. android 多点触摸实例
  3. android 导航总结
  4. Android(安卓)播放网络视频
  5. Android(安卓)代码设置RelativeLayout元素居中
  6. Android实时获得经纬度,发送给c++服务端
  7. android Preference Demo
  8. android - Shader 着色器 翻译
  9. Android全屏设置

随机推荐

  1. android利用广播和服务实现一个杀不死的
  2. Android跟软键盘的故事
  3. Android(安卓)framework Watchdog的监控
  4. Android×××方法详解
  5. Android(安卓)P 系统设置默认值
  6. Android(安卓)进阶16:IntentService 使用
  7. Android五大UI布局的特有属性
  8. YAHFA hook的简单实例 ,hook自己的android
  9. Android(安卓)WakeLock
  10. Android学习心得(六)——位置服务