zoukankan      html  css  js  c++  java
  • Android Gallery组件实现循环显示图像

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

      Gallery组件的传统用法

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

    图1

    图2

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

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

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

    [java] view plaincopy
     
    1. private int[] myImageIds = {R.drawable.photo1,   
    2.                                                 R.drawable.photo2,   
    3.                                              R.drawable.photo3,   
    4.                                              R.drawable.photo4,   
    5.                                              R.drawable.photo5,   
    6.                                              R.drawable.photo6,};  

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

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical" android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.     <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"  
    6.         android:layout_height="wrap_content" android:layout_marginTop="30dp" />  
    7. </LinearLayout>  

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

    [java] view plaincopy
     
    1. public void onCreate(Bundle savedInstanceState)  
    2.    {  
    3.        super.onCreate(savedInstanceState);  
    4.        setContentView(R.layout.main);  
    5.        // 装载Gallery组件  
    6.        Gallery gallery = (Gallery) findViewById(R.id.gallery);  
    7.        // 创建用于描述图像数据的ImageAdapter对象  
    8.        ImageAdapter imageAdapter = new ImageAdapter(this);  
    9.         // 设置Gallery组件的Adapter对象  
    10.        gallery.setAdapter(imageAdapter);  
    11.    }  

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

    [java] view plaincopy
     
    1. public class ImageAdapter extends BaseAdapter  
    2.    {  
    3.        int mGalleryItemBackground;  
    4.        private Context mContext;  
    5.        public ImageAdapter(Context context)  
    6.        {  
    7.            mContext = context;  
    8.              // 获得Gallery组件的属性  
    9.            TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);  
    10.            mGalleryItemBackground = typedArray.getResourceId(  
    11.                    R.styleable.Gallery_android_galleryItemBackground, 0);                          
    12.        }  
    13.         // 返回图像总数  
    14.        public int getCount()  
    15.        {  
    16.            return resIds.length;  
    17.        }  
    18.        public Object getItem(int position)  
    19.        {  
    20.            return position;  
    21.        }  
    22.        public long getItemId(int position)  
    23.        {  
    24.            return position;  
    25.        }  
    26.         // 返回具体位置的ImageView对象  
    27.        public View getView(int position, View convertView, ViewGroup parent)  
    28.        {  
    29.            ImageView imageView = new ImageView(mContext);  
    30.             // 设置当前图像的图像(position为当前图像列表的位置)  
    31.            imageView.setImageResource(myImageIds[position]);  
    32.            imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
    33.            imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));  
    34.            // 设置Gallery组件的背景风格  
    35.            imageView.setBackgroundResource(mGalleryItemBackground);  
    36.            return imageView;  
    37.        }  
    38.    }  

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

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

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <declare-styleable name="Gallery">  
    4.         <attr name="android:galleryItemBackground" />  
    5.     </declare-styleable>  
    6. </resources>  

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

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

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

      循环显示图像的原理

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

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

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

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

    [c-sharp] view plaincopy
     
    1. myImageIds[position % myImageIds.length]  

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

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

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

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

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

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

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

    [java] view plaincopy
     
    1. package irdc.EX04_10;  
    2.   
    3. import android.app.Activity;   
    4. import android.os.Bundle; /*本范例需使用到的class*/  
    5. import android.content.Context;  
    6. import android.content.res.TypedArray;  
    7. import android.view.View;  
    8. import android.view.ViewGroup;  
    9. import android.widget.AdapterView;  
    10. import android.widget.BaseAdapter;  
    11. import android.widget.Gallery;  
    12. import android.widget.ImageView;   
    13. import android.widget.Toast;  
    14. import android.widget.AdapterView.OnItemClickListener;  
    15.   
    16. public class EX04_10 extends Activity  
    17. {  
    18.   /** Called when the activity is first created. */  
    19.   @Override  
    20.   public void onCreate(Bundle savedInstanceState)  
    21.   {  
    22.     super.onCreate(savedInstanceState);  
    23.     setContentView(R.layout.main);                      /* 透过findViewById取得 */  
    24.     Gallery g = (Gallery) findViewById(R.id.mygallery); /* 新增一ImageAdapter并设定给Gallery对象 */  
    25.     g.setAdapter(new ImageAdapter(this));               /* 设定一个itemclickListener并Toast被点选图片的位置 */  
    26.     setTitle("Gallery 实现循环浏览图片");  
    27.     g.setOnItemClickListener(new OnItemClickListener()  
    28.     {  
    29.       public void onItemClick(AdapterView parent, View v, int position, long id)  
    30.       {  
    31.         Toast.makeText(EX04_10.this, getString(R.string.my_gallery_text_pre) + position + getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show();  
    32.       }  
    33.     });  
    34.   }  
    35.   
    36.   public class ImageAdapter extends BaseAdapter         /* 改写BaseAdapter自定义一ImageAdapter class */  
    37.   {   
    38.     int     mGalleryItemBackground;  
    39.     private Context mContext;         /* ImageAdapter的建构子 */  
    40.     private int[] myImageIds = {R.drawable.photo1,   
    41.                                     R.drawable.photo2,   
    42.                                     R.drawable.photo3,   
    43.                                     R.drawable.photo4,   
    44.                                     R.drawable.photo5,   
    45.                                     R.drawable.photo6,};  
    46.       
    47.     public ImageAdapter(Context c)  
    48.     {  
    49.       mContext = c;   
    50.       TypedArray a = obtainStyledAttributes(R.styleable.Gallery); /* 使用在res/values/attrs.xml中的定义 的Gallery属性. */  
    51.       mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); ///*取得Gallery属性的Index  
    52.       a.recycle();/* 让对象的styleable属性能够反复使用 */  
    53.     }   
    54.   
    55.     public int getCount()               /* 一定要重写的方法getCount,传回图片数目总数 */  
    56.     {  
    57.        //return myImageIds.length;  
    58.       return Integer.MAX_VALUE;  
    59.     }   
    60.   
    61.     public Object getItem(int position) /* 一定要重写的方法getItem,传回position */  
    62.     {  
    63.       return position;  
    64.     }   
    65.   
    66.     public long getItemId(int position) /* 一定要重写的方法getItemId,传回position */  
    67.     {  
    68.       return position;  
    69.     }   
    70.   
    71.     public View getView(int position, View convertView, ViewGroup parent)/* 一定要重写的方法getView,传回一View对象 */  
    72.     {    
    73. //      if (position == getCount())  
    74. //      {  
    75. //        position = 0;  
    76. //      }  
    77.       ImageView i = new ImageView(mContext);                  
    78.       i.setImageResource(myImageIds[position%myImageIds.length]);              /* 设定图片给imageView对象 */  
    79.       i.setScaleType(ImageView.ScaleType.FIT_XY);            /* 重新设定图片的宽高 */  
    80.       i.setLayoutParams(new Gallery.LayoutParams(136, 88));  /* 重新设定Layout的宽高 */  
    81.       i.setBackgroundResource(mGalleryItemBackground);       /* 设定Gallery背景图 */  
    82.       return i;                                              /* 传回imageView物件 */  
    83.     }    
    84.   }  
    85. }  

     http://blog.csdn.net/herryz/article/details/6141957

  • 相关阅读:
    visual studio项目多级引用不拷贝dll的问题
    ef6 code first,对已有数据库如何执行迁移
    wsl 修改默认安装路径
    Windows docker镜像文件无法删除
    Docker镜像下载很慢,各种加速无效
    activemq整合springboot使用(个人微信小程序用)
    angular入门一之环境安装及项目创建
    jquery中attr()和prop()的区别
    IntelliJ IDEA部署web项目,Tomcat没有出现Artifacts
    IntelliJ IDEA:创建Java Web工程
  • 原文地址:https://www.cnblogs.com/xlchen/p/4217112.html
Copyright © 2011-2022 走看看