編輯:關於 Android 程式設計
本文由 PurpleSword(jzj1993) 原創,轉載請註明原文網址 https://blog.csdn.net/jzj1993
常見的動畫大致可分為幾類
控制項 View 的動畫(例如 Dialog 視窗中的圓形進度條);
視窗 Window 的動畫(例如 DialogWindow、PopupWindow 的動畫,以及 Activity 切換時整個 Window 頁面的動畫);
ViewGroup 的 LayoutAnimation 動畫,讓每個子控制項按照設定的順序與延遲依次播放動畫。
動畫通常在 anim/*.xml 中定義
在 XML 中定義動畫時,可以直接用 <scale>、<rotate> 這類標籤單獨定義,也可以把它們放進 <set> 標籤裡,此時標籤內定義的動畫會同時開始播放。兩者都能透過 AnimationUtils.loadAnimation 方法來載入。如果是用 set 標籤定義的,載入時回傳的會是一個 AnimationSet 實例(AnimationSet 繼承自 Animation)。
要特別注意的是,在 set 標籤上設定的一些屬性,會直接覆蓋它裡面各動畫的對應屬性;而 AnimationSet 從 Animation 繼承而來的另外一些屬性則會失效。下面是 AnimationSet 類別的官方說明:
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.
The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:
從 android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH 開始,這些屬性在 XML 資源與執行期的行為才保持一致(在此之前,於 XML 中設定的值會對 AnimationSet 失效)。也就是說,對一個 AnimationSet 呼叫 setDuration(500),其效果等同於在 XML 資源中為該 AnimationSet 宣告 android:duration="500"。
常規補間動畫:彈跳(移動)
Layout/activity_welcome_anim.xml(0%p 表示佔父組件尺寸的百分比)
再看幾個例子。
常規補間動畫:縮放、透明度
再如上浮效果(移動、透明度)
這些動畫都能透過程式來載入:
this.setContentView(R.layout.activity_welcome); anim = AnimationUtils.loadAnimation(this, R.anim.welcome_anim); // 動畫播放完畢後,讓 View 物件停留在終止的位置 anim.setFillEnabled(true); anim.setFillAfter(true);
this.findViewById(R.id.point).setAnimation(anim);
此外還可以設定動畫監聽器
anim.setAnimationListener(listener); /** * 動畫監聽器 */ private AnimationListener listener = new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { // startMain(); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } };
在 Dialog 中載入動畫
LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.dialog_voice, null); ImageView img = (ImageView) v.findViewById(R.id.dialog_img); Animation anim = AnimationUtils.loadAnimation(context, R.anim.center_rotate_repeat); img.startAnimation(anim); new AlertDialog.Builder(context).setView(v);
為整個 Dialog 設定動畫
dialog.getWindow().setWindowAnimations(R.style.quick_scale_anim);
為 PopupWindow 設定動畫
pop.setAnimationStyle(R.style.quick_scale_anim);
Activity 的切換動畫
用程式碼實作 Activity 切換動畫
startActivity(new Intent(this, ListAlarm.class)); overridePendingTransition(R.anim.anim_activity_in, R.anim.anim_activity_unchange);
或者:
ActivityGuide.this.finish(); overridePendingTransition(R.anim.alpha_stay, R.anim.alpha_exit);
在 XML 中定義 Activity 切換動畫
提醒:Activity 切換動畫不能設定 SingleInstance 屬性,否則會導致動畫失效。
Manifest.xml 中為 Activity 套用主題:
android:theme="@style/app_theme_fullscreen"
style.xml:
對應的 anim/quick_alpha_enter.xml 與 anim/quick_alpha_stay.xml。
LayoutAnimation
1、先在 layout_anim_item.xml 中定義子 View 動畫;
2、再在 layout_anim.xml 中定義 Layout 動畫,並引用子 View 動畫:
3、最後在 ViewGroup 中引用自訂的 Layout 動畫:
Android 自定義多級樹形列表控件(支持任意層級)
在開發 App 的過程中,我們經常會遇到需要多級列表展示的需求,而 Android 原生 SDK 並沒有提供三級、四級甚至更多層級的列表控件。因此我們需要自己實現一個類似
OpenGL ES 紋理繪製入門(一)
矩陣變換類 package test.com.opengles7_1; import android.opengl.Matrix; /** * Created by h
Android 崩潰異常捕獲與日誌上報實踐:不依賴第三方庫的思路
Android 崩潰異常捕獲與日誌上報實踐 再經驗豐富的開發者,也很難保證程式永遠不會出現異常。當使用者規模變大後,個別崩潰或異常堆疊就會成為排查問題的重要線索。因此,如
Android ListView 多種用法詳解:從 ArrayAdapter 到自訂 MyAdapter
在 Android 開發中,ListView 是我們最常使用的視圖之一。本文系統地介紹 ListView 的多種使用方式,從最簡單的 ArrayAdapter 一直到靈活