分類
Android動畫主要包括視圖動畫和屬性動畫。視圖動畫包括Tween動畫和Frame動畫。Tween動畫又包括漸變動畫、平移動畫、縮放動畫、旋轉動畫。
Tween動畫的基本屬性
- 目標 View;
- 時常 duration;
- 開始狀態 fromXXX;
- 結束動畫 toXXX;
- 開始時間 startOffset;
- 重復次數 repeatCount;
- 時間軸 interpolator(插值器)。
代碼示例
xml實現
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="0"
android:fillAfter="true"
android:duration="3000">
</translate>
在代碼中調用
Animation translate = AnimationUtils.loadAnimation(context,R.anim.translate);
imageView.startAnimation(translate);
補充:
1.對于縮放和旋轉動畫,有一個pivotX或者pivotY,表示的是縮放或旋轉的中心點。對應的屬性值
有三種寫法。
- 數值 50 表示當前控件的左上角加上50px;
- 百分數 50% 表示當前控件的50%;
- 百分數p 50%p 表示父控件的50%。
2.在一個動畫集合里,可以通過設置stratOffset屬性,來實現多個動畫并行和串行的效果。
Frame動畫
Frame動畫的配置文件放在drawable目錄下
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image1" android:duration="50"/>
<item android:drawable="@drawable/image2" android:duration="50"/>
<item android:drawable="@drawable/image3" android:duration="50"/>
</animation-list>
// 需要先設置成背景
imageView.setBackgroundResource(R.drawable.frame_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
frameAnimation.start();