編輯:關於 Android 程式設計
之前談過 ActivityOptions 那套優雅的轉場動畫,但那份參考資料裡其實還藏了不少好東西。這篇就把剩下的部分一次補齊。
Material Design 最標誌性的視覺之一,就是點擊時那圈向外擴散的水波。好消息是它可以自訂,而且只需改 background 屬性。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:dividerPadding="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:text="@string/hello_world" />
</LinearLayout>
兩個按鈕唯一的差別在 background:selectableItemBackground 的漣漪被限制在按鈕自身範圍內;加上 Borderless 的版本則不受按鈕大小約束,波紋可以溢出邊界。

想自己定義顏色?那就得請出 Android 5.0 新增的 RippleDrawable。它同樣是 Drawable,所以一樣能用 XML 宣告:
<!-- drawable/ripple_no_mask.xml -->
<ripple xmlns:android="https://schemas.android.com/apk/res/android"
android:color="#FF00FF00">
<item android:drawable="@mipmap/ic_launcher" />
</ripple>
<!-- drawable/ripple_mask.xml -->
<ripple xmlns:android="https://schemas.android.com/apk/res/android"
android:color="#FF00FF00">
<item android:id="@android:id/mask"
android:drawable="@mipmap/ic_launcher" />
</ripple>
兩份檔案唯一的差異是 id:指定 @android:id/mask 的那一份,預設不會顯示 item 的 drawable,只在點擊瞬間出現;沒指定的則會常駐顯示。使用方式就是把 background 指過去:
<Button android:background="@drawable/ripple_no_mask" ... />
<Button android:background="@drawable/ripple_mask" ... />

漣漪只會出現在 item drawable 的非透明區域;指定 mask 時,預設背景不顯示。
新的 SDK 提供 ViewAnimationUtils,其中靜態方法 createCircularReveal 能讓任何 View 以圓形擴散的方式出現或消失。

public void change(View view) {
int centerX = mImageView.getWidth() / 2;
int centerY = mImageView.getHeight() / 2;
int maxRadius = Math.max(mImageView.getWidth(), mImageView.getHeight());
if (mImageView.getVisibility() == View.VISIBLE) {
// 由大收斂到 0:隱藏
Animator anim = ViewAnimationUtils.createCircularReveal(
mImageView, centerX, centerY, maxRadius, 0);
anim.setDuration(1000);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mImageView.setVisibility(View.GONE);
}
});
anim.start();
} else {
// 由 0 擴散到最大:顯示
Animator anim = ViewAnimationUtils.createCircularReveal(
mImageView, centerX, centerY, 0, maxRadius);
anim.setDuration(1000);
mImageView.setVisibility(View.VISIBLE);
anim.start();
}
}
方法簽章是 createCircularReveal(View view, int centerX, int centerY, float startRadius, float endRadius),五個參數分別對應:作用對象、圓心 X、圓心 Y、起始半徑、結束半徑。顯示與隱藏其實是同一段邏輯,只是把起訖半徑對調。隱藏時記得在動畫結束的 listener 裡才設 GONE,否則畫面會在動畫途中就消失。
這部分先前已在《你所不知道的 Activity 轉場動畫——ActivityOptions》中詳細說明,此處不再重複。
官方文件稱之為 Curved Motion,因為核心建立在 Path 上。Android 5 提供了 PathInterpolator:
<pathInterpolator xmlns:android="https://schemas.android.com/apk/res/android"
android:controlX1="0.4" android:controlY1="0"
android:controlX2="1" android:controlY2="1" />
XML 寫法只能定義插值曲線,變化有限。更實用的是在 Java 端直接建 Path——新版 ObjectAnimator 多了帶 Path 參數的重載:
public static ObjectAnimator ofFloat(Object target,
String xPropertyName, String yPropertyName, Path path);
傳入 x 與 y 兩個屬性名稱,再給一條路徑,物件就會沿著這條線移動。以下讓一段文字沿 Z 字型走一圈後回到原點:
TextView tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Path path = new Path();
path.moveTo(100, 100);
path.lineTo(600, 100);
path.lineTo(100, 600);
path.lineTo(600, 600);
path.close();
ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.X, View.Y, path);
anim.setDuration(5000);
anim.start();
}
});

過去用 selector 定義狀態,切換是硬生生的跳變。現在 item 裡可以放 objectAnimator,讓狀態轉換自帶動畫。
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<set>
<objectAnimator
android:propertyName="translationZ"
android:duration="100"
android:valueTo="12dp"
android:valueType="floatType" />
</set>
</item>
<item>
<set>
<objectAnimator
android:propertyName="translationZ"
android:duration="100"
android:valueTo="0dp"
android:valueType="floatType" />
</set>
</item>
</selector>
佈局中透過新屬性 android:stateListAnimator 掛上它:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stateListAnimator="@drawable/view_state"
android:text="@string/hello_world" />

