android开发--Gallery

来源:互联网 发布:linux 日志 编辑:程序博客网 时间:2024/05/22 08:17
Gallery控件, 即Android的图片浏览控件,还是非常好用的,做个小例子说明一下:
首先是layout file:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <Gallery
  8.     android:id="@+id/gallery"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="fill_parent"
  11.     />
  12. </LinearLayout>
接下来是代码,首先是需要定义一个BaseAdaper的子类来操作控制图片资源,然后在主类中通过Gallery.setAdapter(new ImageAdapter(this));来使用这个控制类。
  1. package com.zx.galery;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.AdapterView;
  8. import android.widget.BaseAdapter;
  9. import android.widget.Gallery;
  10. import android.widget.ImageView;
  11. import android.widget.Toast;
  12. import android.widget.AdapterView.OnItemClickListener;

  13. public class GalleryTest extends Activity {
  14.     private Gallery mGallery;
  15.    
  16.     /** Called when the activity is first created. */
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.main);
  21.        
  22.         mGallery = (Gallery)findViewById(R.id.gallery);
  23.         mGallery.setAdapter(new ImageAdapter(this));
  24.         mGallery.setOnItemClickListener(new OnItemClickListener() {
  25.             public void onItemClick(AdapterView parent, View v, int position, long id) {
  26.                 Toast.makeText(GalleryTest.this, "" + position, Toast.LENGTH_SHORT).show();
  27.             }
  28.         });
  29.     }
  30.    
  31.     /*
  32.     * class ImageAdapter is used to control gallery source and operation.
  33.     */
  34.     private class ImageAdapter extends BaseAdapter{
  35.         private Context mContext;
  36.         private Integer[] mImage = {
  37.                 R.drawable.sample_0,
  38.                 R.drawable.sample_1,
  39.                 R.drawable.sample_2,
  40.                 R.drawable.sample_3,
  41.                 R.drawable.sample_4,
  42.                 R.drawable.sample_5,
  43.                 R.drawable.sample_6,
  44.                 R.drawable.sample_7
  45.         };
  46.        
  47.         public ImageAdapter(Context c){
  48.             mContext = c;
  49.         }
  50.         @Override
  51.         public int getCount() {
  52.             // TODO Auto-generated method stub

  53.             return mImage.length;
  54.         }

  55.         @Override
  56.         public Object getItem(int position) {
  57.             // TODO Auto-generated method stub

  58.             return position;
  59.         }

  60.         @Override
  61.         public long getItemId(int position) {
  62.             // TODO Auto-generated method stub

  63.             return position;
  64.         }

  65.         @Override
  66.         public View getView(int position, View convertView, ViewGroup parent) {
  67.             // TODO Auto-generated method stub

  68.             ImageView i = new ImageView (mContext);
  69.             i.setImageResource(mImage[position]);
  70.             i.setScaleType(ImageView.ScaleType.FIT_XY);
  71.             i.setLayoutParams(new Gallery.LayoutParams(136, 88));
  72.            
  73.            
  74.             return i;
  75.         }
  76.        
  77.     };
  78. }
以上只是一个很简单的例子,下一步就是要将Gallery与ImageSwitcher结合起来做一个电子相册。