Android ImageSwitcher和Gallery的使用
前几天,听说室友的老师要求他们做一个图片效果。其效果如下图所示(可左右滑动切换图片):

我当时晃眼一看,第一感觉好高级的样子。我还没做过这种效果呢,但室友说他们同学已经有人做出来了,我觉得既然有人做出来了,那么我也应该做出来,于是上个星期五的时候在教室研究了一下午,最后在网上找到了两个Demo都与这个效果类似但又不完全一样,果断下载下来研究研究,结果才发现其实这个效果并没有你想的那么难。
废话不多说,上代码:
main.xml:(其实就是两个控件的使用ImageSwitcher和Gallery)
<?xml version="1.0" encoding="utf-8"?> <!-- 相对布局 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 单个图片显示 -->
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<!-- 显示图片列表控件 -->
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dip" >
</Gallery> </RelativeLayout>
创建一个新的JAVA文件galleryImage.java(并在清单文件中设置为默认):
public class galleryImage extends Activity {
private Gallery gallery;
ImageSwitcher imageSwitcher; // 声明ImageSwitcher对象,图片显示区域
public List<Map<String, Object>> list;
private int[] myImageIds = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
R.drawable.i4 };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Init();
}
/**
* 初始化
*/
public void Init(){
// 通过控件的ID获得imageSwitcher的对象
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
// 设置自定义的图片显示工厂类
imageSwitcher.setFactory(new MyViewFactory(this));
Animation();
/*adapter = new SwitchereAdapter(this);
adapter.createReflectedImages(); // 创建倒影效果
gallery.setAdapter(adapter);*/
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// 通过求余数,循环显示图片
imageSwitcher.setImageResource(myImageIds[position
% myImageIds.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void ChangeScale(){
}
/**
* 动画效果
*/
public void Animation() {
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
/**
* Gallery的图片适配器
* @author AF74776
*
*/
public class ImageAdapter extends BaseAdapter {
// 声明一个变量
int mGalleryItemBackGround;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
// 实用布局文件中的Gallery属性
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
// 取得gallery属性饿index id
mGalleryItemBackGround = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
// 让对象的styleable属性能反复实用
a.recycle();
}
public int getCount() {// 返回数组长度
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {// 得到图片ID
return position;
}
public View getView(int position, View converView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
// i.setImageResource(myImageIds[position %myImageIds.length ]);
i.setImageResource(myImageIds[position % myImageIds.length]);
i.setAdjustViewBounds(true); // 图片自动调整显示
i.setScaleType(ImageView.ScaleType.FIT_XY);// 重新设置图片的宽高
i.setLayoutParams(new Gallery.LayoutParams(200, 200));// 设置layout的宽高
i.setBackgroundResource(mGalleryItemBackGround);// 设置背景
return i;// 返回对象
}
}
// 自定义图片显示工厂类,继承ViewFactory
class MyViewFactory implements ViewFactory {
private Context context; // 定义上下文
// 参数为上下文的构造方法
public MyViewFactory(Context context) {
this.context = context;
}
// 显示图标区域
public View makeView() {
ImageView iv = new ImageView(context); // 创建ImageView对象
iv.setScaleType(ImageView.ScaleType.FIT_CENTER); // 图片自动居中显示
// 设置图片的宽和高
iv.setLayoutParams(new ImageSwitcher.LayoutParams(
400, 400));
return iv; // 返回ImageView对象
}
}
}
代码中有详细的注释,我觉得我都能看懂(自认为自己比较笨),相信大家也一定能看懂。
Android ImageSwitcher和Gallery的使用的更多相关文章
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...
- Android -- ImageSwitch和Gallery 混合使用
1. 实现效果
- android ImageSwitcher
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android=&q ...
- Android学习笔记 Gallery图库组件的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android学习之Gallery
在Android中,画廊控件Gallery用来显示图片列表,可以用手指直接拖动图片左右移动.Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到显示到最 ...
- 备忘-Android ViewPager 与Gallery滑动冲突解决方法
解决方法,重新定义gallery,禁止触发pager的触摸事件 1 public class UserGallery extends Gallery implements OnGestureListe ...
- Android源代码之Gallery专题研究(1)
前言 时光飞逝,从事Android系统开发已经两年了,总想写点什么来安慰自己.思考了非常久总是无法下笔,认为没什么好写的.如今最终决定写一些符合大多数人需求的东西,想必使用过Android手机的人们一 ...
- Android ImageSwitcher 配合Picasso解决内存溢出(OOM)问题
最近项目中用到了 ImageSwitcher 来实现图片切换,使用起来很简单,但发现当图片比较大(超过了3M)时,程序出现了内存溢出(OOM)问题而崩溃了. 原因就是图片太大了,显示到 ImageVi ...
- Android基础TOP6_2:Gallery +Image完成画廊
Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
随机推荐
- SSO单点登录解决方案[转载]
1 什么是单点登陆 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互 ...
- 文件写操作--WriteLog
private static void Write(string sMsg, string fileName) { if (sMsg != "") { try { var dir ...
- [BILL WEI]一些经常用到的SQL函数
截取时间 --convert可以截取特点值 convert(varchar(10),getdate(),120) 截取2012-11-11 11:11:11 前10位,得到日期2012-11-11
- HashPasswordForStoringInConfigFile 已过时
在.net 4.5版本下,使用System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile进行MD5加密时,会出 ...
- SAE网站搭建(1)
用了半天时间,把Django的基本结构同步到SAE上了,里边比较麻烦的地方如下: 1. 数据库的同步; SAE用的是SQL数据库,默认使用下面的用户名.密码等变量(SAE为我们做了很多工作) 首先需要 ...
- 【转】CUDA5/CentOS6.4
转载自http://wenzhang.baidu.com/article/view?key=076f36658dd0828c-1393896446 This article explains how ...
- Unity光照图UV显示
美术的同学觉得 Unity 光照图烘焙的不够美丽,需要在 ps 里修一修,但是不知道每个物体对应的光照图在哪个区域,UV 是如何分布的,于是要求写一个工具显示,于是有了下面这个: 打开场景自动读取当前 ...
- 【UER #1】跳蚤OS(Trie)
跳蚤OS 是跳蚤国自主研发的功能强大的操作系统. 跳蚤OS的文件系统与普通的文件系统类似,是个文件夹套文件夹的结构.文件系统根目录称为“//”.我们可以用文件路径来表明文件所在的位置,比如“/flea ...
- codeforces 660C Hard Process
维护一个左右区间指针就可以. #include<cstdio> #include<cstring> #include<iostream> #include<q ...
- 集群搭建:主机宽带拨号上网,虚拟机使用桥接模式,该如何ping通外网
首先介绍一下看这篇文章需要的基础.需要了解虚拟机的 虚拟机的三种网络模式,有Linux基础知识,这些都是前提.首先介绍一下我的环境:主机:win7虚拟机:VMware Workstation 10虚拟 ...