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

用 ImageSwitcher 實作圖片輪播與導航圓點

編輯:關於 Android 程式設計

先前寫過用 ViewFlipper 與 ViewPager 做畫面切換(可參考《Android 中使用 ViewFlipper 類實現屏幕切換》與《Android 中使用 ViewPager 實現屏幕頁面切換和頁面輪播效果》兩篇)。這次換一個更輕量的選擇:ImageSwitcher。

ImageSwitcher 到底是什麼?

顧名思義,它就是「ImageView 的切換器」,最典型的應用就是幻燈片。它的運作原理其實很單純:內部固定持有兩個子 ImageView,切換時在這兩張之間交替顯示,搭配進出動畫就能做出淡入淡出的效果。

也因為子 View 是由它自己管理的,所以我們不能直接在版面檔裡塞 ImageView,必須實作 ViewFactory 的 makeView() 方法,由我們提供 ImageView 的實例。

先看成果(含自動輪播與導航圓點):

第一步:主版面 activity_main.xml

FrameLayout 讓 ImageSwitcher 填滿畫面,底部的 LinearLayout 疊在上面放導航圓點。圓點也可以改成用程式依圖片數量動態產生,彈性更好。

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:id="@+id/activity_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.example.administrator.switcher.MainActivity">

     <ImageSwitcher
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/is">
     </ImageSwitcher>

     <LinearLayout
         android:id="@+id/point_layout"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         android:orientation="horizontal">
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:src="@mipmap/default_holo"/>
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:src="@mipmap/default_holo"/>
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:src="@mipmap/default_holo"/>
         <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:src="@mipmap/default_holo"/>
     </LinearLayout>
 </FrameLayout>

第二步:MainActivity.java

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.ImageSwitcher;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ViewSwitcher;

 import java.util.ArrayList;

 public class MainActivity extends AppCompatActivity
         implements ViewSwitcher.ViewFactory, View.OnTouchListener {

     private ImageSwitcher is;          // 宣告 ImageSwitcher
     private LinearLayout point_layout; // 宣告導航圓點的容器

     int[] images = { R.mipmap.a1, R.mipmap.a2, R.mipmap.a3, R.mipmap.a4 };

     // 存放導航圓點
     ArrayList<ImageView> points = new ArrayList<>();

     int index;    // 目前圖片在陣列中的下標
     float startX; // 手指接觸螢幕時的 X 座標
     float endX;   // 手指離開螢幕時的 X 座標

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         is = (ImageSwitcher) findViewById(R.id.is);
         is.setFactory(this);            // 透過 Factory 提供 ImageSwitcher 使用的 ImageView
         initpoint();
         is.setOnTouchListener(this);
     }

     // 初始化導航圓點
     private void initpoint() {
         point_layout = (LinearLayout) findViewById(R.id.point_layout);
         int count = point_layout.getChildCount(); // 取得圓點數量
         for (int i = 0; i < count; i++) {
             points.add((ImageView) point_layout.getChildAt(i));
         }
         // 第一張圖(下標 0)對應的圓點設為實心
         points.get(0).setImageResource(R.mipmap.touched_holo);
     }

     // 切換圓點狀態:只有目前那張是實心
     public void setImageBackground(int selectImage) {
         for (int i = 0; i < points.size(); i++) {
             if (i == selectImage) {
                 points.get(i).setImageResource(R.mipmap.touched_holo);
             } else {
                 points.get(i).setImageResource(R.mipmap.default_holo);
             }
         }
     }

     // ViewFactory 的方法:實例化切換用的 ImageView
     @Override
     public View makeView() {
         ImageView iv = new ImageView(this);
         iv.setImageResource(images[0]);
         return iv;
     }

     @Override
     public boolean onTouch(View v, MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN) {
             startX = event.getX(); // 按下時記下 X 座標
         } else if (event.getAction() == MotionEvent.ACTION_UP) {
             endX = event.getX();

             if (startX - endX > 30) {
                 // 左滑 → 下一張;已在最後一張則回到第一張
                 index = index + 1 < images.length ? ++index : 0;
                 is.setInAnimation(this, android.R.anim.fade_in);
                 is.setOutAnimation(this, android.R.anim.fade_out);
             } else if (endX - startX > 30) {
                 // 右滑 → 上一張;已在第一張則跳到最後一張
                 index = index - 1 >= 0 ? --index : images.length - 1;
                 is.setInAnimation(this, android.R.anim.fade_in);
                 is.setOutAnimation(this, android.R.anim.fade_out);
             }

             is.setImageResource(images[index]);
             setImageBackground(index); // 同步更新圓點
         }
         return true;
     }
 }

三種切換方案的取捨

單純就「圖片輪播」這個需求而言,ImageSwitcher 的實作量明顯少於 ViewFlipper 與 ViewPager,適合頁數固定、內容簡單的橫幅。若需要「無限循環的自動播放」,可以搭配 Handler.postDelayed() 定時呼叫切換;若頁面內容較複雜或需要預先快取,還是 ViewPager2 更合適。

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