首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

可循环展示图像的Android Gallery组件

2012-07-18 
可循环显示图像的Android Gallery组件转发自:http://www.cnblogs.com/nokiaguy/archive/2010/08/23/180687

可循环显示图像的Android Gallery组件

转发自:http://www.cnblogs.com/nokiaguy/archive/2010/08/23/1806870.html

?

在此感谢原创作者,谢谢。

?

?

Gallery组件主要用于横向显示图像列表,不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说,如果为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时,就不会再继续显示了。这虽然在大多数时候没有什么关系,但在某些情况下,我们希望图像显示到最后一张时再重第1张开始显示,也就是循环显示。要实现这种风格的Gallery组件,就需要对Gallery的Adapter对象进行一番改进。

Gallery组件的传统用法

?????在实现可循环显示图像的Gallery组件之前先来回顾一下Gallery组件的传统用法。Gallery组件可以横向显示一个图像列表,当单击当前图像的后一个图像时,这个图像列表会向左移动一格,当单击当前图像的前一个图像时,这个图像列表会向右移动一样。也可以通过拖动的方式来向左和向右移动图像列表。当前显示的是第1个图像的效果如图1所示。Gallery组件显示到最后一个图像的效果如图2所示。
可循环展示图像的Android Gallery组件

可循环展示图像的Android Gallery组件

从图2可以看出,当显示到最后一个图像时,列表后面就没有图像的,这也是Gallery组件的基本显示效果。在本文后面的部分将详细介绍如何使Gallery组件显示到最后一个图像时会从第1个图像开始显示。

好了,现在我们来看一下图1和图2的效果是如何做出来的吧。Gallery既然用于显示图像,那第1步就必须要有一些图像文件用来显示。现在可以随意准备一些图像。在本文的例子中准备了15个jpg文件(item1.jpg至item15.jpg)。将这些文件都放在res\drawable目录中。

下面将这些图像的资源ID都保存在int数组中,代码如下:




在main.xml文件中定义的Gallery和ImageSwitcher组件的代码如下:

代码package?net.blogjava.mobile;

import?android.app.Activity;
import?android.content.Context;
import?android.content.res.TypedArray;
import?android.os.Bundle;
import?android.view.View;
import?android.view.ViewGroup;
import?android.view.animation.AnimationUtils;
import?android.widget.AdapterView;
import?android.widget.BaseAdapter;
import?android.widget.Gallery;
import?android.widget.ImageSwitcher;
import?android.widget.ImageView;
import?android.widget.AdapterView.OnItemSelectedListener;
import?android.widget.Gallery.LayoutParams;
import?android.widget.ViewSwitcher.ViewFactory;

public?class?Main?extends?Activity?implements?OnItemSelectedListener,
????????ViewFactory
{
????private?Gallery?gallery;
????private?ImageSwitcher?imageSwitcher;
????private?ImageAdapter?imageAdapter;

????private?int[]?resIds?=?new?int[]
????{?R.drawable.item1,?R.drawable.item2,?R.drawable.item3,?R.drawable.item4,
????????????R.drawable.item5,?R.drawable.item6,?R.drawable.item7,
????????????R.drawable.item8,?R.drawable.item9,?R.drawable.item10,
????????????R.drawable.item11,?R.drawable.item12,?R.drawable.item13,
????????????R.drawable.item14,?R.drawable.item15?};

????public?class?ImageAdapter?extends?BaseAdapter
????{
????????int?mGalleryItemBackground;
????????private?Context?mContext;

????????public?ImageAdapter(Context?context)
????????{
????????????mContext?=?context;
????????????TypedArray?typedArray?=?obtainStyledAttributes(R.styleable.Gallery);
????????????mGalleryItemBackground?=?typedArray.getResourceId(
????????????????????R.styleable.Gallery_android_galleryItemBackground,?0);????????????????????????
????????}
????????//?第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
????????public?int?getCount()
????????{
????????????return?Integer.MAX_VALUE;
????????}

????????public?Object?getItem(int?position)
????????{
????????????return?position;
????????}

????????public?long?getItemId(int?position)
????????{
????????????return?position;
????????}
?
????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)
????????{
????????????ImageView?imageView?=?new?ImageView(mContext);
????????????????//?第2点改进,通过取余来循环取得resIds数组中的图像资源ID
????????????imageView.setImageResource(resIds[position?%?resIds.length]);
????????????imageView.setScaleType(ImageView.ScaleType.FIT_XY);
????????????imageView.setLayoutParams(new?Gallery.LayoutParams(163,?106));
????????????imageView.setBackgroundResource(mGalleryItemBackground);
????????????return?imageView;
????????}
????}

????@Override
????public?void?onItemSelected(AdapterView<?>?parent,?View?view,?int?position,long?id)
????{
????????//?选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
????????imageSwitcher.setImageResource(resIds[position?%?resIds.length]);

????}
????@Override
????public?void?onNothingSelected(AdapterView<?>?parent)
????{
????}

????@Override
????//?ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
????//??来显示图像
????public?View?makeView()
????{
????????ImageView?imageView?=?new?ImageView(this);
????????imageView.setBackgroundColor(0xFF000000);
????????imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
????????imageView.setLayoutParams(new?ImageSwitcher.LayoutParams(
????????????????LayoutParams.FILL_PARENT,?LayoutParams.FILL_PARENT));
????????return?imageView;
????}

????@Override
????public?void?onCreate(Bundle?savedInstanceState)
????{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????gallery?=?(Gallery)?findViewById(R.id.gallery);
????????imageAdapter?=?new?ImageAdapter(this);
????????gallery.setAdapter(imageAdapter);
????????gallery.setOnItemSelectedListener(this);
????????imageSwitcher?=?(ImageSwitcher)?findViewById(R.id.imageswitcher);
????????//?设置ImageSwitcher组件的工厂对象
????????imageSwitcher.setFactory(this);
????????//?设置ImageSwitcher组件显示图像的动画效果
????imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
????????????????android.R.anim.fade_in));????????imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
????????????????android.R.anim.fade_out));

????}

总结
???? 在本文介绍了如何实现可循环显示的Gallery组件。实际上,这个循环显示只是一个伪循环,不过由于getCount方法返回的图像总数很大(超过20亿),这就意味着已经非常接近无限循环了。实现循环显示图像的关键点有如下两个:
1.? getCount方法返回一个很大的整数值(例如,Integer.MAX_VALUE)。
2.? 在getView方法中通过取余的方法来循环获得图像的资源ID。

?

?

热点排行