編輯:關於 Android 程式設計
Android 提供了 View Animation、Drawable Animation 與 Property Animation 三套動畫體系。View Animation 最簡單,支援平移、縮放、旋轉、透明度等基本變換;Drawable Animation 分為逐幀與補間兩類;Property Animation 功能最強,某個角度可以視為補間動畫的強化版本。
屬性動畫需要定義的屬性如下:
屬性動畫還需要 Evaluator 決定「怎麼算」:
步驟很固定:先用 ofInt()、ofFloat() 或 ofObject() 建立物件,再用 setXxx() 設定 duration、interpolator、repeatCount,然後 start() 啟動。但只做到這裡,畫面上什麼都不會發生——因為值還沒被套用到任何物件。
animator = ValueAnimator.ofFloat(0f, 300); animator.setDuration(4000); animator.start();
要看到效果,必須註冊 AnimatorUpdateListener,透過 getAnimatedValue() 取出當前幀的值,再自行套用:
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mImageView.setTranslationX((Float) animation.getAnimatedValue());
}
});

若預設的計算規則不合用,也可以自訂 Evaluator:
animator = ValueAnimator.ofObject(new CustomEvaluator(), startVal, endVal); animator.setDuration(1000); animator.start();
ObjectAnimator 繼承自 ValueAnimator,能把計算出的值直接套到指定屬性上,因此不需要再註冊 AnimatorUpdateListener。
public class ObjectAnimatorActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_animator);
}
public void changeAlpha(final View view) {
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 1.0F, 0.2F);
anim.setDuration(500);
anim.start();
}
}

使用 ObjectAnimator 有兩點要留意:
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
view.postInvalidate();
}
});
直覺做法是 AnimatorSet,但只用 ObjectAnimator 也做得到。技巧是把屬性名稱換成一個實際不存在的字串,讓它單純作為「時間驅動器」,真正的套用動作自己寫:
public void changeAlpha(final View view) {
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "xmr", 1.0F, 0.2F);
anim.setDuration(5000);
anim.start();
anim.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (Float) animation.getAnimatedValue();
view.setAlpha(value);
view.setScaleX(value);
view.setScaleY(value);
}
});
}

更正式的作法是用 PropertyValuesHolder:
public void changeAlpha(View view) {
PropertyValuesHolder pvhAlpha = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
ObjectAnimator.ofPropertyValuesHolder(view, pvhAlpha, pvhX, pvhY)
.setDuration(5000)
.start();
}
動畫多半只是輔助效果。做淡出移除時,透明度歸零不代表可以就此了事,視圖還占著位置,必須在動畫真正結束後把它從父容器中移除。
public void fadeOut(View view) {
ObjectAnimator anim = ObjectAnimator.ofFloat(mImageView, "alpha", 0.5f);
anim.setDuration(5000);
anim.start();
anim.addListener(new AnimatorListener() {
@Override public void onAnimationStart(Animator animation) { }
@Override public void onAnimationRepeat(Animator animation) { }
@Override public void onAnimationCancel(Animator animation) { }
@Override
public void onAnimationEnd(Animator animation) {
ViewGroup parent = (ViewGroup) mImageView.getParent();
if (parent != null) {
parent.removeView(mImageView);
}
}
});
}
如果只在意結束事件,用 AnimatorListenerAdapter 更省事——它實作了 AnimatorListener 介面並把方法都留成空實作:
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewGroup parent = (ViewGroup) mImageView.getParent();
if (parent != null) {
parent.removeView(mImageView);
}
}
});

同時播放用 playTogether,循序播放用 playSequentially:
public void togetherRun(View view) {
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.0f, 2.0f);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mImageView, "scaleY", 1.0f, 2.0f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(5000);
animSet.setInterpolator(new LinearInterpolator());
animSet.playTogether(anim1, anim2);
animSet.start();
}
需要更細膩的排程時,用 play().with() 與 play().after():
public void playWithAfter(View view) {
float cx = mImageView.getX();
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.0f, 2.0f);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mImageView, "scaleY", 1.0f, 2.0f);
ObjectAnimator anim3 = ObjectAnimator.ofFloat(mImageView, "x", cx, 0.0f);
ObjectAnimator anim4 = ObjectAnimator.ofFloat(mImageView, "x", cx);
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim1).with(anim2);
animSet.play(anim2).with(anim3);
animSet.play(anim4).after(anim3);
animSet.setDuration(2000);
animSet.start();
}

最後提醒一個容易踩的坑:雖然 play().with() 支援鏈式呼叫,但不要把順序寫成一長串,例如 play(anim1).with(anim2).before(anim3).before(anim5)。系統不會照你寫的先後去排,結果往往不如預期。老實照範例那樣分成幾行寫,反而最可靠。
Android 與設計模式:觀察者(Observer)模式的源碼實踐
閻宏博士在《JAVA與模式》一書開頭是這樣描述觀察者模式的: 觀察者模式屬於對象的行為模式,又叫發布-訂閱(Publish/Subscribe)模式、模型-檢視(Mode
自定義 View 實作折線圖:從 attrs 屬性到 Path 繪製的完整拆解
上一篇雷達圖結尾留了個坑——折線圖。折線圖大概初中數學就學過,用來看一段時間內某個數據的走向;考試題裡最常見的例子就是某學生的成績變化。 這次仍然拿 max+ 的折線圖
Android SeekBar 仿淘寶滑動驗證效果實作
SeekBar 是一個拖動條控制項,最簡單的案例就是調節音量,以及音訊、影片的播放進度。傳統 SeekBar 樣式如下圖:傳統實現太簡單,不足以展現效果。本來打算做滴滴出
Android N 行為變更整理:這些調整可能會影響你的應用
Android N 除了帶來大量新功能,也對系統與 API 的既有行為做了不少調整。以下幾項最值得留意,如果你曾經上架過應用,請務必評估影響。 效能相關調整 為了解決電