本節要介紹 Android 中用於輪播圖片的圖片切換器 ImageSwitcher,以及用於切換文字的文字切換器 TextSwitcher。
一、ImageSwitcher 圖片切換器
我們常看到許多網站首頁有個圖片輪播控制項,用來展示站內的重點新聞,這些網站多半採用 jQuery 等 JS 框架提供的輪播外掛,而在 Android 裡,ImageSwitcher 也提供了類似的功能。
那我們就動手做個例子感受一下:
1、新建一個專案:Lesson45_ImageSwitcher。
2、準備好 5 張順眼的圖片,放在 res\drawable 目錄下:

3、在 main.xml 中加入一個 ImageSwitcher 組件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
- <ImageSwitcher android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageSwitcher1">
- </ImageSwitcher>
- </LinearLayout>
4、MainActivity.java 的程式碼如下:
- package basic.android.lesson45;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.ViewSwitcher.ViewFactory;
- public class MainActivity extends Activity {
- // 當前顯示的圖片索引
- private int index;
- // 圖片陣列
- private int[] images = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
- R.drawable.image5 };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 全螢幕設定
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- // 取得 ImageSwitcher 物件
- final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
- // 實作並設定工廠內部介面的 makeView 方法,用來顯示視圖
- is.setFactory(new ViewFactory() {
- @Override
- public View makeView() {
- return new ImageView(MainActivity.this);
- }
- });
- // 設定圖片來源
- is.setImageResource(images[index]);
- // 設定點擊監聽器
- is.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 點擊會切換圖片
- index++;
- if (index >= images.length) {
- index = 0;
- }
- is.setImageResource(images[index]);
- }
- });
- // 設定切入動畫
- is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
- // 設定切出動畫
- is.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
- }
- }
5、編譯並運行程式,查看結果:
抱歉,我抓不到切換圖片那一瞬間的截圖。

二、TextSwitcher 文字切換器
文字切換的動畫,可以靠一個叫 TextSwitcher 的類來完成,它的用法與 ImageSwitcher 類似。至於為何兩者用法如此相似,看看下面兩張繼承關係圖就明白了:


下面直接上例子:
1、新建一個專案:Lesson45_TextSwitcher。
2、在 main.xml 中加入一個 TextSwitcher 組件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
- <TextSwitcher android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/textSwitcher1">
- </TextSwitcher>
- </LinearLayout>
3、MainActivity.java 的程式碼如下:
- package basic.android.lesson45;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.AnimationUtils;
- import android.widget.TextSwitcher;
- import android.widget.TextView;
- import android.widget.ViewSwitcher.ViewFactory;
- public class MainActivity extends Activity {
- // 索引
- private int index;
- // 文字陣列
- private String[] poemArray = { "去年今日栽", "臨去見花開", "好住守空院", "夜間人不來" };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 定義文字切換器
- final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);
- // 定義視圖顯示工廠,並設定
- ts.setFactory(new ViewFactory() {
- @Override
- public View makeView() {
- TextView tv = new TextView(MainActivity.this);
- tv.setTextSize(32);
- tv.setTextColor(Color.GREEN);
- return tv;
- }
- });
- // 設定文字來源
- ts.setText(poemArray[index]);
- // 設定點擊監聽器
- ts.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 點擊會切換文字
- index++;
- if (index >= poemArray.length) {
- index = 0;
- }
- ts.setText(poemArray[index]);
- }
- });
- // 設定切入動畫
- ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
- // 設定切出動畫
- ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
- }
- }
4、編譯並運行程式,查看結果:

抱歉,還是沒能截取到切換時的畫面。本節就講到這裡,相信大家都已經相當清楚了。