首页 > 代码库 > ImageSwitcher (图像切换器,显示图片)
ImageSwitcher (图像切换器,显示图片)
ImageSwitcher继承了ViewSwitcher,主要在切换图片时加入动画效果
使用方法:
1.为ImageSwitcher提供一个ViewFactory,该ViewFactory生成的View组件必须是ImageView
2.切换图片时,用到的3个方法:imageSwitcher.setImageDrawable(Drawable drawable);
imageSwitcher.setImageResource(int resid);
imageSwitcher.setImageURI(URI uri);
1.java代码:
1 package gdp.switcherview2; 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.view.Menu; 11 import android.view.View; 12 import android.view.ViewGroup.LayoutParams; 13 import android.widget.AdapterView; 14 import android.widget.AdapterView.OnItemClickListener; 15 import android.widget.AdapterView.OnItemSelectedListener; 16 import android.widget.GridView; 17 import android.widget.ImageSwitcher; 18 import android.widget.ImageView; 19 import android.widget.SimpleAdapter; 20 import android.widget.Toast; 21 import android.widget.ViewSwitcher.ViewFactory; 22 23 public class MainActivity extends Activity { 24 //声明一个数组,用来装载图片资源 25 int[] imageIds = new int[]{R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu, R.drawable.juxie, 26 R.drawable.mojie, R.drawable.sheshou, R.drawable.shizi, R.drawable.shuangyu, 27 R.drawable.shuangzi, R.drawable.shuiping, R.drawable.tiancheng, R.drawable.tianxie}; 28 //声明ImageSwitcher对象 29 private ImageSwitcher switcher ; 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.main); 34 35 //创建一个List对象,其元素Map 36 List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>(); 37 for(int i = 0; i<imageIds.length; i++){ 38 Map<String, Object> listItem = new HashMap<String, Object>(); 39 listItem.put("image", imageIds[i]); 40 listItems.add(listItem); 41 } 42 43 //获取显示图片的ImageSwitcher 44 switcher = (ImageSwitcher)findViewById(R.id.switcher); 45 //为imageSwicher设置图片切换的动画效果 46 switcher.setFactory(new ViewFactory() { 47 48 @Override 49 public View makeView() { 50 // 创建imageView对象 51 ImageView imageView = new ImageView(MainActivity.this); 52 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 53 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, 54 LayoutParams.WRAP_CONTENT)); 55 //返回ImageView对象 56 return imageView; 57 } 58 }); 59 //创建Adapter 60 SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1}); 61 GridView grid = (GridView)findViewById(R.id.grid01); 62 grid.setAdapter(simpleAdapter); 63 //添加列表项被点击的监听器 64 grid.setOnItemClickListener(new ItemClick()); 65 //添加列表项被选中的监听器 66 grid.setOnItemSelectedListener(new ItemSelect()); 67 } 68 69 class ItemClick implements OnItemClickListener{ 70 71 @Override 72 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 73 long arg3) { 74 // TODO Auto-generated method stub 75 switcher.setImageResource(imageIds[arg2]); 76 } 77 78 } 79 80 class ItemSelect implements OnItemSelectedListener{ 81 82 @Override 83 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 84 long arg3) { 85 // TODO Auto-generated method stub 86 switcher.setImageResource(imageIds[arg2]); 87 } 88 89 @Override 90 public void onNothingSelected(AdapterView<?> arg0) { 91 // TODO Auto-generated method stub 92 Toast.makeText(MainActivity.this, "Nothing Selected" , Toast.LENGTH_LONG).show(); 93 } 94 95 } 96 97 @Override 98 public boolean onCreateOptionsMenu(Menu menu) { 99 // Inflate the menu; this adds items to the action bar if it is present. 100 getMenuInflater().inflate(R.menu.main, menu); 101 return true; 102 } 103 104 }
2.xml文件
main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" 5 android:gravity="center_horizontal" > 6 <!-- 定义一个GridView组件 --> 7 <GridView 8 android:id="@+id/grid01" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:horizontalSpacing="2dp" 12 android:verticalSpacing="2dp" 13 android:numColumns="4" 14 android:gravity="center" 15 /> 16 <!-- 定义一个ImageSwitcher组件 --> 17 <ImageSwitcher 18 android:id="@+id/switcher" 19 android:layout_width="300dp" 20 android:layout_height="300dp" 21 android:layout_gravity="center_horizontal" 22 android:inAnimation="@android:anim/fade_in" 23 android:outAnimation="@android:anim/fade_out" 24 /> 25 </LinearLayout>
cell.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 <ImageView 6 android:id="@+id/image1" 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:scaleType="fitXY" 10 /> 11 </LinearLayout>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。