http://txlong-onz.iteye.com/blog/934959

我们有时候在iPhone手机上或者Windows上面看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的图片滚动效果,还可以根据你的点击或者触摸触发其他事件响应。同样的,在Android中也提供这这种实现,这就是通过Gallery在UI上实现缩略图浏览器。

我们来看看Gallery是如何来实现的,先把控件从布局文件中声明,只需知道ID为gallery。

Gallery gallery = (Gallery) findViewById(R.id.gallery);

一般情况下,我们在Android中要用到类似这种图片容器的控件,都需要为它指定一个适配器,让它可以把内容按照我们定义的方式来显示,因此我们来给它加一个适配器,至于这个适配器如何实现,后面接着来操作,这里只需知道这个适配器的类叫ImageAdapter。 gallery.setAdapter(new ImageAdapter(this));

复制代码接下来就是重头戏了,适配器可以说是最重要的,我们来看看如何做?到这里似乎还缺少一些很重要的东西?什么东西呢?我们需要显示的是图片,那么图片我们当然首先要准备好,这里我们准备了5张图片(存放drawable文件夹中),我们用其IDs做索引,以便在适配器中使用。

private Integer[] mps = {  
        R.drawable.icon1,  
        R.drawable.icon2,  
        R.drawable.icon3,  
        R.drawable.icon4,  
        R.drawable.icon5  
};

OK,这里将开始定义适配器了,通过继承BaseAdapter用以实现的适配器。

public class ImageAdapter extends BaseAdapter {  
    private Context mContext;  
        public ImageAdapter(Context context) {  
        mContext = context;  
    }  
 
    public int getCount() {   
        return mps.length;  
    }  
 
    public Object getItem(int position) {  
        return position;  
    }  
 
    public long getItemId(int position) {  
        return position;  
    }  
 
    public View getView(int position, View convertView, ViewGroup parent) {  
        ImageView image = new ImageView(mContext);  
        image.setImageResource(mps[position]);  
        image.setAdjustViewBounds(true);  
        image.setLayoutParams(new Gallery.LayoutParams(  
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
        return image;  
    }  
}

至此,整个Gallery基本都是先完成了,我们还需要为它添加一个监听器,否则这个缩略图浏览器就仅仅只可以看不能用了。

gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
    @Override  
    public void onItemSelected(AdapterView<?> parent, View v,int position, long id) {  
    }  
 
    @Override  
    public void onNothingSelected(AdapterView<?> arg0) {  
    //这里不做响应  
    }  
});

 

 

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

 

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所示。当不断向后移动图像时,图像可不断显示,读者可以自己运行本例来体验一下。

 

例子:

package irdc.EX04_10;
 
import android.app.Activity; 
import android.os.Bundle; /*本范例需使用到的class*/
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView; 
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
public class EX04_10 extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);                      /* 透过findViewById取得 */
    Gallery g = (Gallery) findViewById(R.id.mygallery); /* 新增一ImageAdapter并设定给Gallery对象 */
    g.setAdapter(new ImageAdapter(this));               /* 设定一个itemclickListener并Toast被点选图片的位置 */
    setTitle("Gallery 实现循环浏览图片");
    g.setOnItemClickListener(new OnItemClickListener()
    {
      public void onItemClick(AdapterView parent, View v, int position, long id)
      {
        Toast.makeText(EX04_10.this, getString(R.string.my_gallery_text_pre) + position + getString(R.string.my_gallery_text_post), Toast.LENGTH_SHORT).show();
      }
    });
  }
 
  public class ImageAdapter extends BaseAdapter         /* 改写BaseAdapter自定义一ImageAdapter class */
  { 
    int     mGalleryItemBackground;
    private Context mContext;         /* ImageAdapter的建构子 */
    private int[] myImageIds = {R.drawable.photo1, 
                                    R.drawable.photo2, 
                                    R.drawable.photo3, 
                                    R.drawable.photo4, 
                                    R.drawable.photo5, 
                                    R.drawable.photo6,};
 
    public ImageAdapter(Context c)
    {
      mContext = c; 
      TypedArray a = obtainStyledAttributes(R.styleable.Gallery); /* 使用在res/values/attrs.xml中的定义 的Gallery属性. */
      mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); ///*取得Gallery属性的Index
      a.recycle();/* 让对象的styleable属性能够反复使用 */
    } 
 
    public int getCount()               /* 一定要重写的方法getCount,传回图片数目总数 */
    {
       //return myImageIds.length;
      return Integer.MAX_VALUE;
    } 
 
    public Object getItem(int position) /* 一定要重写的方法getItem,传回position */
    {
      return position;
    } 
 
    public long getItemId(int position) /* 一定要重写的方法getItemId,传回position */
    {
      return position;
    } 
 
    public View getView(int position, View convertView, ViewGroup parent)/* 一定要重写的方法getView,传回一View对象 */
    {  
//      if (position == getCount())
//      {
//        position = 0;
//      }
      ImageView i = new ImageView(mContext);                
      i.setImageResource(myImageIds[position%myImageIds.length]);              /* 设定图片给imageView对象 */
      i.setScaleType(ImageView.ScaleType.FIT_XY);            /* 重新设定图片的宽高 */
      i.setLayoutParams(new Gallery.LayoutParams(136, 88));  /* 重新设定Layout的宽高 */
      i.setBackgroundResource(mGalleryItemBackground);       /* 设定Gallery背景图 */
      return i;                                              /* 传回imageView物件 */
    }  
  }
}

 

http://www.cnblogs.com/salam/archive/2010/10/06/1844564.html

更新日期: 2014-12-02 13:57:12
文章标签:
文章链接: Android中的Gallery的使用
站方声明: 除特别标注, 本站所有文章均为原创, 互联分享, 尊重版权, 转载请注明.