今天学习了Android的Gallery,根据自己的喜好做了一个NBA球星的幻灯片,也算是一边学习,一边自娱自乐吧。。。
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
|
package
com.test.gallery;
import
android.app.Activity;
import
android.content.res.TypedArray;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.ViewGroup.LayoutParams;
import
android.view.animation.AnimationUtils;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.BaseAdapter;
import
android.widget.Gallery;
import
android.widget.ImageSwitcher;
import
android.widget.ImageView;
import
android.widget.ViewSwitcher.ViewFactory;
public
class
TestGallleryActivity
extends
Activity {
int
[] imgIds =
new
int
[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,
R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,
R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16};
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.switcher);
switcher.setInAnimation(AnimationUtils.loadAnimation(
this
, android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(
this
, android.R.anim.fade_out));
switcher.setFactory(
new
ViewFactory() {
public
View makeView() {
ImageView img1 =
new
ImageView(TestGallleryActivity.
this
);
img1.setBackgroundColor(
0xff0000
);
img1.setScaleType(ImageView.ScaleType.FIT_CENTER);
img1.setLayoutParams(
new
ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return
img1;
}
});
BaseAdapter adapter =
new
BaseAdapter(){
public
int
getCount() {
// TODO Auto-generated method stub
return
imgIds.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) {
// TODO Auto-generated method stub
ImageView img2 =
new
ImageView(TestGallleryActivity.
this
);
img2.setImageResource(imgIds[position]);
img2.setScaleType(ImageView.ScaleType.FIT_XY);
img2.setLayoutParams(
new
Gallery.LayoutParams(
75
,
100
));
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
img2.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground,
0
));
return
img2;
}};
Gallery gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(
new
OnItemClickListener(){
public
void
onItemClick(AdapterView<?> arg0, View arg1,
int
arg2,
long
arg3) {
switcher.setImageResource(imgIds[arg2]);
}});
}
}
|