Gallery Tutorials 详解

使用Google官方的例子

程序效果(图):

Gallery Tutorials 详解

main.xmland attr.xml

<?xml version="1.0" encoding="utf-8"?> <!-- 这是一个可以用到布局文件上的通用的风格资源。 在这个例子中,它会应用到Gallery中每一个单独的一项里。 <attr>标签用于给风格定义属性,在本例中,使用的是现成的系统属性 galleryItemBackground,它的意思是给Gallery中每一项定义一个带边框的风格 --> <resources> <declare-styleable name="UseGallery"> <attr name="android:galleryItemBackground"/> </declare-styleable> </resources>

<?xml version="1.0" encoding="utf-8"?> <!-- 这是一个可以用到布局文件上的通用的风格资源。 在这个例子中,它会应用到Gallery中每一个单独的一项里。 <attr>标签用于给风格定义属性,在本例中,使用的是现成的系统属性 galleryItemBackground,它的意思是给Gallery中每一项定义一个带边框的风格 --> <resources> <declare-styleable name="UseGallery"> <attr name="android:galleryItemBackground"/> </declare-styleable> </resources>

GalleryPhotoShowActivity.java

package com.zeph.android.gallery.photoshow; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; public class GalleryPhotoShowActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 这里的写法和GridView一样 // 获取Gallery对象,给它设置适配器,并添加事件相应 Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(GalleryPhotoShowActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } class ImageAdapter extends BaseAdapter { /* * 首先,定义一些成员变量。包括一个用来指向drawable的资源目录文件的数组。 其次,在构造器中, * 为ImageAdapter类的的Context变量进行初始化。 然后获取在XML文件中定义的风格,并将它保存在本地变量中。 * 在构造器的最后,调用TypedArray的recycle()函数,以便它可以变系统再利用。 */ int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; public ImageAdapter(Context mContext) { this.mContext = mContext; // TypedArray是一个可以存放多个数据的数组容器。存放我们在XML文件中定义的一系列风格属性。 /* * public TypedArray obtainStyledAttributes (int[] attrs)方法 * 返回一个一个TypedArray类型数据,里面存放的就是我们在XML中定义的所有的风格属性 * 你可以在R.java文件中看到R.styleable.UseGallery是一个数组, * 里面只有一个值,是因为我们只定义了一个属性。 */ TypedArray attr = mContext.obtainStyledAttributes(R.styleable.UseGallery); // public int getResourceId (int index, int defValue) // 获取index属性的资源指示符 // index 获取属性的指数值 。defValue 如果属性没有定义,默认返回的值。 mGalleryItemBackground = attr.getResourceId( R.styleable.UseGallery_android_galleryItemBackground, 0); //回收之前收到的风格属性,留在以后使用。 attr.recycle(); } public int getCount() { // TODO Auto-generated method stub return mImageIds.length; } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); //设置背景图像 imageView.setBackgroundResource(mGalleryItemBackground); return imageView; } } }
例子中使用的图片在这里下载:sample images