編輯:關於 Android 程式設計
Android 的動畫體系主要分兩大類:Tween 動畫(補間動畫)與 Frame 動畫(逐幀動畫)。本文記錄 Tween 動畫的四個核心類、公用方法與完整示例。
Tween 的意思是「補間」——你只需描述「從什麼狀態變成什麼狀態」,中間的每一幀由系統插值計算。它能實現縮放、旋轉、平移、透明度變化這四類效果。
AlphaAnimation(float fromAlpha, float toAlpha)
fromAlpha:動畫開始時的透明度,取值 0.0 ~ 1.0toAlpha:動畫結束時的透明度
ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue)
fromX / toX:X 軸方向起始 / 結束的伸縮比例(相對於原始尺寸)fromY / toY:Y 軸方向起始 / 結束的伸縮比例pivotXType:X 軸伸縮模式,可取 Animation.ABSOLUTE(絕對值)、Animation.RELATIVE_TO_SELF(相對於自身)、Animation.RELATIVE_TO_PARENT(相對於父容器)pivotXValue:X 軸縮放中心位置pivotYType / pivotYValue:Y 軸的伸縮模式與中心位置縮放效果好不好看,九成取決於中心點選得對不對。做「從按鈕中心放大」的效果,就用 RELATIVE_TO_SELF, 0.5f。
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
fromXDelta / toXDelta:X 軸起始 / 結束位置fromYDelta / toYDelta:Y 軸起始 / 結束位置這些值同樣可以是絕對像素,也可以用 Animation.RELATIVE_TO_SELF 這類常量修飾成比例值(0.0f ~ 1.0f)。
RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue)
fromDegrees:起始角度toDegrees:結束角度ScaleAnimation 完全一致animation.setDuration(2000); // 設置播放時長(毫秒) animation.setRepeatCount(2); // 設置重複次數(注意:是「額外重複」的次數) animation.setRepeatMode(AlphaAnimation.REVERSE); // 重複模式 iv.startAnimation(animation); // 啟動動畫
重複模式有兩種:Animation.RESTART(每次都從頭開始)與 Animation.REVERSE(往返播放,第二遍倒著放)。做呼吸燈效果時 REVERSE 特別好用。
佈局文件:五個按鈕分別觸發五種效果,下方一個 ImageView 作為動畫對象。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ftf.tween.MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:onClick="click"
android:text="透明度"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="click2"
android:text="縮放"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="click3"
android:text="旋轉"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="click4"
android:text="平移"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="click5"
android:text="組合"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<ImageView
android:id="@+id/iv"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Activity 代碼:
// 透明度變化
public void click(View view) {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
// 縮放
public void click2(View view) {
ScaleAnimation animation = new ScaleAnimation(
0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
// 旋轉
public void click3(View view) {
RotateAnimation animation = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
// 平移
public void click4(View view) {
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, 1.0f);
animation.setDuration(2000);
animation.setRepeatCount(2);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
// 組合動畫
public void click5(View view) {
AnimationSet set = new AnimationSet(false);
TranslateAnimation pa = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, 1.0f);
pa.setDuration(2000);
pa.setRepeatCount(2);
pa.setRepeatMode(AlphaAnimation.REVERSE);
RotateAnimation ra = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(2000);
ra.setRepeatCount(2);
ra.setRepeatMode(AlphaAnimation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(
0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(2000);
sa.setRepeatCount(2);
sa.setRepeatMode(AlphaAnimation.REVERSE);
set.addAnimation(sa);
set.addAnimation(ra);
set.addAnimation(pa);
iv.startAnimation(set);
}
AnimationSet(false) 裡的布爾參數是 shareInterpolator。傳 false 表示每個子動畫保留自己的插值器;傳 true 則所有子動畫共用集合的插值器,效果會完全不同。LayoutAnimation 或改用屬性動畫(ObjectAnimator)。setFillAfter(true),但這只是視覺欺騙,真正要改狀態還是得用屬性動畫。res/anim/ 下寫好動畫描述文件,用 AnimationUtils.loadAnimation(context, R.anim.xxx) 加載,能讓動畫參數和業務代碼分離,便於設計走查。最後說一下選型:Tween 動畫適合「一次性的視覺效果」,比如進場動畫、提示閃爍。如果需要讓一個 View 真正從 (0,0) 移動到 (100,100) 並能被點擊,或者需要複雜的連鎖與關鍵幀控制,應該用 Android 3.0 之後引入的屬性動畫(ObjectAnimator / ValueAnimator),它直接操作對象屬性,沒有上述那些「視覺與實際不一致」的問題。
Android Studio 集成 ShareSDK 實現一鍵分享(完整配置步驟)
社交分享幾乎是每個 App 的標配功能。ShareSDK 提供了封裝好的「一鍵分享」能力,配合官方的快速集成工具,接入過程並不複雜。下面記錄一次完整的集成過程。 先新建
Android 進程間通訊 Binder 入門:四大元件與閱讀原始碼的路線圖
一個 Android 應用通常由若干 Activity 與 Service 組成,它們可能同處一個進程,也可能被分配到不同進程。跨進程的那部分該怎麼溝通?答案就是 Bin
自定義 View 實戰(二):用複合控件封裝一個可復用的 TopBar
複合控件:把「一組控件」變成「一個控件」 自定義 View 的第二條路徑是創建複合控件。它的核心思路並不複雜:挑一個合適的 ViewGroup 作為父容器,把若干功能明確
Android 入門:ListView 與 SimpleAdapter 的基本用法(上)
ListView 是 Android 裡最常被用到的控件之一,而它裡面的每一個子項(Item)既可以只是一串文字,也可以是一個由多個控件組成的複合版面。本文先從最基礎的