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

Android 動畫效果全解析:幀動畫、補間動畫與畫面跳轉

編輯:關於 Android 程式設計

動畫是提升 App 使用體驗的重要手段。本文系統整理 Android 常見的三類動畫——幀動畫、補間動畫與屬性動畫,並附上畫面跳轉過場的實作方式,同時彙整了常用的插值器。

一、常用插值器(Interpolator)

插值器決定了動畫的「速度曲線」,掌握它們能讓動畫更自然流暢:

插值器類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 打造出豐富而順滑的動態效果。

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