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

Android 為播放器實作反射收縮動畫效果

編輯:關於 Android 程式設計

本篇示範如何為音樂播放器實作一個「控制按鈕向中心收縮滑動後淡出消失、點擊專輯封面再重新顯示」的動畫效果,讓介面在播放時更為簡潔。

主要程式碼(MainActivity)

package smalt.play.show;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Splayer2Activity extends Activity {
    ImageView ivImage, ivPlay, ivStop, ivPre, ivNext, ivShuffer, ivRepeat;
    Animation playAnim, stopAnim, preAnim, nextAnim, shufferAnim, repeatAnim;

    void initView() { // 初始化控制項
        ivImage = (ImageView) findViewById(R.id.player_image);
        ivPlay = (ImageView) findViewById(R.id.player_play);
        ivStop = (ImageView) findViewById(R.id.player_stop);
        ivNext = (ImageView) findViewById(R.id.player_next);
        ivPre = (ImageView) findViewById(R.id.player_pre);
        ivRepeat = (ImageView) findViewById(R.id.player_repeat);
        ivShuffer = (ImageView) findViewById(R.id.player_shuffle);
    }

    void initData() { demoAnim(); } // 啟動動畫

    void initListener() { // 監聽器
        playAnim.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) { }
            public void onAnimationRepeat(Animation animation) { }
            public void onAnimationEnd(Animation animation) {
                // 動畫結束後隱藏所有控制項
                ivPlay.setVisibility(View.GONE);
                ivStop.setVisibility(View.GONE);
                ivPre.setVisibility(View.GONE);
                ivNext.setVisibility(View.GONE);
                ivRepeat.setVisibility(View.GONE);
                ivShuffer.setVisibility(View.GONE);
            }
        });
        // 點擊專輯封面重新顯示控制項
        ivImage.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ivPlay.setVisibility(View.VISIBLE);
                ivStop.setVisibility(View.VISIBLE);
                ivPre.setVisibility(View.VISIBLE);
                ivNext.setVisibility(View.VISIBLE);
                ivRepeat.setVisibility(View.VISIBLE);
                ivShuffer.setVisibility(View.VISIBLE);
            }
        });
    }

    void demoAnim() { // 載入各按鈕動畫並延遲播放
        playAnim = AnimationUtils.loadAnimation(this, R.anim.play_out);
        stopAnim = AnimationUtils.loadAnimation(this, R.anim.stop_out);
        preAnim = AnimationUtils.loadAnimation(this, R.anim.pre_out);
        nextAnim = AnimationUtils.loadAnimation(this, R.anim.next_out);
        shufferAnim = AnimationUtils.loadAnimation(this, R.anim.shuffer_out);
        repeatAnim = AnimationUtils.loadAnimation(this, R.anim.repeat_out);
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            public void run() {
                ivPlay.startAnimation(playAnim);
                ivStop.startAnimation(stopAnim);
                ivPre.startAnimation(preAnim);
                ivNext.startAnimation(nextAnim);
                ivRepeat.startAnimation(repeatAnim);
                ivShuffer.startAnimation(shufferAnim);
            }
        }, 2000);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        initData();
        initListener();
    }
}

版面配置(main.xml 摘要)

以 RelativeLayout 為根,內含一個居中的 300dp × 300dp 容器,把專輯封面與六個控制按鈕以對齊屬性排列在封面四周:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true">

        <!-- 專輯圖片 -->
        <ImageView
            android:id="@+id/player_image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:src="@drawable/player_image" />

        <!-- 播放、停止、上一曲、下一曲、隨機、順序等 ImageView 以
             layout_alignParentTop / Bottom / Left / Right 等方式環繞排列 -->
    </RelativeLayout>
</RelativeLayout>

動畫資源(以 play_out 為例)

動畫效果由「淡出」與「位移」組合而成,讓按鈕朝某個方向滑出並逐漸透明:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    <translate
        android:duration="2500"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:toXDelta="-800%p"
        android:toYDelta="0%p" />
</set>

效果總結

載入頁面約 2 秒後,所有控制按鈕會朝各自方向滑動並淡出消失,畫面回歸乾淨的專輯封面;點擊封面後控制項又重新出現。透過為每個按鈕設計不同方向(toXDelta/toYDelta)的動畫,即可營造出向中心收縮或向外散開的反射效果。

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