編輯:關於 Android 開發
Android 提供兩套機制用來製作簡單動畫:補間動畫(tweened animation)與逐幀動畫(frame-by-frame animation)。本文聚焦在補間動畫的四種基本形式:
Alpha(淡入淡出)、Scale(縮放)、Rotate(旋轉)、Translate(平移)。
實作上有兩條路:直接在 Java 程式碼中建立動畫物件,或把動畫定義寫成 XML 資源檔。前者直觀易讀,後者則利於維護與重複使用。
基本流程只有五步:
1. 建立一個 AnimationSet 物件。
2. 建立對應效果的 Animation 物件,例如 AlphaAnimation。
3. 設定 duration 等屬性。
4. 把 Animation 加入 AnimationSet。
5. 對目標 View 呼叫 startAnimation()。
private class alphaButtonListener implements OnClickListener{ @Override public void onClick(View v) { //建立一個AnimationSet對象 AnimationSet animationSet = new AnimationSet(true); //創建一個AlphaAnimation對象,設置透明度為由1到0 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //設置整個動畫持續的時間 alphaAnimation.setDuration(10000); //將Animation對象添加到animationSet對象中 animationSet.addAnimation(alphaAnimation); //對圖片開始執行AnimationSet imagView.startAnimation(animationSet); } }
setDuration(long):設定動畫持續時間,單位毫秒。
setFillAfter:設為 true 時,動畫結束後仍套用最終轉換狀態,也就是停在結束的位置。
setFillBefore:設為 true 時,在動畫開始前套用起始轉換(系統預設為 true)。
setStartOffset:延遲多久才開始播放。
setRepeatMode:設定重複播放的模式,搭配 repeatCount 使用。
package com.mecury.animationstest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imagView; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imagView = (ImageView) findViewById(R.id.imageView); alphaButton = (Button) findViewById(R.id.alphaButton); alphaButton.setOnClickListener(new alphaButtonListener()); scaleButton = (Button) findViewById(R.id.scaleButton); scaleButton.setOnClickListener(new scaleButtonListener()); rotateButton = (Button) findViewById(R.id.rotateButton); rotateButton.setOnClickListener(new rotateButtonListener()); translateButton = (Button) findViewById(R.id.translateButton); translateButton.setOnClickListener(new translateButtonListener()); } /* * 淡入淡出效果 */ private class alphaButtonListener implements OnClickListener{ @Override public void onClick(View v) { //建立一個AnimationSet對象 AnimationSet animationSet = new AnimationSet(true); //創建一個AlphaAnimation對象,設置透明度為由1到0 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //設置整個動畫持續的時間 alphaAnimation.setDuration(10000); //將Animation對象添加到animationSet對象中 animationSet.addAnimation(alphaAnimation); //對圖片開始執行AnimationSet imagView.startAnimation(animationSet); } } /* * 縮放效果 */ private class scaleButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /*這裡用的是百分比表示法,f表示浮點型 * 四個參數代表縮放的比例,前兩個代表了x軸的縮放(這裡用的是百分比表示法:1f代表原來的寬度,0.5f代表原來寬度的一半)後兩個代表了y軸的縮放 * Animation中有兩個常用的屬性,RELATIVE_TO_SELF(依賴自身),RELATIVE_TO_PARENT(依賴父控件) * 後面四個表明縮放的軸為依賴自身的在x為0.5f,y為0.5f的地方 */ ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(2000); imagView.startAnimation(animationSet); } } /* * 旋轉效果 */ private class rotateButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /*第一個參數,表示圖片相對於起始位置的角度, *第二個參數:表示動畫結束時圖片的偏轉角度。這裡就是由0變到360即原點 *後面的四個參數是為了設置圖片旋轉圍繞的軸,意思是軸的橫坐標是依賴父控件的0f處,縱坐標是依賴父控件的-1f處 *這裡要著中說的是:關於坐標的問題。這這個動畫中,坐標原點是圖片的左上角,原點左x軸和下y軸代表正軸 *此時的圍繞的點的坐標軸就在圖片的正上方 */ RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, -1f); rotateAnimation.setDuration(2000); animationSet.addAnimation(rotateAnimation); imagView.startAnimation(animationSet); } } /* * 移動效果 */ private class translateButtonListener implements OnClickListener{ @Override public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); /* * 前兩個參數決定了動畫開始時圖片的x軸起始位置 * 第三四個參數表示圖片將要到達位置的x軸 * 後面四個是表示y軸的和上面類似 * 代碼中表示:由起始位置位移到右上角 */ TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_PARENT, -1f); translateAnimation.setDuration(2000); animationSet.addAnimation(translateAnimation); imagView.startAnimation(animationSet); } } }
版面配置用 RelativeLayout,四顆按鈕由下往上依序堆疊,中央擺一個 ImageView 當作動畫主角:
<RelativeLayout 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" tools:context="com.mecury.animationstest.MainActivity" > <Button android:id="@+id/translateButton" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:text="Translate平移"/> <Button android:id="@+id/rotateButton" android:layout_width="match_parent" android:layout_height="40dp" android:layout_above="@id/translateButton" android:text="Rotate旋轉"/> <Button android:id="@+id/scaleButton" android:layout_width="match_parent" android:layout_height="40dp" android:layout_above="@id/rotateButton" android:text="Scale縮放"/> <Button android:id="@+id/alphaButton" android:layout_width="match_parent" android:layout_height="40dp" android:layout_above="@id/scaleButton" android:text="Alpha淡入淡出"/> <ImageView android:id="@+id/imageView" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerInParent="true" android:src="@drawable/china"/> </RelativeLayout>
步驟如下:
1. 在 res 目錄下新增名為 anim 的資料夾。
2. 在其中建立 XML 檔,例如 alpha.xml。
3. 於檔案中設定各項屬性。
4. 用 AnimationUtils.loadAnimation() 載入。
<?xml version="1.0" encoding="utf-8"?> <!-- aplha是一個淡入淡出的效果 startOffser時間偏移量,在動畫開始之前,停頓500毫秒 --> <set xmlns:android="https://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1" android:toAlpha="0" android:startOffset="500" android:duration="2000"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android"> <!-- scale是實現縮放 privat 是設置縮放圍繞的軸的縮放 privatX="50"使用的是絕對位置 ="%50"相對控件本身定位,在控件的1/2處 ="50%p" 相對於父控件的位置定位--> <scale android:fromXScale="1" android:toXScale="0.1" android:fromYScale="1" android:toYScale="0.1" android:pivotX="50%" android:pivotY="50%" android:startOffset="500" android:duration="2000"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="100%p" android:pivotY="0%p" android:startOffset="500" android:duration="2000"/> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0%" android:toXDelta="100%p" android:fromYDelta="0%" android:toYDelta="-100%p" android:interpolator="@android:anim/decelerate_interpolator" android:startOffset="500" android:duration="30000" /> </set>
Accelerate_Decelerate_Interpolator:頭尾較慢,中段加速。
Accelerate_Interpolator:起步慢,之後逐漸加速。
Cycle_Interpolator:速度沿正弦曲線循環變化。
Decelerate_Interpolator:起步較快,隨後減速。
Linear_Interpolator:全程等速。
使用方式:android:interpolator="@android:anim/....."
package com.example.animation_01; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imagView; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imagView = (ImageView) findViewById(R.id.imageView); alphaButton = (Button) findViewById(R.id.alphaButton); alphaButton.setOnClickListener(new alphaButtonListener()); scaleButton = (Button) findViewById(R.id.scaleButton); scaleButton.setOnClickListener(new scaleButtonListener()); rotateButton = (Button) findViewById(R.id.rotateButton); rotateButton.setOnClickListener(new rotateButtonListener()); translateButton = (Button) findViewById(R.id.translateButton); translateButton.setOnClickListener(new translateButtonListener()); } class alphaButtonListener implements OnClickListener{ @Override public void onClick(View v) { //使用AnimaionUtils來裝載動畫設置文件 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imagView.startAnimation(animation); } } class scaleButtonListener implements OnClickListener{ @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); imagView.startAnimation(animation); } } class rotateButtonListener implements OnClickListener{ @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imagView.startAnimation(animation); } } class translateButtonListener implements OnClickListener{ @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); imagView.startAnimation(animation); } } }
版面配置與第一個範例完全相同,差別只在動畫來源改為資源檔。相較於把動畫參數寫死在 Java 裡,XML 方式讓設計與邏輯分離,同一個動畫也能在不同頁面重複使用,維護成本明顯更低。
Android NDK 環境搭建入門:從 ADT 設定到第一個 JNI HelloWorld
Android NDK 環境搭建入門:從 ADT 設定到第一個 JNI HelloWorld Android NDK 讓開發者可以使用 C 或 C++ 撰寫效能關鍵或平台
騰訊 Bugly 乾貨:RxJava 結合 MVP 的實戰分層與線程調度
本文整理自騰訊 Bugly 開發者社群的技術分享,主講者為騰訊 IEG Android 開發工程師戴俊,主題是《基於 RxJava 的一種 MVP 實現》。 RxJava
Android 9-patch 圖片入門
9-patch 圖片 9-patch 是 Android 常用的可拉伸圖片格式,副檔名為 .9.png。它能依元件大小自動調整中間可拉伸區域,適合按鈕、對話框與列表背景。
自定義 TabHost 與 TabWidget 樣式的完整實作
打造自定義樣式的 TabHost 底部菜單 本文以京東商城、新浪微博的底部菜單欄為參考,示範如何自定義 TabHost 與 TabWidget 的外觀,做出屬於自己的底部