首页 > 代码库 > Android学习之-----Gallery

Android学习之-----Gallery

在Android中,画廊控件Gallery用来显示图片列表,可以用手指直接拖动图片左右移动。Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到显示到最后一个图片为止

1.XML布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <ImageView 8         android:id="@+id/imageView" 9         android:layout_width="match_parent"10         android:layout_height="650dp" >11     </ImageView>12 13     <Gallery14         android:id="@+id/gallery"15         android:layout_width="match_parent"16         android:layout_height="wrap_content"17         android:spacing="5dp" >18     </Gallery>19 20 </LinearLayout>

在Gallery属性标签中,android:spacing="5dp"属性用于指定Gallery列表图片之间的间隔为2dp。

2.实现适配器

要实现Gallery画廊控件功能,需要一个容器来存放Gallery显示的图片。我们可以使用一个继承自BaseAdapter类的派生类ImageAdapter来装这些图片。

代码如:

 1 package com.example.gallery; 2  3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Context; 6 import android.view.Menu; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.AdapterView;10 import android.widget.AdapterView.OnItemClickListener;11 import android.widget.BaseAdapter;12 import android.widget.Gallery;13 import android.widget.ImageView;14 15 public class MainActivity extends Activity {16     private ImageView mImageView;17     private Gallery mGallery;18     private int[] images = { R.drawable.m1, R.drawable.m2, R.drawable.m3,19             R.drawable.m4, R.drawable.m5, };20 21     @Override22     protected void onCreate(Bundle savedInstanceState) {23         super.onCreate(savedInstanceState);24         setContentView(R.layout.activity_main);25         mImageView = (ImageView) this.findViewById(R.id.imageView);26         mGallery = (Gallery) this.findViewById(R.id.gallery);27         mGallery.setAdapter(new ImagesAdapter(this));28         mGallery.setOnItemClickListener(new OnItemClickListener() {29             @Override30             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,31                     long arg3) {32                 mImageView.setBackgroundResource(images[arg2]);33             }34         });35     }36 37     public class ImagesAdapter extends BaseAdapter {38         private Context context;39 40         public ImagesAdapter(Context context) {41             this.context = context;42         }43 44         @Override45         public int getCount() {46             // TODO Auto-generated method stub47             return images.length;48         }49 50         @Override51         public Object getItem(int position) {52             // TODO Auto-generated method stub53             return images[position];54         }55 56         @Override57         public long getItemId(int position) {58             // TODO Auto-generated method stub59             return position;60         }61 62         @Override63         public View getView(int position, View arg1, ViewGroup arg2) {64             ImageView imageView = new ImageView(context);65             imageView.setImageResource(images[position]);66             imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));67             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);68             return imageView;69         }70     }71 72     @Override73     public boolean onCreateOptionsMenu(Menu menu) {74         // Inflate the menu; this adds items to the action bar if it is present.75         getMenuInflater().inflate(R.menu.main, menu);76         return true;77     }78 79 }

 

Android学习之-----Gallery