本文实例介绍的android的gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观。本文实例中的gallery的用法,主要实现用反射机制来动态读取资源中的图片。
该实例代码运行的效果图如下:
main.xml源码如下:
1
2
3
4
5
6
7
8
|
<?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程序源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
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;
}
};
}
|