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代码:
package gdp.switcherview2; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
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.Toast;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity {
//声明一个数组,用来装载图片资源
int[] imageIds = new int[]{R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu, R.drawable.juxie,
R.drawable.mojie, R.drawable.sheshou, R.drawable.shizi, R.drawable.shuangyu,
R.drawable.shuangzi, R.drawable.shuiping, R.drawable.tiancheng, R.drawable.tianxie};
//声明ImageSwitcher对象
private ImageSwitcher switcher ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //创建一个List对象,其元素Map
List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
for(int i = 0; i<imageIds.length; i++){
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
} //获取显示图片的ImageSwitcher
switcher = (ImageSwitcher)findViewById(R.id.switcher);
//为imageSwicher设置图片切换的动画效果
switcher.setFactory(new ViewFactory() { @Override
public View makeView() {
// 创建imageView对象
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//返回ImageView对象
return imageView;
}
});
//创建Adapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});
GridView grid = (GridView)findViewById(R.id.grid01);
grid.setAdapter(simpleAdapter);
//添加列表项被点击的监听器
grid.setOnItemClickListener(new ItemClick());
//添加列表项被选中的监听器
grid.setOnItemSelectedListener(new ItemSelect());
} class ItemClick implements OnItemClickListener{ @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switcher.setImageResource(imageIds[arg2]);
} } class ItemSelect implements OnItemSelectedListener{ @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switcher.setImageResource(imageIds[arg2]);
} @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Nothing Selected" , Toast.LENGTH_LONG).show();
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
2.xml文件
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<!-- 定义一个GridView组件 -->
<GridView
android:id="@+id/grid01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:numColumns="4"
android:gravity="center"
/>
<!-- 定义一个ImageSwitcher组件 -->
<ImageSwitcher
android:id="@+id/switcher"
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"
/>
</LinearLayout>
cell.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
/>
</LinearLayout>
ImageSwitcher (图像切换器,显示图片)的更多相关文章
- 基于图像切换器(imageSwitcher)的支持动画的图片浏览器
利用GridView和ImageSwitcher的基本用法 public class MainActivity extends Activity { int[] imageIds = new int[ ...
- android学习之--网格视图(GridView)和图像切换器(ImageSwitcher)
GridView用于在界面上按行.列分布显示多个组件.GridView和ListView有共同父类:AbsListView. GridView与ListView的差别在于:ListV ...
- 图像切换器(ImageSwitcer)的功能与用法
ImageSwitcher继承了VewSwitcher,因此它具有与ViewSwitcher相同的特征,可以在切换View组件时使用动画效果.ImageSwitcher继承了ViewSwitcher并 ...
- Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法
网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...
- Android 高级UI设计笔记12:ImageSwitcher图片切换器
1. ImageSwitcher ImageSwitcher是Android中控制图片展示效果的一个控件,如:幻灯片效果...,颇有感觉啊.做相册一绝 2. 重要方法 setImageURI(Uri ...
- 图片切换器(ImageSwitcher)的功能与用法
ImageSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.ImageSwitcher继承了ViewSwitcher ...
- ImageSwitcher 图片切换器
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android应用开发学习之图片切换器
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 如果我们要实现类似Windows的照片查看器切换上一张下一张照片的效果,可以使用图片切换器ImageSwitcher ...
- [Android] 给图像加入相框、圆形圆角显示图片、图像合成知识
前一篇文章讲述了Android触屏setOnTouchListener实现突破缩放.移动.绘制和加入水印,继续我的"随手拍"项目完毕给图片加入相框.圆形圆角显示图片和图像合 ...
随机推荐
- Ubuntu虚拟机编译Android6.0总结
1 前言 昨天使用清华的源下载了android 6.0的源码,校园网可以达到10M的速度,爽!今天一大早就迫不及待地准备编译一个模拟器版本,看看效果,哪知竟然耗费了一整天的时间才搞定...为了避免其他 ...
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...
- error LNK2001: 无法解析的外部符号 "public: virtual long __stdcall CBaseFilter(转)
原文转自 https://www.cnblogs.com/xiongjiaji/archive/2010/12/31/2476565.html 今天用VS2005编译DirectShow程序,发现出来 ...
- switch与if 性能测试
测试结果:switch性能更高. 测试过程:新建一个Win32 Console Application, 在cpp文件中添加下面代码 #include "stdafx.h" #in ...
- Linux USB驱动框架分析【转】
转自:http://blog.csdn.net/jeffade/article/details/7701431 Linux USB驱动框架分析(一) 初次接触和OS相关的设备驱动编写,感觉还挺有意思的 ...
- 考勤的lua脚本
ngx.header.content_type = "text/plain;charset=utf-8" local cjson = require "cjson&quo ...
- MVC中@Html.Action的用法(类似自定义控件)
MVC项目中如果有公共部分的代码就可以单独拿出来作为控件来用(比如头部和底部代码).跟ASP.NET中的ASCX实现的效果一样,但MVC比它方便的多. 一.@Html.Action的用法 @Html. ...
- springBoot Ribbon Hystrix Dashboard
1.引入依赖 <!-- 引入关于 hystrix Dashboard的依赖 --> <dependency> <groupId>org.springframewor ...
- [BZOJ1017][JSOI2008]魔兽地图DotR 树形dp
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 2597 Solved: 1010[Submit][ ...
- Nginx配置文件分析
#user nobody; #启动进程数,即启动ngnix服务的个数,通常设置和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/e ...