編輯:關於 Android 程式設計
不論是以 XML 撰寫還是以程式碼建立的動畫,都能定義連續播放的效果。一般建議使用 XML 來撰寫動畫,因為它更容易完成、更容易重複利用,也更方便日後修改。XML 動畫檔案放在 res/anim 目錄中,根節點必須是 <alpha>、<scale>、<translate>、<rotate> 或 <set> 之一。預設情況下所有動畫會同時進行,若要讓它們依序播放,可以透過 startOffset 屬性來控制。
以下範例示範縮放與旋轉動畫的組合,透過 startOffset 讓第二段動畫在第一段結束後才開始:
<set android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400"
android:fillBefore="false" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400" />
</set>
</set>
螢幕座標以左上角為原點 (0,0),往右與往下依序遞增。像 pivotX 這類值可以設為相對值,例如 50% 代表相對於自身的一半,若不加上百分比符號則代表相對於父容器的一半。此外也可以自訂 Interpolator(速度插入器)來控制動畫的速度曲線。
在程式碼中載入並啟動動畫的方式如下:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
若不呼叫 startAnimation,也可以改用 animation.setStartTime(),當時間抵達該點時動畫會自動播放。
Drawable 動畫能將一張張的 Drawable 資源依序播放出來,就像傳統的動畫一樣,其基礎類別為 AnimationDrawable。由於本質上是由 Drawable 資源組成,其 XML 檔案通常放在 res/drawable 中,以 <animation-list> 作為根節點,並以 <item> 描述每一格畫面:
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
其中 android:oneshot="true" 代表只播放一次並停在最後一張,設為 false 則會持續循環播放。以下範例示範在螢幕被觸碰時載入並播放動畫:
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
值得注意的是,start() 方法不能寫在 onCreate() 中呼叫,因為此時要播放動畫的 View 尚未被加入視窗。若想讓動畫一進入就立刻播放,可以覆寫 onWindowFocusChanged() 方法,在 Activity 被放到最前方時自動執行。
Android 中的 JSON 數據解析實戰
一、JSON 簡介 JSON 建立在兩種結構之上: (1)「名稱/值」對的集合。在不同語言中,它被理解為對象(object)、記錄(record)、結構(struct)、
自訂 View 實作:用 ViewPager 打造可縮放的封面畫廊
基本介紹 畫廊效果在許多 App 裡都看得到——滑到中央的圖片會放大,離開中央的會縮小,營造出封面瀏覽的層次感: 這是我閒暇時寫的小專案,完整源碼在 https:
21 天學會 Android 開發:MediaPlayer 影音播放實作
本文來談 MediaPlayer。它可以播放音訊與影片,是 Android 多媒體播放的核心類別。 如果只是要播影片,VideoView 更簡單易用;但客製化程度不如 M
Android 接入百度鷹眼軌跡:配送場景實戰
以外賣配送為例:配送員從接單開始上傳行駛軌跡,用戶端可以實時查看配送員的行駛路線。對於點外賣的用戶來說,能更直觀地看到訂單是否正在配送,不必再一遍遍打電話催商戶了。