专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 图形/图像

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

发布时间:2011-06-27 20:27:36 文章来源:www.iduyao.cn 采编人员:星星草
本可循环显示图像的Android Gallery组件

 

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

Gallery组件的传统用法

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

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




p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

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

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

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

 

     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 };

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

在本例的 main.xml 文件中配置了一个 Gallery 组件,代码如下:

 

<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"  android:layout_width ="fill_parent"
    android:layout_height
="fill_parent" >
    
< Gallery  android:id ="@+id/gallery"  android:layout_width ="fill_parent"
        android:layout_height
="wrap_content"  android:layout_marginTop ="30dp"   />
</ LinearLayout >

 

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

现在在 onCreate 方法中装载这个组件,代码如下:

 

 

     public   void  onCreate(Bundle savedInstanceState)
    {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//  装载Gallery组件
        Gallery gallery  =  (Gallery) findViewById(R.id.gallery);
        
//  创建用于描述图像数据的ImageAdapter对象
        ImageAdapter imageAdapter  =   new  ImageAdapter( this );
         
//  设置Gallery组件的Adapter对象
        gallery.setAdapter(imageAdapter);
    }

 

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

在上面的代码中涉及到一个非常重要的类: ImageAdapter 。该类是 android.widget.BaseAdapter 的子类,用于描述图像信息。下面先看一下这个类的完整代码。

 

 

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

        
public  ImageAdapter(Context context)
        {
            mContext 
=  context;
              
//  获得Gallery组件的属性
            TypedArray typedArray  =  obtainStyledAttributes(R.styleable.Gallery);
            mGalleryItemBackground 
=  typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 
0 );                        
        }
         
//  返回图像总数
         public   int  getCount()
        {
            
return  resIds.length;
        }
        
public  Object getItem( int  position)
        {
            
return  position;
        }

        
public   long  getItemId( int  position)
        {
            
return  position;
        }
         
//  返回具体位置的ImageView对象
         public  View getView( int  position, View convertView, ViewGroup parent)
        {
            ImageView imageView 
=   new  ImageView(mContext);
             
//  设置当前图像的图像(position为当前图像列表的位置)
            imageView.setImageResource(resIds[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(
new  Gallery.LayoutParams( 163 106 ));
            
//  设置Gallery组件的背景风格
            imageView.setBackgroundResource(mGalleryItemBackground);
            
return  imageView;
        }
    }

 

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

在编写 ImageAdapter 类时应注意的两点:

1.  ImageAdapter 类的构造方法中获得了 Gallery 组件的属性信息。这些信息被定义在 res\values\attrs.xml 文件中,代码如下:

 

 

<? xml version="1.0" encoding="utf-8" ?>
< resources >
    
< declare-styleable  name ="Gallery" >
        
< attr  name ="android:galleryItemBackground"   />
    
</ declare-styleable >
</ resources >

 

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

上面的属性信息用于设置 Gallery 的背景风格。

2.  ImageAdapter 类中有两个非常重要的方法: getCount getView 。其中 getCount 方法用于返回图像总数,要注意的是,这个总数不能大于图像的实际数(可以小于图像的实际数),否则会抛出越界异常。当 Gallery 组件要显示某一个图像时,就会调用 getView 方法,并将当前的图像索引( position 参数)传入该方法。一般 getView 方法用于返回每一个显示图像的组件( ImageView 对象)。从这一点可以看出, Gallery 组件是即时显示图像的,而不是一下将所有的图像都显示出来。在 getView 方法中除了创建了 ImageView 对象,还用从 resIds 数组中获得了相应的图像资源 ID 来设置在 ImageView 中显示的图像。最后还设置了 Gallery 组件的背景显示风格。

OK ,现在来运行这个程序,来回拖动图像列表,就会看到如图 1 和图 2 所示的效果了。

循环显示图像的原理

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

循环显示有些类似于循环链表,最后一个结点的下一个结点又是第 1 个结点。循环显示图像也可以模拟这一点。

也许细心的读者从上一节实现的 ImageAdapter 类中会发现些什么。对!就是 getView 方法中的 position 参数和 getCount 方法的关系。 position 参数的值是不可能超过 getCount 方法返回的值的,也就是说, position 参数值的范围是 0 getCount() - 1

如果这时 Gallery 组件正好显示到最后一个图像, position 参数值正好为 getCount() - 1 。那么我们如何再让 Gallery 显示下一个图像呢?也就是说让 position 参数值再增 1 ,对!将 getCount() 方法的返回值也增 1

那么这里还有一个问题,如果 position 参数值无限地增加,就意味着 resIds 数组要不断地增大,这样会大大消耗系统的资源。想到这,就需要解决两个问题:既要 position 不断地增加,又让 resIds 数组中保存的图像资源 ID 是有限的,该怎么做呢?对于 getCount() 方法非常好解决,可以让 getCount 方法返回一个很大的数,例如, Integer.MAX_VALUE 。这时 position 参数值就可以随着 Gallery 组件的图像不断向前移动而增大。现在 resIds 数组只有 15 个元素,如果 position 的值超过数组边界,要想继续循环取得数组中的元素(也就是说,当 position 的值是 15 时,取 resIds 数组的第 0 个元素,是 16 时取第 1 个元素),最简单的方法就是取余,代码如下:

resIds[position % resIds.length]

在本节对 ImageAdapter 类做了如下两个改进:

1.  使 getCount 方法返回一个很大的值。建议返回 Integer.MAX_VALUE

2.  getView 方法中通过取余来循环取得 resIds 数组中的图像资源 ID

通过上面两点改进,可以使图像列表在向右移动时会循环显示图像。当然,这种方法从本质上说只是伪循环,也就是说,如果真把图像移动到 getCount 方法返回的值那里,那也就显示到最后一个图像的。不过在这里 getCount 方法返回的是 Integer.MAX_VALUE ,这个值超过了 20 亿,除非有人真想把图像移动到第 20 亿的位置,否则 Gallery 组件看着就是一个循环显示图像的组件。

 

 

实现循环显示图像的Gallery组件

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

在本节将组出与循环显示图像相关的 ImageAdapter 类的完整代码。读者可以从中看到上一节介绍的两点改进。为了使界面看上去更丰满,本例还在单击某一个 Gallery 组件中的图像时在下方显示一个放大的图像(使用 ImageSwitcher 组件)。本例的显示效果如图 3 所示。当不断向后移动图像时,图像可不断显示,读者可以自己运行本例来体验一下。



p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

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

 

 

<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"  android:layout_width ="fill_parent"
    android:layout_height
="fill_parent" >
    
< Gallery  android:id ="@+id/gallery"  android:layout_width ="fill_parent"
        android:layout_height
="wrap_content"  android:layout_marginTop ="30dp"   />
    
< ImageSwitcher  android:id ="@+id/imageswitcher"
        android:layout_width
="fill_parent"  android:layout_height ="wrap_content"
        android:layout_marginTop
="30dp"   />
</ LinearLayout >

 

 

p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; line-height: 125%; font-size: 10.5pt; font-family: "Times New Roman","serif"; }.MsoChpDefault { font-size: 10pt; }div.WordSection1 { page: WordSection1; }

本例中 Main 类的完整代码如下:

 

 

 

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。
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: