Android動畫簡述

  在Android開發中,我們往往會用到動畫去優化一些顯示不流暢,做一些顯示酷炫的UI,如果把android比如成一個人的話,那動畫就是這個人的精神面貌,時時刻刻反映着這個人的狀況,哈哈,可能有些不恰當,但是實際上它確實是Android的一種美好修飾。話不多說,接下來我們就簡單地講解下動畫的類型和使用。
  類型:
  <1>:   幀動畫:圖片按順序一幀一幀地播放
  <2>:   補間動畫:旋轉(rotate),縮放(scale),位移(translate),透明度(alpha)
  <3>:屬性動畫:通過改動相應view的屬性來實現動畫效果

使用:

幀動畫:

<?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.getDrawable();
        animationDrawable1.start();
    }

補間動畫

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
    <translate
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set>
</set>

透明度(alpha)
android:fromAlpha爲動畫開始的透明度【0:完全透明,1:完全不透明】
android:toAlpha爲動畫結束的透明度

縮放(scale)
android:fromXScale、android:fromYScale爲動畫開始X,Y方向上的縮放比例【0:小的沒有,1:原本大小】
android:toXScale、android:toYScale爲動畫開始X,Y方向上的縮放比例
android:pivotX、android:pivotY爲動畫在X,Y方向上的縮放中心點(比如設置爲0,0那麼就是寬和高向左上角縮小)【例如:50%是距左邊界圖片一半的距離】

平移(translate)
android:fromXDelta、android:fromYDelta爲動畫平移的起始點
android:toXDelta、android:toYDelta爲動畫平移的結束點

旋轉(rotate)
android:fromDegrees爲動畫旋轉的起始角度【0:不動,180:轉半圈,360:轉一圈,3600:轉10圈,-180逆時針轉半圈】
android:toDegrees爲動畫旋轉的結束角度
android:pivotX、android:pivotY爲動畫旋轉的中心點【例如:50%是距左邊界圖片一半的距離】

通過xml新建完動畫資源還需要使用AnimationUtils進行裝載。
final Animation anim = AnimationUtils
.loadAnimation(this, R.anim.anim);
上面的R.anim.anim就是上面用xml寫出來的動畫文件。

在我們使用的時候,直接調用View的startAnimation方法就好了。
view.startAnimation(anim);

屬性動畫(ValueAnimator和ObjectAnimator)

ValueAnimator簡單使用
使用流程:
1.調用ValueAnimator的ofInt(),ofFloat()或ofObject()靜態方法創建ValueAnimator實例
2.調用實例的setXxx方法設置動畫持續時間,插值方式,重複次數等
3.調用實例的addUpdateListener添加AnimatorUpdateListener監聽器,在該監聽器中 可以獲得ValueAnimator計算出來的值,你可以值應用到指定對象上~
4.調用實例的start()方法開啓動畫! 另外我們可以看到ofInt和ofFloat都有個這樣的參數:float/int… values代表可以多個值。

舉例:
ValueAnimator vValue = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
vValue.setDuration(1000L);
vValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale = (Float) animation.getAnimatedValue();
view.setScaleX(scale);
view.setScaleY(scale);
}
});
vValue.setInterpolator(new LinearInterpolator());
vValue.start();

ObjectAnimator簡單使用
比起ValueAnimator,ObjectAnimator顯得更爲易用,通過該類我們可以直接 對任意對象的任意屬性進行動畫操作!沒錯,是任意對象,而不單單只是View對象, 不斷地對對象中的某個屬性值進行賦值,然後根據對象屬性值的改變再來決定如何展現 出來!比如爲TextView設置如下動畫: ObjectAnimator.ofFloat(textview, “alpha”, 1f, 0f);
這裏就是不斷改變alpha的值,從1f - 0f,然後對象根據屬性值的變化來刷新界面顯示,從而 展現出淡入淡出的效果,而在TextView類中並沒有alpha這個屬性,ObjectAnimator內部機制是: 尋找傳輸的屬性名對應的get和set方法~,而非找這個屬性值! 不信的話你可以到TextView的源碼裏找找是否有alpha這個屬性。

舉例:
animator1 = ObjectAnimator.ofFloat(view, “alpha”, 1f, 0f, 1f, 0f, 1f);
animator1.setDuration(1000l);
animator1.start();
animator2 = ObjectAnimator.ofFloat(view, “rotation”, 0f, 360f, 0f);
animator2.setDuration(2000l);
animator2.start();
animator3 = ObjectAnimator.ofFloat(view, “scaleX”, 2f, 4f, 1f, 0.5f, 1f);
animator3.setDuration(3000l);
animator3.start();
animator4 = ObjectAnimator.ofFloat(view, “translationY”, height / 8, -100, height / 2);
animator4.setDuration(4000l);
animator4.start();

以上爲這三種動畫實現的簡述,寫的比較簡單,供大家參考下,其中比較複雜點的就是屬性動畫,這裏大概說了下,如果需要知道其使用詳細的方法,可以去google官網上查看相應的api使用,好了,有什麼好的建議讀者可以提出來,我將虛心接受大家的建議。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章