01
02
03
04
05
06
07
|
private
int
[] resIds =
new
int
[]{
R.drawable.item1, R.drawable.item2, R.drawable.item3,
R.drawable.item4, R.drawable.item5, R.drawable.item6,
R.drawable.item7, R.drawable.item8, R.drawable.item9,
R.drawable.item10, R.drawable.item11, R.drawable.item12,
R.drawable.item13, R.drawable.item14, R.drawable.item15
};
|
01
02
03
04
05
06
07
08
09
10
11
|
<?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_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"30dp"
/>
</LinearLayout>
|
01
02
03
04
05
06
07
08
09
10
|
public
void
onCreate(Bundle savedInstanceState){
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 装载Gallery组件
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 创建用于描述图像数据的ImageAdapter对象
ImageAdapter imageAdapter =
new
ImageAdapter(
this
);
// 设置Gallery组件的Adapter对象
gallery.setAdapter(imageAdapter);
}
|
01
02
03
04
05
06
07
08
09
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
|
public
class
ImageAdapter
extends
BaseAdapter
{
int
mGalleryItemBackground;
private
Context mContext;
public
ImageAdapter(Context context)
{
mContext = context;
// 获得Gallery组件的属性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,
0
);
}
// 返回图像总数
public
int
getCount()
{
return
resIds.length;
}
public
Object getItem(
int
position)
{
return
position;
}
public
long
getItemId(
int
position)
{
return
position;
}
// 返回具体位置的ImageView对象
public
View getView(
int
position, View convertView, ViewGroup parent)
{
ImageView imageView =
new
ImageView(mContext);
// 设置当前图像的图像(position为当前图像列表的位置)
imageView.setImageResource(resIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new
Gallery.LayoutParams(
163
,
106
));
// 设置Gallery组件的背景风格
imageView.setBackgroundResource(mGalleryItemBackground);
return
imageView;
}
}
|
01
02
03
04
05
06
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<resources>
<declare-styleable name=
"Gallery"
>
<attr name=
"android:galleryItemBackground"
/>
</declare-styleable>
</resources>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
<font color=
"black"
><font face=
"微软雅黑"
><font size=
"3"
><?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_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"30dp"
/>
<ImageSwitcher
android:id=
"@+id/imageswitcher"
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"30dp"
/>
</LinearLayout></font></font></font>
|
01
02
03
04
05
06
07
08
09
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
108
109
110
111
112
113
114
115
|
package
eoe.demo;
import
android.app.Activity;
import
android.content.Context;
import
android.content.res.TypedArray;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.view.animation.AnimationUtils;
import
android.widget.AdapterView;
import
android.widget.BaseAdapter;
import
android.widget.Gallery;
import
android.widget.ImageSwitcher;
import
android.widget.ImageView;
import
android.widget.AdapterView.OnItemSelectedListener;
import
android.widget.Gallery.LayoutParams;
import
android.widget.ViewSwitcher.ViewFactory;
public
class
Main
extends
Activity
implements
OnItemSelectedListener,
ViewFactory{
private
Gallery gallery;
private
ImageSwitcher imageSwitcher;
private
ImageAdapter imageAdapter;
private
int
[] resIds =
new
int
[]
{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,
R.drawable.item5, R.drawable.item6, R.drawable.item7,
R.drawable.item8, R.drawable.item9, R.drawable.item10,
R.drawable.item11, R.drawable.item12, R.drawable.item13,
R.drawable.item14, R.drawable.item15 };
public
class
ImageAdapter
extends
BaseAdapter
{
int
mGalleryItemBackground;
private
Context mContext;
public
ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,
0
);
}
// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
public
int
getCount()
{
return
Integer.MAX_VALUE;
}
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 =
new
ImageView(mContext);
// 第2点改进,通过取余来循环取得resIds数组中的图像资源ID
imageView.setImageResource(resIds[position % resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new
Gallery.LayoutParams(
163
,
106
));
imageView.setBackgroundResource(mGalleryItemBackground);
return
imageView;
}
}
@Override
public
void
onItemSelected(AdapterView<?> parent, View view,
int
position,
long
id)
{
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
imageSwitcher.setImageResource(resIds[position % resIds.length]);
}
@Override
public
void
onNothingSelected(AdapterView<?> parent)
{
}
@Override
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
public
View makeView()
{
ImageView imageView =
new
ImageView(
this
);
imageView.setBackgroundColor(
0xFF000000
);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(
new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return
imageView;
}
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter =
new
ImageAdapter(
this
);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(
this
);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 设置ImageSwitcher组件的工厂对象
imageSwitcher.setFactory(
this
);
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.fade_out));
}
}
|