分類
Android動畫主要包括視圖動畫和屬性動畫。視圖動畫包括Tween動畫和Frame動畫姑廉。Tween動畫又包括漸變動畫缺亮、平移動畫、縮放動畫桥言、旋轉(zhuǎn)動畫萌踱。
Tween動畫的基本屬性
- 目標(biāo) View;
- 時常 duration;
- 開始狀態(tài) fromXXX;
- 結(jié)束動畫 toXXX;
- 開始時間 startOffset;
- 重復(fù)次數(shù) repeatCount;
- 時間軸 interpolator(插值器)号阿。
代碼示例
xml實現(xiàn)
<?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>
在代碼中調(diào)用
Animation translate = AnimationUtils.loadAnimation(context,R.anim.translate);
imageView.startAnimation(translate);
補(bǔ)充:
1.對于縮放和旋轉(zhuǎn)動畫并鸵,有一個pivotX或者pivotY,表示的是縮放或旋轉(zhuǎn)的中心點。對應(yīng)的屬性值
有三種寫法扔涧。
- 數(shù)值 50 表示當(dāng)前控件的左上角加上50px;
- 百分?jǐn)?shù) 50% 表示當(dāng)前控件的50%园担;
- 百分?jǐn)?shù)p 50%p 表示父控件的50%。
2.在一個動畫集合里扰柠,可以通過設(shè)置stratOffset屬性粉铐,來實現(xiàn)多個動畫并行和串行的效果。
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>
// 需要先設(shè)置成背景
imageView.setBackgroundResource(R.drawable.frame_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
frameAnimation.start();