6.1.6 Gallery结合案例详解

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案例,可以用手滑动Gallery,当用户点击某个图片弹出一个Toast,如6-11图:
 
6.1.6 Gallery结合案例详解_第1张图片

6-11 Gallery控件使用效果图
程序代码请参考代码清单6-9:
【代码清单6-9】 chapter6_5/src/com/work/GalleryActivity.java
public class GalleryActivity extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
       
        registerForContextMenu(g);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(R.string.gallerytext);
    }
   
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
        return true;
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
       
        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {       
ImageView imageView;
imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
        }

        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.beijing,
                R.drawable.changsha,
                R.drawable.chengdu,
                R.drawable.chongqing,
                R.drawable.haerbing,
                R.drawable.jinan,
                R.drawable.jiujiang,
                R.drawable.kunming,
                R.drawable.nanjing
        };
    }
}
代码的关键的地方是实现BaseAdapter适配器类——ImageAdapter,其中关键是getView()实现。在convertView为null时候实例化控件,imageView.setLayoutParams(new GridView.LayoutParams(136, 88)是设置一个单元格中图片的大小是136×88像素。imageView.setScaleType(ImageView.ScaleType.FIT_XY) 缩放图片使用FILL方式。imageView.setImageResource(mImageIds[position])为图片控件设置图片。
在布局文件/chapter6_5/res/layout/main.xml中添加Gallery控件:
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
本例中设置图片的背景样式是边框样式,如图6-12。
 
6.1.6 Gallery结合案例详解_第2张图片

图6-12 图片背景样式
imageView.setBackgroundResource(mGalleryItemBackground)语句就是设定样式的,成员变量mGalleryItemBackground是在ImageAdapter的构造方法中初始化的。
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
mGalleryItemBackground是与galleryItemBackground背景资源绑定的id值,这个id对应的galleryItemBackground属性就是设定带有边框的背景样式。
此外还要在chapter6_5/res/values/目录下面创建一个attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>
这是一个自定义控件属性的xml文件。但是在Android1.0时候没有这么麻烦,而是如下方式实现:
public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
        mGalleryItemBackground = a.getResourceId(
                android.R.styleable.Theme_galleryItemBackground, 0);
        a.recycle();
}
android.R.styleable.Theme_galleryItemBackground 属性在Android1.0是可以访问的,而在Android1.0之后就不能访问了,而要通过本例的方式获得galleryItemBackground的id值。
                                                                   出自《Android开发案例驱动教程》

你可能感兴趣的:(6.1.6 Gallery结合案例详解)