Android入门之Gallery用法实例解析
程序员文章站
2022-05-16 12:45:02
本文实例介绍的android的gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观。本文实例中的gallery的用法,主要实现用反...
本文实例介绍的android的gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观。本文实例中的gallery的用法,主要实现用反射机制来动态读取资源中的图片。
该实例代码运行的效果图如下:
main.xml源码如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></gallery> </linearlayout>
java程序源码如下:
package com.testimageview; import java.lang.reflect.field; import java.util.arraylist; import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; import android.widget.adapterview.onitemclicklistener; public class testimageview extends activity { private gallery mgallery; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mgallery = (gallery)findviewbyid(r.id.gallery); try { mgallery.setadapter(new imageadapter(this)); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalaccessexception e) { // todo auto-generated catch block e.printstacktrace(); } mgallery.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview parent, view v, int position, long id) { testimageview.this.settitle(string.valueof(position)); } }); } /* * class imageadapter is used to control gallery source and operation. */ private class imageadapter extends baseadapter{ private context mcontext; private arraylist<integer> imglist=new arraylist<integer>(); private arraylist<object> imgsizes=new arraylist<object>(); public imageadapter(context c) throws illegalargumentexception, illegalaccessexception{ mcontext = c; //用反射机制来获取资源中的图片id和尺寸 field[] fields = r.drawable.class.getdeclaredfields(); for (field field : fields) { if (!"icon".equals(field.getname()))//除了icon之外的图片 { int index=field.getint(r.drawable.class); //保存图片id imglist.add(index); //保存图片大小 int size[]=new int[2]; bitmap bmimg=bitmapfactory.decoderesource(getresources(),index); size[0]=bmimg.getwidth();size[1]=bmimg.getheight(); imgsizes.add(size); } } } @override public int getcount() { // todo auto-generated method stub return imglist.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub imageview i = new imageview (mcontext); //从imglist取得图片id i.setimageresource(imglist.get(position).intvalue()); i.setscaletype(imageview.scaletype.fit_xy); //从imgsizes取得图片大小 int size[]= new int[2]; size=(int[]) imgsizes.get(position); i.setlayoutparams(new gallery.layoutparams(size[0], size[1])); return i; } }; }