簡介
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();
總結
以上就是這篇文章的全部內容了,希望本文的內容能對大家開發Android的時候有所協助,如果有疑問大家可以留言交流。