Android详细教程(基础篇):十六、View组件高级篇:Gallery组件详解

版权声明:本文为博主原创文章,转载请注明出处。作者:杨雄进 https://blog.csdn.net/makyan/article/details/89164845

7.11. Gallery组件(拖拉组件)

对于许多使用过Android手机的用户应该知道,在Android中可以使用一些软件方便的进行图片的拖拽浏览,

这样的功能就可以通过Gallery实现,使用Gallery组件可以定义一组图片浏览框,可以减轻开发者对于图片

浏览开发的功能的开发困难。

它的继承结构如下:

java.lang.Object

   

android.view.View

 

   

android.view.ViewGroup

 

 

   

android.widget.AdapterView<T extends android.widget.Adapter>

 

 

 

   

android.widget.AbsSpinner

 

 

 

 

   

android.widget.Gallery

在使用Gallery设置图片集的时候需要使用setAdapter()方法,此时设置的SpinnerAdapter接口的对象,主

要的功能是定义一组要显示的组件的适配器,而对于这种适配器操作有两种可选方式:

  • 方式一:用一个自定义的类直接继承SpinnerAdapter接口的子类----android.widget.BaseAdapter类实现,

这样用户只需要覆写核心操作方法即可,不需要的方法可以不用覆写。

  • 方式二:直接使用之前学习过的SpinnerAdapter类完成。

SpinnerAdapter接口:

public interface

SpinnerAdapter

implements Adapter

已知子类:

Known Indirect Subclasses

ArrayAdapter<T>, BaseAdapter, CursorAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter

范例一:使用BaseAdapter

配置文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:id="@+id/MyLayout"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

     <Gallery

          android:id="@+id/myGallery"

          android:gravity="center_vertical"

          android:spacing="3px"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

</LinearLayout>

自定义Adapter:

package com.makyan.demo;

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

public class ImageGalleryAdapter extends BaseAdapter {

     private Context myContext;                                  // Context对象

     private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,

               R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e };

     public ImageGalleryAdapter(Context c) {                // 接收Context

          this.myContext = c;

     }

     @Override

     public int getCount() {                                // 返回图片个数

          return this.imgRes.length;

     }

     @Override

     public Object getItem(int position) {                   // 取得指定位置的图片

          return this.imgRes[position];

     }

     @Override

     public long getItemId(int position) {                   // 取得指定位置的图片

          return this.imgRes[position];

     }

     @Override

     public View getView(int position, View convertView, ViewGroup parent) {

          ImageView img = new ImageView(this.myContext);

          img.setBackgroundColor(0xFFFFFFFF);

          img.setImageResource(this.imgRes[position]);  // 给ImageView设置资源

          img.setScaleType(ImageView.ScaleType.CENTER); // 居中显示

          img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,

                    LayoutParams.WRAP_CONTENT));            // 布局参数

          return img;

     }

}

Activity:

package com.makyan.demo;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Gallery;

import android.widget.Toast;

public class GalleryActivity extends Activity {

     private Gallery gallery = null;                                  // 图片浏览

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_gallery);                       // 调用布局文件

          gallery = (Gallery) super.findViewById(R.id.myGallery) ;     // 取得组件

          gallery.setAdapter(new ImageGalleryAdapter(this)); // 设置图片集

          gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;// 设置单击事件

     }

     private class OnItemClickListenerImpl implements OnItemClickListener {

          @Override

          public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

               Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show(); // 显示图片编号

          }

     }

}

范例二:使用SimpleAdapter

注意:使用SimpleAdapter一定要创建一个内部的组件(这是一个布局文件)。

配置:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:id="@+id/MyLayout"

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:gravity="bottom">

     <ImageSwitcher

          android:id="@+id/myImageSwitcher"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

     <Gallery

          android:id="@+id/myGallery"

          android:gravity="center_vertical"

          android:spacing="3px"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

</LinearLayout>

自定义布局文件:

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

     xmlns:android="http://schemas.android.com/apk/res/android"

     android:orientation="horizontal"

     android:layout_width="wrap_content"

     android:layout_height="wrap_content"

     android:background="#FFFFFF">

     <ImageView

          android:id="@+id/img"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:scaleType="center"/>

</LinearLayout>

Activity:

package com.makyan.demo;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Gallery;

import android.widget.ImageSwitcher;

import android.widget.ImageView;

import android.widget.SimpleAdapter;

import android.widget.ViewSwitcher.ViewFactory;

public class GallerySimpleAdapterActivity extends Activity {

     private Gallery gallery = null;                                  // 图片浏览

     private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;

     private SimpleAdapter simpleAdapter = null;                  // 适配器

     private ImageSwitcher myImageSwitcher = null ;                    // 图片切换

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_gallery);                       // 调用布局文件

          initAdapter() ;                                        // 初始化适配器

          gallery = (Gallery) super.findViewById(R.id.myGallery) ;// 取得组件

          myImageSwitcher = (ImageSwitcher) super

                    .findViewById(R.id.myImageSwitcher); // 取得组件

// 设置图片工厂

          myImageSwitcher.setFactory(new ViewFactoryImpl()) ;

// 设置图片集     

gallery.setAdapter(this.simpleAdapter);

          gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;// 设置单击事件

     }

     private class OnItemClickListenerImpl implements OnItemClickListener {

          @SuppressWarnings("unchecked")

          @Override

          public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

               Map<String, Integer> map = (Map<String, Integer>)simpleAdapter.getItem(position);                         // 取出Map

               myImageSwitcher.setImageResource(map.get("img")); // 设置显示图片

          }

     }

     public void initAdapter(){    // 初始化适配器

          Field[] fields = R.drawable.class.getDeclaredFields(); //用反射机制获取已定义的资源

          //加载资源

          for (int x = 0; x < fields.length; x++) {   

               if (fields[x].getName().startsWith("ispic_")){          // 所有ispic_*命名的图片

                    Map<String,Integer> map = new HashMap<String,Integer>() ;    // 定义Map

                    try {

                         map.put("img", fields[x].getInt(R.drawable.class)) ;

                    } catch (Exception e) {  // 设置图片资源

                    }

                    list.add(map) ; // 保存Map

               }

          }

          simpleAdapter = new SimpleAdapter(this, // 实例化SimpleAdapter

                    this.list, // 要包装的数据集合

                    R.layout.grid_layout, // 要使用的显示模板

                    new String[] { "img" },// 定义要显示的Map的Key

                    new int[] {R.id.img });  // 与模板中的组件匹配

     }

     private class ViewFactoryImpl implements ViewFactory {            // 定义视图工厂类

          @Override

          public View makeView() {

               ImageView img = new ImageView(GallerySimpleAdapterActivity.this);      // 实例化图片显示

               img.setBackgroundColor(0xFFFFFFFF);                         // 设置背景颜色

               img.setScaleType(ImageView.ScaleType.CENTER);           // 居中显示

               img.setLayoutParams(new ImageSwitcher.LayoutParams(          // 自适应图片大小

                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));// 定义组件

               return img;

          }

     }

}

演示效果:

 

猜你喜欢

转载自blog.csdn.net/makyan/article/details/89164845
今日推荐