編輯:關於 Android 程式設計
先前寫過用 ViewFlipper 與 ViewPager 做畫面切換(可參考《Android 中使用 ViewFlipper 類實現屏幕切換》與《Android 中使用 ViewPager 實現屏幕頁面切換和頁面輪播效果》兩篇)。這次換一個更輕量的選擇:ImageSwitcher。
顧名思義,它就是「ImageView 的切換器」,最典型的應用就是幻燈片。它的運作原理其實很單純:內部固定持有兩個子 ImageView,切換時在這兩張之間交替顯示,搭配進出動畫就能做出淡入淡出的效果。
也因為子 View 是由它自己管理的,所以我們不能直接在版面檔裡塞 ImageView,必須實作 ViewFactory 的 makeView() 方法,由我們提供 ImageView 的實例。
先看成果(含自動輪播與導航圓點):

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>
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 更合適。
Android 用 setDataAndType 打開各種檔案:MIME 對照表與 Intent 範例大全
在 Android 中要用系統現有的應用打開某個檔案,關鍵是「正確的 MIME 類型」。只要透過 Intent.ACTION_VIEW 搭配 setDataAndType
如何讀寫服務器數據?——搭建 Tomcat 服務器並實現 GET/POST 請求
在本次項目中,我用 apache-tomcat 把自己的計算機變成了一台小服務器,然後對裡面的 jsp 類型文件進行讀寫。 首先,如何搭建服務器呢? 1. 下載一個 ap
Android 音樂播放器:MediaPlayer、SeekBar 與電話攔截
音樂播放器可整合 MediaPlayer、SeekBar、ListView、廣播接收者與電話狀態監聽。主要功能支援播放、暫停、上下一首、拖動進度、列表排序,並在來電時暫停
Android UI 之 FrameLayout(幀佈局)详解
FrameLayout 幀佈局介紹 幀佈局會為其中的每個元件開闢一塊空白區域(稱為「幀」),這些幀一層層疊加在一起,有點像一張張覆蓋貼上的海報,後加入的元件會把前面的元件