Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學習指南之四十五:ImageSwitcher 與 TextSwitcher 使用教學

編輯:關於 Android 開發

本節要介紹 Android 中用於輪播圖片的圖片切換器 ImageSwitcher,以及用於切換文字的文字切換器 TextSwitcher。

一、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="https://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
  3.     <ImageSwitcher android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageSwitcher1">
  4.     </ImageSwitcher>
  5. </LinearLayout>

4、MainActivity.java 的程式碼如下:

  1. package basic.android.lesson45;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. import android.view.animation.AnimationUtils;
  8. import android.widget.ImageSwitcher;
  9. import android.widget.ImageView;
  10. import android.widget.ViewSwitcher.ViewFactory;
  11. public class MainActivity extends Activity {
  12.      // 當前顯示的圖片索引
  13.      private int index;
  14.      // 圖片陣列
  15.      private int[] images = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
  16.              R.drawable.image5 };
  17.      @Override
  18.      public void onCreate(Bundle savedInstanceState) {
  19.          super.onCreate(savedInstanceState);
  20.          // 全螢幕設定
  21.          requestWindowFeature(Window.FEATURE_NO_TITLE);
  22.          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  23.          setContentView(R.layout.main);
  24.          // 取得 ImageSwitcher 物件
  25.          final ImageSwitcher is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
  26.          // 實作並設定工廠內部介面的 makeView 方法,用來顯示視圖
  27.          is.setFactory(new ViewFactory() {
  28.              @Override
  29.              public View makeView() {
  30.                  return new ImageView(MainActivity.this);
  31.              }
  32.          });
  33.          // 設定圖片來源
  34.          is.setImageResource(images[index]);
  35.          // 設定點擊監聽器
  36.          is.setOnClickListener(new View.OnClickListener() {
  37.              @Override
  38.              public void onClick(View v) {
  39.                  // 點擊會切換圖片
  40.                  index++;
  41.                  if (index >= images.length) {
  42.                      index = 0;
  43.                  }
  44.                  is.setImageResource(images[index]);
  45.              }
  46.          });
  47.          // 設定切入動畫
  48.          is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
  49.          // 設定切出動畫
  50.          is.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
  51.      }
  52. }

5、編譯並運行程式,查看結果:

抱歉,我抓不到切換圖片那一瞬間的截圖。

二、TextSwitcher 文字切換器

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

下面直接上例子:

1、新建一個專案:Lesson45_TextSwitcher。

2、在 main.xml 中加入一個 TextSwitcher 組件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
  3.     <TextSwitcher android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/textSwitcher1">
  4.     </TextSwitcher>
  5. </LinearLayout>

3、MainActivity.java 的程式碼如下:

  1. package basic.android.lesson45;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.TextSwitcher;
  8. import android.widget.TextView;
  9. import android.widget.ViewSwitcher.ViewFactory;
  10. public class MainActivity extends Activity {
  11.      // 索引
  12.      private int index;
  13.      // 文字陣列
  14.      private String[] poemArray = { "去年今日栽", "臨去見花開", "好住守空院", "夜間人不來" };
  15.      @Override
  16.      public void onCreate(Bundle savedInstanceState) {
  17.          super.onCreate(savedInstanceState);
  18.          setContentView(R.layout.main);
  19.          // 定義文字切換器
  20.          final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);
  21.          // 定義視圖顯示工廠,並設定
  22.          ts.setFactory(new ViewFactory() {
  23.              @Override
  24.              public View makeView() {
  25.                  TextView tv = new TextView(MainActivity.this);
  26.                  tv.setTextSize(32);
  27.                  tv.setTextColor(Color.GREEN);
  28.                  return tv;
  29.              }
  30.          });
  31.          // 設定文字來源
  32.          ts.setText(poemArray[index]);
  33.          // 設定點擊監聽器
  34.          ts.setOnClickListener(new View.OnClickListener() {
  35.              @Override
  36.              public void onClick(View v) {
  37.                  // 點擊會切換文字
  38.                  index++;
  39.                  if (index >= poemArray.length) {
  40.                      index = 0;
  41.                  }
  42.                  ts.setText(poemArray[index]);
  43.              }
  44.          });
  45.          // 設定切入動畫
  46.          ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
  47.          // 設定切出動畫
  48.          ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
  49.      }
  50. }

4、編譯並運行程式,查看結果:

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved