点击打开链接
前面的一节,介绍了如何组合使用Gallery和ImageView。但是,有的时候当你在gallery中点击一个图片,你可能不希望一个图片“突然地”在imageview中显示出来。例如,你可能希望给某个图片设置一些切换动画。此时,就需要使用ImageSwitcher和Gallery一起使用。下面展示如何使用ImageSwitcher。
1. 创建一个工程,ImageSwitcher。
2. main.xml中的代码。
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
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Images of San Francisco"
/>
<
Gallery
android:id
=
"@+id/gallery1"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<
ImageSwitcher
android:id
=
"@+id/switcher1"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentRight
=
"true"
android:layout_alignParentBottom
=
"true"
/>
</
LinearLayout
>
|
3. 在res/values文件夹下面新建一个文件,attrs.xml。
4. attrs.xml中的代码。
1
2
3
4
5
6
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
declare-styleable
name
=
"Gallery1"
>
<
attr
name
=
"android:galleryItemBackground"
/>
</
declare-styleable
>
</
resources
>
|
5. 在res/drawable-mdpi中放置一些图片。
6. ImageSwitcherActivity.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
108
109
|
public
class
ImageSwitcherActivity
extends
Activity
implements
ViewFactory {
//---the images to display---
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
private
ImageSwitcher imageSwitcher;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
imageSwitcher.setFactory(
this
);
/*
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
*/
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_out_right));
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(
new
ImageAdapter(
this
));
gallery.setOnItemClickListener(
new
OnItemClickListener()
{
public
void
onItemClick(AdapterView<?> parent,
View v,
int
position,
long
id)
{
imageSwitcher.setImageResource(imageIDs[position]);
}
});
}
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;
}
public
class
ImageAdapter
extends
BaseAdapter
{
private
Context context;
private
int
itemBackground;
public
ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground,
0
);
a.recycle();
}
//---returns the number of images---
public
int
getCount()
{
return
imageIDs.length;
}
//---returns the item---
public
Object getItem(
int
position)
{
return
position;
}
//---returns the ID of an item---
public
long
getItemId(
int
position)
{
return
position;
}
//---returns an ImageView view---
public
View getView(
int
position, View convertView, ViewGroup parent)
{
ImageView imageView =
new
ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new
Gallery.LayoutParams(
150
,
120
));
imageView.setBackgroundResource(itemBackground);
return
imageView;
}
}
}
|
7. 按F11在模拟器上面调试。会看见Gallery和ImageSwitcher。
首先,要注意,ImageSwitcherActivity不紧继承了Activity,而且实现了ViewFactory接口。要使用ImageSwitcher,就需要实现ViewFactory接口,这个接口为ImageSwitcher创建视图。只需实现makeView()方法就可以了。
1
2
3
4
5
6
7
8
9
10
11
|
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;
}
|
这个方法返回一个新的view,这个view将会被添加到ImageSwitcher中,在本例中,此方法返回一个ImageView。
像上一节的例子一样,我们要实现一个ImageAdapter子类,目的是把Gallery和ImageView绑定在一起。
在onCreate()方法中,我们获得ImageSwitcher实例的引用,同时设定了动画animation,这个动画在图片切换的时候被使用。
最后,当一个图片在Gallery中被选择的时候,这个图片就在ImageSwitcher中显示出来的。
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
|
private
ImageSwitcher imageSwitcher;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
imageSwitcher.setFactory(
this
);
/*
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
*/
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_out_right));
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(
new
ImageAdapter(
this
));
gallery.setOnItemClickListener(
new
OnItemClickListener()
{
public
void
onItemClick(AdapterView<?> parent,
View v,
int
position,
long
id)
{
imageSwitcher.setImageResource(imageIDs[position]);
}
});
}
|
在这个例子中,通过以下的方法设置动画。
1
2
3
4
5
6
7
8
9
10
11
|
/*
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
*/
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this
,
android.R.anim.slide_out_right));
|