編輯:關於 Android 程式設計
本篇示範如何為音樂播放器實作一個「控制按鈕向中心收縮滑動後淡出消失、點擊專輯封面再重新顯示」的動畫效果,讓介面在播放時更為簡潔。
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();
}
}
以 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>
動畫效果由「淡出」與「位移」組合而成,讓按鈕朝某個方向滑出並逐漸透明:
<?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)的動畫,即可營造出向中心收縮或向外散開的反射效果。
Android 離線緩存實作:Volley 與圖片快取打造斷網可讀體驗
離線體驗的核心,是在網路正常時把關鍵資料保存下來,讓使用者即使暫時斷網,也能看到上次載入的內容。新聞、社群、知識類應用常使用這種策略,以降低空白頁與等待焦慮。 在 And
酷狗音樂歌詞怎麼解鎖與上鎖?兩步輕鬆搞定
有時我們一邊聽歌、一邊操作桌面,又想讓歌詞持續顯示,為了不擋住桌面操作,通常會把歌詞鎖定。那麼酷狗音樂的歌詞該如何上鎖與解鎖呢?一起來看看。第一步,將滑鼠移到歌詞區域,會
從零跑通 Android JNI:把 C 語言編成 .so 給 Java 呼叫
前面寫的都是純 Java 開發 Android 應用,這次換個角度,談談如何透過 JNI 由 Java 呼叫 C 語言。為了讓整個位置關係更清楚,先看 Android 的
Servlet 學習筆記(六):Session 會話追蹤完整解析
Session 與 Cookie 有著相似之處,兩者都能用來記錄資料與狀態;真正的差異在於儲存的位置:Cookie 把資料保存在用戶端,而 Session 則把資料保存在