Android入门之Gallery用法实例解析
程序员文章站
2024-01-23 11:55:40
本文实例介绍的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; } }; }
上一篇: Linux系统下NTP协议的超级配置攻略
推荐阅读
-
Android入门之Gallery用法实例解析
-
Android入门之Style与Theme用法实例解析
-
Android入门之PopupWindow用法实例解析
-
Android入门之AlertDialog用法实例分析
-
Android入门——数据解析之使用GSON解析JSON字符串(二)
-
Android开发之占位符应用实例解析
-
android开发之listView组件用法实例简析
-
Android编程四大组件之Activity用法实例分析
-
Android编程四大组件之BroadcastReceiver(广播接收者)用法实例
-
Android开发之浏览器用法实例详解(调用uc,opera,qq浏览器访问网页)