1.幀動畫:将圖檔一張張的播放,進而達到一種動畫效果。它容易産生OOM,使用中應該避免使用過多的尺寸較大的圖檔。
實作步驟:
1.在res/drawable/目錄下定義一個XML動畫檔案;
2.在Java代碼中調用start()方法開啟動畫。
京東動畫實作
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/a_0"
android:duration="100" />
<item
android:drawable="@drawable/a_1"
android:duration="100" />
<item
android:drawable="@drawable/a_2"
android:duration="100" />
</animation-list>
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animation);
ImageView animationImg1 = (ImageView) findViewById(R.id.animation1);
animationImg1.setImageResource(R.drawable.frame_anim1);
AnimationDrawable animationDrawable1 = (AnimationDrawable) animationImg1.getBackground;
animationDrawable1.start();
} 2.補間動畫/View動畫:可以對View實作透明度(對應AlphaAnimation類)、旋轉(RotateAnimation)、
3.屬性動畫:API11新加入的特性,操作對象不限于View,而是對任意對象。它對應着三個類:ObjectAnimator(繼承自
ValueAnimator
)、ValueAnimator、AnimatorSet(動畫集)。
可以在res/animator/下面定義一個屬性動畫的XML檔案,但是有些對象的屬性起始值無法預先知道,是以推薦在代碼中動态建立。
eg:
// 使用屬性動畫改變按鈕的背景色,在3秒内實作顔色從藍色漸變為綠色,動畫會無限播放并且會有反轉效果
ObjectAnimator colorAnim=ObjectAnimator.ofInt(btn,"backgroundColor", Color.BLUE,Color.GREEN);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();