編輯:Android 開發實例
本文要說明如何實作一個「介面跳動提示」的動畫效果,藉由讓頁面上緣微微跳動,暗示使用者可以往上拉動來切換頁面。先來看效果圖:


先來看版面配置:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/red" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2blue" android:layout_centerHorizontal="true" android:text="首頁" /> <Button android:id="@+id/switch2blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="置換位藍色" /> </RelativeLayout> <RelativeLayout android:id="@+id/blue" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2red" android:layout_centerHorizontal="true" android:text="第二頁" /> <Button android:id="@+id/switch2red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="置換位紅色" /> </RelativeLayout> </RelativeLayout>
程式碼如下:
import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.DisplayMetrics; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnGestureListener { private RelativeLayout red, blue; private Button switch2blue, switch2red; private float thisDelta = 0.05f; private static boolean hasEverPulled = false; private boolean pulled = false; private static int height; private GestureDetector gestureDetector; private static int statusHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayMetrics metrics = getResources().getDisplayMetrics(); height = metrics.heightPixels; statusHeight = getStatusHeight(); initView(); // 跳動提示可以上拉 final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { jump(thisDelta); // handler.postDelayed(this, 3000); } }, 3000); } private void initView() { red = (RelativeLayout) findViewById(R.id.red); blue = (RelativeLayout) findViewById(R.id.blue); switch2blue = (Button) findViewById(R.id.switch2blue); switch2red = (Button) findViewById(R.id.switch2red); switch2blue.setOnClickListener(this); switch2red.setOnClickListener(this); blue.setOnTouchListener(this); blue.setLongClickable(true); gestureDetector = new GestureDetector(this, this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.switch2blue: red2BlueUI(); break; case R.id.switch2red: blue2RedUI(); break; } } // 從紅頁面轉到藍頁面 private void red2BlueUI() { blue.bringToFront(); blue.requestLayout(); blue.invalidate(); } // 從藍頁面轉到紅頁面 private void blue2RedUI() { red.bringToFront(); red.requestLayout(); red.invalidate(); } // 獲取狀態欄的高度 private int getStatusHeight() { Class<?> c = null; Object obj = null; Field field = null; int x = 0; int height = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); height = getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return height; } // 主頁面跳動 private void jump(float delta) { if (thisDelta - 0.03f < 0.001f) { thisDelta = 0.05f; return; } thisDelta = delta; if (hasEverPulled) { return; } playJumpAnimation(thisDelta); } // 上拉/下拉主頁面 private void pull(boolean upward) { if (upward && pulled) { return; } if (!upward && !pulled) { return; } float originalY; float finalY; if (!pulled) { originalY = 0; finalY = (float) (0 - height + 0.4 * height); } else { originalY = (float) (0 - height + 0.4 * height); finalY = 0; } pulled = !pulled; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(300); animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (!pulled) { red2BlueUI(); } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { // if (pulled) { // blue2RedUI(); // } } }); blue.startAnimation(animationSet); hasEverPulled = true; } // 跳起動畫 private void playJumpAnimation(final float delta) { float originalY = 0; float finalY = 0 - height * delta; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(300); animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { playLandAnimation(delta); } }); blue.startAnimation(animationSet); } // 落下動畫 private void playLandAnimation(final float delta) { float originalY = 0 - height * delta; float finalY = 0; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(200); animationSet.setInterpolator(new AccelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { jump(0.03f); } }); blue.startAnimation(animationSet); } @Override public boolean onDown(MotionEvent e) { pull(false); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 手勢滑動達到100才觸發 if (e1.getY() - e2.getY() > 100) { pull(true); } else if (e2.getY() >= e1.getY()) { pull(false); } return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onTouch(View v, MotionEvent event) { if (pulled) { // 首張頁可觸控點 if (event.getY() > height * 0.4 - statusHeight) { return false; } } return gestureDetector.onTouchEvent(event); } }整體設計透過 TranslateAnimation 製作跳起與落下動畫,並用 GestureDetector 判斷上滑手勢。跳動幅度(thisDelta)會逐次遞減,形成越跳越小的自然提示感;一旦使用者真的往上拉(hasEverPulled 為真),跳動便停止,改由 pull() 完成頁面切換。這種細節能大幅提升使用者對「可上拉」操作的察覺度。
希望本文的說明能對你的 Android 開發有所幫助。
Android 開發最佳實踐:輸入體驗、背景作業、效能與資安
寫 Android 時有一些做法值得養成習慣。這些建議一部分來自官方文件,一部分是社群在長期實務中沉澱出來的共識,涵蓋互動設計、效能、安全性與隱私、相容性、測試、散布與變
Android MediaPlayer 教學:打造可播放、暫停與快轉的音樂播放器
要在 App 裡播放音訊或影片,最直接的方式是使用 Android 內建的 MediaPlayer 類別。它封裝了底層的播放服務,讓你不必煩惱解碼細節,專心處理播放控制。
Android 本地化:讓應用程式適配不同語言環境
Android 應用程式可以在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性,它應該能處理文字、數字、檔案等內容,使其適配應用程式即將使用的語言環境。 本章將說明
Android 本地化教學:讓 App 依地區顯示對應語言
Android 應用會運行在不同語言環境的裝置上。要讓應用更具親和力,就必須讓文字、數字與檔案等资源能配合裝置的語言環境自動調整。本章說明如何依不同區域(Locale)本