仔細看,按下時 Button 的 translationZ 有一段平滑的過渡。那背景能不能也一起動?可以,改用 AnimatedStateListDrawable,在 XML 中寫成 animated-selector:
<animated-selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@+id/press"
android:drawable="@android:color/holo_blue_dark"
android:state_pressed="true" />
<item android:id="@+id/focus"
android:drawable="@android:color/holo_purple"
android:state_focused="true" />
<item android:id="@+id/normal"
android:drawable="@android:color/holo_red_dark" />
<transition android:fromId="@id/normal" android:toId="@id/press">
<animation-list>
<item android:drawable="@android:color/holo_red_dark" android:duration="200" />
<item android:drawable="@android:color/holo_blue_dark" android:duration="200" />
</animation-list>
</transition>
</animated-selector>
三個 item 對應三種狀態並各自標了 id;transition 用 fromId/toId 描述轉場方向,內層 animation-list 決定中間要閃過哪些顏色。順序是有講究的:從 fromId 到 toId 依序播放,反向則倒著播。

Google 終於原生支援 SVG,功臣是 VectorDrawable。它一樣是 Drawable,一樣能用 XML 定義:
<vector xmlns:android="https://schemas.android.com/apk/res/android"
android:width="64dp" android:height="64dp"
android:viewportWidth="600" android:viewportHeight="600">
<group android:name="group"
android:pivotX="300" android:pivotY="300" android:rotation="0">
<path android:name="path"
android:fillColor="#FFFF0000"
android:pathData="M300,70 l70,70 70,0 0,70 -70,70 0,70 -70,0 0,-70 -70,0 0,-70 70,-70 z" />
</group>
</vector>
根節點是 vector,內部由一個 group 與多個 path 組成,一個 group 可以裝多個 path。最關鍵的是 android:pathData,它決定圖形輪廓。使用方式和普通圖片一樣,塞給 ImageView 的 src 即可。

目前只是靜態圖形。要讓它動起來,需要 animated-vector。先準備兩個動畫,一個管旋轉、一個管顏色,注意 propertyName 要對得上前面 group 與 path 的屬性名稱:
<!-- animator/rotation.xml -->
<objectAnimator xmlns:android="https://schemas.android.com/apk/res/android"
android:propertyName="rotation"
android:duration="1000"
android:valueFrom="0" android:valueTo="360"
android:valueType="floatType" />
<!-- animator/color.xml -->
<objectAnimator xmlns:android="https://schemas.android.com/apk/res/android"
android:propertyName="fillColor"
android:duration="1000"
android:valueFrom="#FFFF0000" android:valueTo="#FF0000FF"
android:valueType="colorType" />
接著用 animated-vector 把兩者綁到對應的 name 上:
<animated-vector xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_clock">
<target android:name="group" android:animation="@animator/rotation" />
<target android:name="path" android:animation="@animator/color" />
</animated-vector>
把 ImageView 的 src 指向這個檔案後執行,你會發現什麼都沒發生——因為 animated-vector 不會自動播放,得程式觸發:
final ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable d = iv.getDrawable();
if (d instanceof Animatable) {
((Animatable) d).start();
}
}
});
判斷是否為 Animatable 再呼叫 start(),旋轉與變色就會同時跑起來。

至此,Material Design 的六大動畫主題就走完了。這些 API 大多集中在 5.0 以上,用在低版本時需要留意相容性問題。
Android Universal Image Loader 使用完全指南
一、功能介紹 1.1 Android Universal Image Loader Android Universal Image Loader 是一個強大且可高度自訂的
Android 圖片選取實戰:調起系統相冊並取得合適尺寸的 Bitmap
做問題反饋頁面時,需要讓用戶上傳截圖。最初的念頭是把設備裡所有圖片都掃描出來自己列清單,寫到一半才反應過來——系統相冊本來就有現成的 API 可以調起,何必自己造輪子。
Android View 與 ViewGroup 架構解析:測量、排版與繪製
Android 的視圖控制項大致分為兩類:ViewGroup 與 View。ViewGroup 作為父容器,包含並管理子 View;兩者共同構成一棵「控制項樹」,每個 V
麥芒5與榮耀8怎麼選?規格、售價與定位的完整對照
華為新機的節奏很快,榮耀8由吳亦凡代言登場之後,麥芒5也接著亮相。兩台都是全網通、都是 4GB+64GB 的高階配置,售價只差 100 元——於是「該買哪一台」變成一個需