画廊视图(Gallery)的功能和用法

      Gallery与Spinner组件有共同的父类:AbsSpinner,表明Gallery和Spinner是同一个列表框。它们之间的区别是Spinner显示的垂直的列表选择框,而Gallery显示的是一个水平的列表选择框。Gallery和Spinner还有一个区别:Spinner的作用是供用户选择,而Gallery则允许用户通过拖动来查看上一个、下一个列表项。

     Gallery本身的用法非常简单——基本上与Spinner的用法相似,只要为它提供一个内容Adapter即可,该Adapter的getView方法所返回的View将作为Gallery列表的列表项。如果程序要监控到Gallery列表项的改变,通过为Gallery添加OnItemSelectedListener监听器即可实现。

     实例:“幻灯片”式图片查看器

     本实例也是带预览的图片浏览器,但本实例的界面更加友好,因为本实例采用Gallery作为图片预览器,Gallery会生成一个“水平列表”,每个列表项就是一张图片预览,而且Gallery生成的“水平列表”可以让用户通过拖动来切换列表项。

     下面是本实例的界面布局文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >
<!-- 定义一个ImageView组件 -->
<ImageView android:id="@+id/imageView"
    android:layout_width="320dp"
    android:layout_height="320dp"
android:layout_gravity="center"
/> <!-- 定义一个Gallery组件 --> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:unselectedAlpha="0.6" android:spacing="2pt"/> </LinearLayout>

上面的布局文件非常简单,仅仅定义了两个组件:ImageView和Gallery组件。接下来的主程序很简单:为Gallery传入一个Adapter对象,这样Gallery即可正常工作。

为了让ImageView可显示Gallery中选中的图片,为Gallery绑定OnItemSelectedListener监听器即可。程序代码如下:

package org.crazyit.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;

public class GalleryTest extends Activity {
       int[] imageIds=new int[]{
               R.drawable.shuangzi,
               R.drawable.shuangyu,
               R.drawable.chunv,
               R.drawable.tiancheng,
               R.drawable.tianxie,
               R.drawable.sheshou,
               R.drawable.juxie,
               R.drawable.shuiping,
               R.drawable.shizi,
               R.drawable.baiyang,
               R.drawable.jinniu,
               R.drawable.mojie
       };
       
       @SuppressWarnings("deprecation")
         Gallery gallery;
      ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_test);
        gallery=(Gallery)findViewById(R.id.gallery);
        //获取显示图片的ImageView对象
        imageView=(ImageView)findViewById(R.id.imageView);
        //创建一个BaseAdapter对象,该对象负责提供Gallery所显示的列表项
        BaseAdapter adapter=new BaseAdapter()
        {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return imageIds.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @SuppressWarnings("deprecation")
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                //创建一个ImageView
                ImageView imageView=new ImageView(GalleryTest.this);
                imageView.setImageResource(imageIds[position]);
                //设置ImageView的缩放类型
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                //为imageView设置布局参数
                imageView.setLayoutParams(new Gallery.LayoutParams( 175,
                        275));
                //设置背景风格。Gallery背景风格定义在attrs.xml中  
                TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery);
                imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 1));
        
                return imageView;
            }
        };
        gallery.setAdapter(adapter);
        gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
            //当Gallery选中项发生改变时触发该方法
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                imageView.setImageResource(imageIds[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                
            }
            
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.gallery_test, menu);
        return true;
    }

}

上面代码中使用了R.styleable.Gallery资源,该资源是/res/values/attrs.xml文件,其布局文件如下:

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

上面的程序中粗体字代码创建了一个BaseAdapter对象,该Adapter将负责为Gallery提供列表项。运行上面的程序,将可以看到如下效果:

原文地址:https://www.cnblogs.com/wolipengbo/p/3381395.html