一、ImageSwitcher 图片切换器
我们可以看到很多网站首页里的有个图片轮显控件,用来显示站内重点新闻等,在这些网站里很多采用了JQuery等JS框架提供的轮显插件,而在Android里也有这个ImageSwitcher提供了类似的功能。
那么我们就一起做一个例子感觉一下:
1、新建一个项目:Lesson45_ImageSwitcher
2、准备好5张看着顺眼的图片,放在res\drawable目录下:

3、在main.xml中添加一个ImageSwitcher组件:
1 | <?xml version= "1.0" encoding= "utf-8" ?> |
2 | <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > |
3 | <imageswitcher android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/imageSwitcher1" > |
4、在MainActivity.java中的代码如下:
01 | package basic.android.lesson45; |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.view.Window; |
07 | import android.view.WindowManager; |
08 | import android.view.animation.AnimationUtils; |
09 | import android.widget.ImageSwitcher; |
10 | import android.widget.ImageView; |
11 | import android.widget.ViewSwitcher.ViewFactory; |
13 | public class MainActivity extends
Activity { |
19 | private int [] images = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, |
22 | /** Called when the activity is first created. */ |
24 | public void onCreate(Bundle savedInstanceState) { |
26 | super .onCreate(savedInstanceState); |
29 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
30 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); |
32 | setContentView(R.layout.main); |
35 | final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.imageSwitcher1); |
38 | is.setFactory( new ViewFactory() { |
40 | public View makeView() { |
41 | return new ImageView(MainActivity. this ); |
46 | is.setImageResource(images[index]); |
49 | is.setOnClickListener( new View.OnClickListener() { |
52 | public void onClick(View v) { |
55 | if (index >= images.length) { |
58 | is.setImageResource(images[index]); |
63 | is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left)); |
65 | is.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right)); |
5、编译并运行程序,查看结果:
抱歉我抓不到切换图片瞬间的截图。

二、TextSwitcher 文本切换器
文本的切换动画也是有一个叫TextSwitcher的类可以做到,它的使用方法和ImageSwitcher类似。至于为什么用法如此相似,还是看下面两张继承关系图吧:


下面直接上例子:
1、新建一个项目:Lesson45_TextSwitcher
2、在main.xml中添加一个TextSwitcher组件:
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < linearlayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > |
3 | < textswitcher android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/textSwitcher1" > |
3、在MainActivity.java中的代码如下:
01 | package basic.android.lesson45; |
03 | import android.app.Activity; |
04 | import android.graphics.Color; |
05 | import android.os.Bundle; |
06 | import android.view.View; |
07 | import android.view.animation.AnimationUtils; |
08 | import android.widget.TextSwitcher; |
09 | import android.widget.TextView; |
10 | import android.widget.ViewSwitcher.ViewFactory; |
12 | public class MainActivity extends
Activity { |
17 | private String[] poemArray = { "去年今日栽" , "临去见花开" , "好住守空院" , "夜间人不来" }; |
19 | /** Called when the activity is first created. */ |
21 | public void onCreate(Bundle savedInstanceState) { |
22 | super .onCreate(savedInstanceState); |
23 | setContentView(R.layout.main); |
26 | final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1); |
29 | ts.setFactory( new ViewFactory() { |
32 | public View makeView() { |
33 | TextView tv = new TextView(MainActivity. this ); |
35 | tv.setTextColor(Color.GREEN); |
41 | ts.setText(poemArray[index]); |
44 | ts.setOnClickListener( new View.OnClickListener() { |
47 | public void onClick(View v) { |
50 | if (index >= poemArray.length) { |
53 | ts.setText(poemArray[index]); |
58 | ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left)); |
60 | ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right)); |
4、编译并运行程序,查看结果:
抱歉还是没法截取到切换时的场景#_#
好吧,本讲就到这里,各位下次再见。