GridView应用2--(使用ImageSwitcher显示)
2012-04-28 14:43 ...平..淡... 阅读(406) 评论(0) 编辑 收藏 举报不多说,通过程序来演示...
注意:android:numColumns属性最好指定大于1,否则该属性默认值是1.这意味着只有一列,等同于ListView.
step1:新建android工程,命名为ImageSwitcherTest.
step2:设置配置文件
main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <GridView 8 android:id="@+id/gridview" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:numColumns="7" /> 12 13 <ImageSwitcher 14 android:id="@+id/imageswitcher" 15 android:layout_width="320dp" 16 android:layout_height="320dp" 17 android:layout_gravity="center_horizontal" /> 18 19 </LinearLayout>
main1.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/imageview" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>
step3:主Activity程序..
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.cb.imageswitcher; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.util.Log; 11 import android.view.View; 12 import android.view.ViewGroup.LayoutParams; 13 import android.view.animation.AnimationUtils; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemClickListener; 16 import android.widget.AdapterView.OnItemSelectedListener; 17 import android.widget.GridView; 18 import android.widget.ImageSwitcher; 19 import android.widget.ImageView; 20 import android.widget.SimpleAdapter; 21 import android.widget.ViewSwitcher.ViewFactory; 22 23 public class ImageSwitcherTest extends Activity { 24 private GridView mGridView; 25 private String TAG = "ImageSwitcherTest"; 26 private int[] images = { R.drawable.a, R.drawable.b, R.drawable.nash1, 27 R.drawable.nash2, R.drawable.nash3, R.drawable.nash4, 28 R.drawable.nash5 };// 加载图片 29 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.main); 34 Log.i(TAG, "onCreate()"); 35 36 mGridView = (GridView) findViewById(R.id.gridview); 37 38 List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();// 存储数据 39 40 for (int i = 0; i < images.length; i++) { 41 Map<String, Object> listItem = new HashMap<String, Object>(); 42 listItem.put("images", images[i]); 43 listItems.add(listItem); 44 } 45 46 /* 47 * ImageSwitcher由FrameLayout派生而出,ImageSwitcher组件和ImageView很相似,它们都可用于显示图片, 48 * 但ImageSwitcher比普通ImageView多一个功能:它所显示的图片切换时可以设置动画效果. 49 */ 50 final ImageSwitcher switcher = (ImageSwitcher) findViewById(R.id.imageswitcher); 51 52 // 设置图片更换的动画效果 53 switcher.setInAnimation(AnimationUtils.loadAnimation(this, 54 android.R.anim.fade_in)); 55 switcher.setOutAnimation(AnimationUtils.loadAnimation(this, 56 android.R.anim.fade_out)); 57 58 switcher.setFactory(new ViewFactory() { 59 60 @Override 61 public View makeView() { 62 ImageView imageView = new ImageView(ImageSwitcherTest.this); 63 imageView.setBackgroundColor(0Xff0000); 64 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 65 imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 66 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 67 68 return imageView; // ImageSwitcher负责显示返回的这个ImageView. 69 } 70 }); 71 72 SimpleAdapter adapter = new SimpleAdapter(this, listItems, 73 // 使用R.layout.main1作为布局文件. 74 R.layout.main1, new String[] { "images" }, 75 new int[] { R.id.imageview }); 76 77 mGridView.setAdapter(adapter); 78 79 // 添加列表项被选中的监听器 80 mGridView.setOnItemSelectedListener(new OnItemSelectedListener() { 81 82 @Override 83 public void onItemSelected(AdapterView<?> parent, View view, 84 int position, long id) { 85 // 显示当前被选中的图片 86 switcher.setImageResource(images[position % images.length]); 87 } 88 89 @Override 90 public void onNothingSelected(AdapterView<?> parent) { 91 } 92 }); 93 94 // 添加列表项被单击的监听器 95 mGridView.setOnItemClickListener(new OnItemClickListener() { 96 97 @Override 98 public void onItemClick(AdapterView<?> parent, View view, 99 int position, long id) { 100 switcher.setImageResource(images[position % images.length]); 101 } 102 }); 103 } 104 }
step4:图示结果(点击上面的图片,在下面的ImageSwitcher中切换显示)
continue my dream...