編輯:關於 Android 程式設計
動畫是提升 App 使用體驗的重要手段。本文系統整理 Android 常見的三類動畫——幀動畫、補間動畫與屬性動畫,並附上畫面跳轉過場的實作方式,同時彙整了常用的插值器。
插值器決定了動畫的「速度曲線」,掌握它們能讓動畫更自然流暢:
| 插值器類 | xml 屬性值 | 說明 |
|---|---|---|
| LinearInterpolator | @android:anim/linear_interpolator | 動畫以均勻速度變化。 |
| AccelerateInterpolator | @android:anim/accelerate_interpolator | 開始時速度較慢,然後逐漸加速。 |
| AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 開始與結束時較慢,中間加速。 |
| CycleInterpolator | @android:anim/cycle_interpolator | 動畫循環播放特定次數,變化速度按正弦曲線改變。 |
| DecelerateInterpolator | @android:anim/decelerate_interpolator | 開始時速度較快,然後逐漸減速。 |
| AnticipateInterpolator | @android:anim/anticipate_interpolator | 先向反方向運動一段,再加速前進。 |
| AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 開始時先向後,然後向前甩過一定值後再回到最終值。 |
| BounceInterpolator | @android:anim/bounce_interpolator | 彈跳,快到目標值時會跳動。 |
| OvershootInterpolator | @android:anim/overshoot_interpolator | 回彈,先超出目標值再緩慢回到目標值。 |

以下程式碼展示了補間動畫(anim1)、幀動畫(anim2)以及屬性動畫(rotation、alpha)的啟動寫法:
if (id == R.id.action_settings) {
// 補間動畫實例
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.anim1);
an1.startAnimation(animation);
} else if (id == R.id.action_settings1) {
// 幀動畫
an2.setBackgroundResource(R.drawable.anim2);
animation = (AnimationDrawable) an2.getBackground();
animation.start();
} else if (id == R.id.action_settings2) {
// 屬性動畫:旋轉
ObjectAnimator oja = ObjectAnimator.ofFloat(an3, "rotation", 0, 360);
oja.setDuration(3000);
oja.setRepeatCount(1);
oja.setInterpolator(new BounceInterpolator());
oja.start();
} else if (id == R.id.action_settings3) {
// 屬性動畫:透明度漸變
ObjectAnimator oja = ObjectAnimator.ofFloat(an4, "alpha", 1, 0);
oja.setDuration(4000);
oja.setRepeatCount(1);
oja.setRepeatMode(ObjectAnimator.REVERSE);
oja.setInterpolator(new LinearInterpolator());
oja.start();
}

點擊後交回正常狀態:

點擊圖片觸發動畫效果:

動畫效果展示:

透過 onCreateOptionsMenu 載入選單,並在 onOptionsItemSelected 中依選項 id 觸發對應動畫。核心 Activity 程式碼如下:
public class Tool_barActivity extends AppCompatActivity {
private TextView an1;
private ImageView an2;
private AnimationDrawable animation;
private TextView an3;
private TextView an4;
private Button bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tool_bar);
an1 = (TextView) findViewById(R.id.an1);
an2 = (ImageView) findViewById(R.id.an2);
an3 = (TextView) findViewById(R.id.an3);
an4 = (TextView) findViewById(R.id.an4);
bt2 = (Button) findViewById(R.id.button2);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Tool_barActivity.this, MainActivity.class);
startActivity(intent);
// 系統自帶動畫樣式:左邊進右邊出
// overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
// 淡入淡出
// overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
// 自訂過場動畫
overridePendingTransition(R.anim.pop_enter, R.anim.pop_exit);
}
});
an2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (animation != null && animation.isRunning()) {
animation.stop();
}
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.anim1);
an1.startAnimation(animation);
} else if (id == R.id.action_settings1) {
an2.setBackgroundResource(R.drawable.anim2);
animation = (AnimationDrawable) an2.getBackground();
animation.start();
} else if (id == R.id.action_settings2) {
ObjectAnimator oja = ObjectAnimator.ofFloat(an3, "rotation", 0, 360);
oja.setDuration(3000);
oja.setRepeatCount(1);
oja.setInterpolator(new BounceInterpolator());
oja.start();
} else if (id == R.id.action_settings3) {
ObjectAnimator oja = ObjectAnimator.ofFloat(an4, "alpha", 1, 0);
oja.setDuration(4000);
oja.setRepeatCount(1);
oja.setRepeatMode(ObjectAnimator.REVERSE);
oja.setInterpolator(new LinearInterpolator());
oja.start();
}
return super.onOptionsItemSelected(item);
}
}
選單檔 menu_tool_bar.xml 用來定義各個動畫效果選項:
<menu xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
tools:context="com.example.jreduch728.Tool_barActivity">
<item android:icon="@mipmap/ic_launcher" android:id="@+id/home"
android:title="主頁" app:showAsAction="ifRoom|withText" />
<item android:icon="@mipmap/ic_launcher" android:id="@+id/home1"
android:title="查找" app:showAsAction="ifRoom|withText" />
<group>
<item android:icon="@mipmap/ic_launcher" android:id="@+id/action_settings"
android:orderInCategory="100" android:title="動畫效果0" app:showAsAction="never" />
<item android:icon="@mipmap/qqq" android:id="@+id/action_settings1"
android:orderInCategory="100" android:title="動畫效果1" app:showAsAction="never" />
<item android:icon="@mipmap/ic_launcher" android:id="@+id/action_settings2"
android:orderInCategory="100" android:title="動畫效果2" app:showAsAction="never" />
<item android:icon="@mipmap/ic_launcher" android:id="@+id/action_settings3"
android:orderInCategory="100" android:title="動畫效果3" app:showAsAction="never" />
</group>
</menu>
主介面佈局以 RelativeLayout 為基礎,依序排列動畫效果 1~3 的文字方塊、圖片、按鈕,並加入「跳轉畫面」按鈕演示過場動畫。



除了上述效果,本文還示範了以幀動畫搭配清單式登入介面,六個功能入口(資訊平台、校園活動、思想引領、就業招聘、考研出國、便利工具)以 LinearLayout 縱向排列,並以分隔線區隔。

掌握補間動畫、幀動畫與屬性動畫這三種方式,再加上自訂過場動畫,你就能為 App 打造出豐富而順滑的動態效果。
Android 為 Button 綁定點擊事件的五種寫法與取捨
一、開場 這篇整理 Android 裡替 Button 綁定事件的五種常見寫法,並順便談談各自的優缺。 二、五種實作 第一種:直接在 XML 上綁定 步驟一,在 Butt
Android 6.0 來電(MT)流程源碼全解析
本文基於對 telephony 模塊的研究,結合網上資料與個人理解,總結了 Android 6.0 中 MT(Mobile Terminated,移動主叫被叫,即來電)的
Android 初學之路:Activity 元件入門
過去我的程式設計經驗幾乎都在 PC 上,且主要集中在演算法與資料結構。由於某些需要,加上意識到 Android 的重要性,而大學階段課業不重、空閒時間很多,於是便開始學習
靠譜助手卡頓怎麼辦:四個方向一次試
靠譜助手是國內相當知名的電腦端安卓模擬器,用的人不少。但它最被詬病的一點就是卡,而且幾乎沒有可調的空間——一個小程式都能跑得很滯,畫面與操作都不順。綜合官方與使用者回饋來