android ImageSwitcher
2015-09-14 10:59
585 查看
使用ImageSwitcher要两个步骤:
1、为ImageSwitcher提供一个ViewFactory,改factory生成的View组件必须是ImageView
2、切换图片时,调用setImageDrawble或者setImageResourece即可
附录:布局文件
1、为ImageSwitcher提供一个ViewFactory,改factory生成的View组件必须是ImageView
2、切换图片时,调用setImageDrawble或者setImageResourece即可
<span style="font-size:14px;">import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ActionBar.LayoutParams; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.GridView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.ViewSwitcher.ViewFactory; import com.example.android_lb_day3_ui.R; public class ImageSwitcherTest extends Activity { int imgIds[] = new int[] { R.drawable.animal_01, R.drawable.animal_02, R.drawable.animal_03, R.drawable.animal_04, R.drawable.animal_05, R.drawable.animal_01, R.drawable.animal_02, R.drawable.animal_03, R.drawable.animal_04, R.drawable.animal_05, R.drawable.animal_01, R.drawable.animal_02, }; ImageSwitcher switcher; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_switcher); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imgIds.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("image", imgIds[i]); list.add(map); } switcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); // 为ImageSwitcher设置切换的动画效果 switcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView image = new ImageView(ImageSwitcherTest.this); image.setScaleType(ImageView.ScaleType.FIT_CENTER);// 设置图片缩放比例 image.setLayoutParams(new ImageSwitcher.LayoutParams( 160, 160)); return image; } }); SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.swticher_img, new String[] { "image" }, new int[] { R.id.swticher_img }); GridView grid = (GridView) findViewById(R.id.gridView); grid.setAdapter(adapter); // 被选中的事件 grid.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { switcher.setImageResource(imgIds[position]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); // 呗单机的事件 grid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switcher.setImageResource(imgIds[position]); } }); } }</span>
附录:布局文件
<?xml version="1.0" encoding= "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <GridView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="pt" android:numColumns="4" android:verticalSpacing="2pt" android:id="@+id/gridView" > </GridView > <ImageSwitcher android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" android:id="@+id/imageSwitcher" > </ImageSwitcher > </LinearLayout>
相关文章推荐
- Android基础控件ToggleButton→ Switch→ SeekBar→ProgressBar 控件
- android ViewSwticher
- phoneGap(cordova)插件开发学习
- Android之日期及时间选择对话框用法实例分析
- Android-RoundCornerProgressBar——Android圆角进度条(Progress Bar)类库
- Android学习文档之秘密发送短信
- Argument for @NotNull parameter 'name' of com/android/tools/idea/welcome/Platform.<init> must not be
- Android自定义ProgressBar
- [转]Android开发过程中遇到的问题
- Android系统自带样式(android:theme)解析
- Android RadioGroup 横向显示
- 使用android NDK r10e gdb 出现识别不了ABI的问题
- Android code-工具类库-GPS、百度、高德、国测局、腾讯等经纬度坐标转换
- android中点击空白处隐藏软键盘
- Android 如何使用GPU硬件加速
- Android项目模块化介绍——用注解使各个模块间不再依赖
- Android 系统服务
- Android TagFlowLayout完全解析 一款针对Tag的布局(转)
- 下载Android 5.0源码(附源码)
- 使用ThinkAndroid快速开发框架需要有以下权限