Android实战简易教程<十>(画廊组件Gallery实用研究)

Gallery组件用于拖拽浏览图片,下面我们就来看一下如何实现。

一、实现Gallery

1.布局文件很简单:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/MyLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#FFFFFF"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <Gallery  
  10.         android:id="@+id/myGallery"  
  11.         android:gravity="center_vertical"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content" />  
  14.   
  15. </LinearLayout>  


2.自定义适配器类,可以直接覆写BaseAdapter类中的几个方法。

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseAdapter;  
  7. import android.widget.Gallery;  
  8. import android.widget.ImageView;  
  9. import android.widget.Gallery.LayoutParams;  
  10.   
  11. public class ImageGalleryAdapter extends BaseAdapter {  
  12.     private Context context;  
  13.     private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,  
  14.             R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e, };  
  15.   
  16.     public ImageGalleryAdapter(Context c) {//构造方法,用于获取上下文对象  
  17.         this.context = c;  
  18.     }  
  19.   
  20.     public int getCount() {  
  21.   
  22.         return imgRes.length;  
  23.     }  
  24.   
  25.     public Object getItem(int position) {  
  26.         return imgRes[position];  
  27.     }  
  28.   
  29.     public long getItemId(int position) {  
  30.         return imgRes[position];  
  31.     }  
  32.   
  33.     public View getView(int position, View convertView, ViewGroup parent) {  
  34.         ImageView img = new ImageView(this.context);  
  35.         img.setBackgroundColor(0xFFFFFFFF);  
  36.         img.setImageResource(this.imgRes[position]);//设置资源  
  37.         img.setScaleType(ImageView.ScaleType.CENTER);//居中显示  
  38.         img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,  
  39.                 LayoutParams.WRAP_CONTENT));  
  40.         return img;  
  41.     }  
  42.   
  43. }  


3.MainActivity.java:

[java]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.Toast;  
  8. import android.widget.AdapterView.OnItemClickListener;  
  9. import android.widget.Gallery;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private Gallery gallery;  
  13.   
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState); // 生命周期方法  
  16.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  17.         gallery = (Gallery) findViewById(R.id.myGallery);  
  18.         gallery.setAdapter(new ImageGalleryAdapter(this));  
  19.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  20.   
  21.             public void onItemClick(AdapterView<?> parent, View view,  
  22.                     int position, long id) {  
  23.                 Toast.makeText(MainActivity.this,  
  24.                         "您选择了第" + String.valueOf(position + 1) + "张图片",  
  25.                         Toast.LENGTH_SHORT).show();  
  26.   
  27.             }  
  28.         });  
  29.     }  
  30. }  


4.运行实例如下:

二、Gallery和ImageSwitcher结合

这时的Gallery我们用SimpleAdapter类完成。

1.布局文件:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/MyLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#FFFFFF"  
  7.     android:gravity="bottom"  
  8.     android:orientation="vertical" >  
  9.   
  10.     <ImageSwitcher  
  11.         android:id="@+id/imageSwitcher"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content" >  
  14.     </ImageSwitcher>  
  15.   
  16.     <Gallery  
  17.         android:id="@+id/gallery"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:gravity="center_vertical"  
  21.         android:spacing="5dp" />  
  22.   
  23. </LinearLayout>  


2.定义显示模板:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#FFFFFF"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/img"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:scaleType="center" />  
  13.   
  14. </LinearLayout>  


3.MainActivity.java程序:

[html]  view plain copy
  1. package org.yayun.demo;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.ImageSwitcher;  
  14. import android.widget.ImageView;  
  15. import android.widget.SimpleAdapter;  
  16. import android.widget.Toast;  
  17. import android.widget.AdapterView.OnItemClickListener;  
  18. import android.widget.Gallery.LayoutParams;  
  19. import android.widget.Gallery;  
  20. import android.widget.ViewSwitcher.ViewFactory;  
  21.   
  22. public class MainActivity extends Activity {  
  23.     private Gallery gallery;  
  24.     private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();  
  25.     private SimpleAdapter simpleAdapter;  
  26.     private ImageSwitcher imageSwitcher;  
  27.   
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState); // 生命周期方法  
  30.         super.setContentView(R.layout.main); // 设置要使用的布局管理器  
  31.         initAdapter();  
  32.         gallery = (Gallery) findViewById(R.id.gallery);  
  33.         imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);  
  34.         imageSwitcher.setFactory(new ViewFactory() {  
  35.   
  36.             public View makeView() {  
  37.                 ImageView imageView = new ImageView(MainActivity.this);  
  38.                 imageView.setBackgroundColor(0xFFFFFFFF);  
  39.                 imageView.setScaleType(ImageView.ScaleType.CENTER);  
  40.                 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(  
  41.                         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  42.                 return imageView;  
  43.             }  
  44.         });  
  45.         gallery.setAdapter(simpleAdapter);  
  46.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  47.   
  48.             public void onItemClick(AdapterView<?> parent, View view,  
  49.                     int position, long id) {  
  50.                 Map<String, Integer> map = (Map<String, Integer>) MainActivity.this.simpleAdapter  
  51.                         .getItem(position);// 取出map  
  52.                 MainActivity.this.imageSwitcher.setImageResource(map.get("img"));// 设置显示图片  
  53.   
  54.             }  
  55.         });  
  56.     }  
  57.   
  58.     private void initAdapter() {  
  59.         Field[] fields = R.drawable.class.getDeclaredFields();// Java反射机制获取所有资源图片  
  60.         for (int i = 0; i < fields.length; i++) {  
  61.             if (fields[i].getName().startsWith("ispic_")) {// 判断开头  
  62.                 Map<String, Integer> map = new HashMap<String, Integer>();  
  63.                 try {  
  64.                     map.put("img", fields[i].getInt(R.drawable.class));  
  65.                 } catch (IllegalArgumentException e) {  
  66.                     // TODO Auto-generated catch block  
  67.                     e.printStackTrace();  
  68.                 } catch (IllegalAccessException e) {  
  69.                     // TODO Auto-generated catch block  
  70.                     e.printStackTrace();  
  71.                 }  
  72.                 this.list.add(map);  
  73.             }  
  74.         }  
  75.         simpleAdapter = new SimpleAdapter(this, this.list,  
  76.                 R.layout.grid_layout, new String[] { "img" },  
  77.                 new int[] { R.id.img });  
  78.   
  79.     }  
  80. }  


4.运行实例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值