編輯:關於 Android 程式設計
這裡說的螢幕切換,指的是同一個 Activity 內部幾個畫面之間的轉場。ViewFlipper 繼承自 FrameLayout,搭配 ViewAnimator 就能為子 View 的切換加上動畫。先看成果:

setInAnimation:設定 View 進場動畫。有兩個版本,一個接收 Animation 物件,另一個接收 Context 與動畫資源 ID。setOutAnimation:設定 View 退場動畫,參數同上。showNext:顯示 FrameLayout 中的下一個 View。showPrevious:顯示上一個 View。
假設目前顯示的是圖二,要看圖一,就讓圖一由左向右進入,X 軸從 -100%p 移動到 0;要看圖三,則由右向左進入,X 軸從 100%p 變為 0。由於是橫向滑動,Y 軸不涉及。釐清起點與終點後,四個動畫檔就很直觀了(放在 res 下自建的 anim 目錄)。
in_leftright.xml — 由左向右進場:
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:duration="500"
android:fromXDelta="-100%p"
android:toXDelta="0"/>
</set>
out_leftright.xml — 由左向右離場:
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p"/>
</set>
in_rightleft.xml — 由右向左進場:
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0"/>
</set>
out_rightleft.xml — 由右向左離場:
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate android:duration="500"
android:fromXDelta="0"
android:toXDelta="-100%p"/>
</set>
直接把三張圖包成 LinearLayout 放進 ViewFlipper:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewFlipper
android:id="@+id/vf"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/sample_1" />
</LinearLayout>
<!-- sample_2、sample_3 結構相同 -->
</ViewFlipper>
</LinearLayout>
public class FlipperActivity extends AppCompatActivity
implements View.OnTouchListener {
private ViewFlipper vf;
float startX; // 手指按下時的 X 座標
float endX; // 手指抬起時的 X 座標
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewflipper_layout);
vf = (ViewFlipper) findViewById(R.id.vf);
vf.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startX = event.getX();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
endX = event.getX();
if (startX > endX) {
// 由右向左滑,圖片從右側進場
vf.setInAnimation(this, R.anim.in_rightleft);
vf.setOutAnimation(this, R.anim.out_rightleft);
vf.showNext();
} else if (startX < endX) {
// 由左向右滑,圖片從左側進場
vf.setInAnimation(this, R.anim.in_leftright);
vf.setOutAnimation(this, R.anim.out_leftright);
vf.showPrevious();
}
}
return true;
}
}
剛開始我被動畫的呈現方式困擾了很久,後來索性畫了張座標圖,把每個動畫的起點終點標出來,瞬間豁然開朗。不過要提醒的是:Android 的螢幕座標系統和數學座標不同,原點在左上角且 Y 軸向下遞增。我最初畫的版本把原點放在左下角,經朋友提醒後查證才發現有誤,修正如下:

寫自定義動畫時,先把座標系搞清楚,能省下大量試錯時間。
Android 之圖片變換:縮放、旋轉、剪裁與保存
說到圖片,第一反應就是 Bitmap,那就先來認識一下 Bitmap。 Bitmap 是 Android 系統中圖像處理最重要的類之一。用它可以獲取圖像文件信息,進行圖像
Android 模糊效果詳解:BlurMaskFilter 的四種模式對照說明
在 Android 中,邊緣模糊效果是透過 BlurMaskFilter 實現的。使用時需要指定一個模糊半徑與一種模糊風格(Blur),而不同的風格會帶來截然不同的視覺結
SearchView基本使用方法入門(含suggestion實現)
SearchView 是 Android 系統內建的搜索框組件,加到界面上很方便。但它有個明顯的短板:UI 是固定的,定製起來相當麻煩。如果設計稿對搜索框的要求比較高,與
Android 底層開發:音訊輸入通道的軟硬體全面解析
在手機與平板這類裝置上,最常見的麥克風形式是整合在四段式耳機插頭中的 Mic。不過並非所有情境都如此單一,例如在普通 PC 上安裝 Android 系統時,就可能遇到麥克