Android開發之Animation Resources(動畫資源)

首先附上谷歌官方文檔鏈接地址

    在Android中,一個動畫資源可由兩種形式的動畫之一確定:屬性動畫(Property Animation)和視圖動畫(View Animation);
視圖動畫又包含了兩種形式的動畫:幀動畫(Frame animation)和補間動畫(Tween animation).
    In this document
        Property Animation
        View Animation
            Tween animation
            Frame animation

一、幀動畫(Frame animation)

先來看看谷歌的官方定義:An animation defined in XML that shows a sequence of images in order (like a film).(在XML文件中定義的用于按次序播放一系列圖片(類似于電影)的動畫)。顯然這是一個傳統動畫,它創建一個個不同的圖像序列并有序的播放,類似于早期動畫片的原理。
??幀動畫可以定義在XML中并在代碼中實現,如果我們要在XML中定義幀動畫,需在res/drawable/filename.xml文件下,文件名會被用作資源ID;如果完全由代碼實現的話,就要用到AnimationDrawable對象。

resource reference:
    In Java: R.drawable.filename
    In XML: @[package:]drawable.filename

將動畫定義在XML中官方給出的語法規范示例:

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot=["true" | "false"] >
        <item
            android:drawable="@[package:]drawable/drawable_resource_name"
            android:duration="integer" />
    </animation-list>

元素及必要屬性:

    <animation-list>元素:
        必要元素,包含一個或多個<item>元素。
        包含屬性:
        android:oneshot
            boolean值,“true”表示播放動畫一次,“false”表示循環播放動畫。
    <item>元素:
        一幀動畫,必須是<animation-list>元素的子元素。
        包含屬性:
        android:drawable
            圖片資源,表示這一幀代表的圖片
        android:duration
            整形值,表示展示這一幀的的時間,以毫秒為單位。

然后官方給出了示例代碼:

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
        <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
        <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
        <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
    </animation-list>

上面幀動畫中共有三張圖片,每張停留200毫秒并且循環播放。我們在代碼中會將上述動畫設置為背景視圖,現在我們在代碼中播放上述動畫:

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

    rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
    rocketAnimation.start();

OK,關于幀動畫的部分基本介紹完了,相對比較簡單,上面基本是翻譯的官方文檔。
??另外需要注意的一點是:如果我們將rocketAnimation.start();放在Activity的onCreate()函數中,在低版本的安卓設備上(4X以下)可能會出現問題(如動畫會停留在第一幀等)因為AnimationDrawable的start方法時,窗口Window對象還沒有完全初始化,AnimationDrawable不能完全追加到窗口Window對象中,官網給出的建議是在Activity的onWindowFocusChanged方法(如果對此方法不熟可以看此博客http://blog.csdn.net/dmk877/article/details/45059261)中去掉用AnimationDrawable.start()方法,因為onWindowFocusChanged在Activity窗口獲得或失去焦點時調用,例如創建時首次展示在用戶面前,所以在調用onWindowFocusChanged方法時窗口已經初始化完全了。當然如果不適配那么低的版本的設備的話我們可以不必考慮這個問題。(這段參考自博客http://blog.csdn.net/dmk877/article/details/45893017)。

二、補間動畫(Tween animation)

我們先來看看官方對于補間動畫的定義:

    Creates an animation by performing a series of transformations on a single image with an Animation(通過對單張圖片執行一系列的轉變來創造一個動畫)
    An animation defined in XML that performs transitions such as rotating, fading, moving, and stretching on a graphic(一個定義在XML文件中,通過執行諸如旋轉,淡變(透明度),移動和圖形拉伸創造的動畫)

    同樣文件位置在**res/anim/filename.xml**中,
    resource reference:
        In Java: R.anim.filename
        In XML: @[package:]anim/filename

語法規范:

<?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(漸變)、scale(縮放)、translate(位移)、rotate(旋轉),這四種動畫既有他獨有的屬性同時又有animation類的通用屬性。
    The file must have a single root element: either an <alpha>, <scale>, <translate>, <rotate>, 
or <set> element that holds a group (or groups) of other animation elements (even nested <set> elements).
此文件必須含有alpha,scale,translate或者rotate中的一個根元素,或者含有一個包含了一組或者更多組的其他動畫元素(甚至是嵌套的<set>元素)。

從Animation類繼承的共有屬性 :

android:duration:動畫執行的時間,以毫秒為單位
android:fillEnabled:true|false,true:動畫結束時還原到開始動畫前的狀態
android:fillBefore:如果fillEnabled的值為true,它的值才有意義,否則沒有意義默認值是true,視圖會停留在動畫開始的狀態
android:fillAfter:設置的是在這個動畫結束后是否保留這個動畫的最后一幀的效果填充后面的動畫,它的設置不受fillEnabled的影響
android:repeatMode:reverse|restart,重復類型,reverse:表示倒序回訪,restart:表示重新放一遍,這個屬性必須與repeatCount聯合使用,因為它的前提是重復,即重復播放時的播放類型。
android:repeatCount:動畫重復的次數(注意是重復的次數),可以是你想循環播放的次數,也是可以是infinite:表示無限循環
android:interpolator:設定的插值器,它主要用來為動畫設置一些特殊的效果,比方說:加速運動、減速運動、動畫結束的時候彈 起等等
(1)roate動畫,旋轉動畫

上面我們交代過四種動畫有共有屬性和私有屬性,現在我們先講講私有屬性。
這里可以參考roateanimation類的說明文檔(https://developer.android.com/reference/android/view/animation/RotateAnimation.html)
屬性:

android:fromDegrees
    Float. Starting angular position, in degrees.
    動畫開始時的旋轉的角度位置,浮點類型,正值代表順時針方向的度數,負值代表逆時針。
android:toDegrees
    Float. Starting angular position, in degrees.
    動畫結束時旋轉到的角度位置,float類型,正值代表順時針方向度數,負值代碼逆時針方向度數
android:pivotX
    Float or percentage. The X coordinate of the center of rotation. Expressed either: in pixels relative to the object's left edge (such as "5"), in percentage relative to the object's left edge (such as "5%"), or in percentage relative to the parent container's left edge (such as "5%p").
    表示的事旋轉點的X軸坐標,可以是數值、百分數、百分數p三種樣式,比如50、50%、50%p。如果為數值,表示當前View的左上角(Android中View的原點)加上50px作為旋轉點X坐標;如果是50%,表示當前View的左上角加上自己寬度的50%作為旋轉點的X坐標;如果是50%p,則表示在當前控件的左上角加上父控件寬度的50%作為旋轉點X坐標。
android:pivotY
    和pivotX 同理,這里不再獒述。
    默認的旋轉起始點是View左上角的視圖原點,需要注意的是這里的開始旋轉角度fromDegrees和結束時旋轉角度都是相對于其實位置而言的。

可以參考這篇博客:http://blog.csdn.net/dmk877/article/details/51912104,講的很好很全面。

(2)Alpha動畫(漸變透明度動畫)

一種淡入或者淡出的動畫,可以參考AlphaAnimation類說明文檔https://developer.android.com/reference/android/view/animation/AlphaAnimation.html
屬性說明:

android:fromAlpha
    Float. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque.
    浮點值,表示動畫開始時的透明度,變化范圍從0.0(透明)到1.0(不透明)。
android:toAlpha
    Float. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.
    浮點值,表示動畫結束時的透明度,取值同上。
(3)translate(位移動畫)

A vertical and/or horizontal motion. Supports the following attributes in any of the following three formats: values from -100 to 100 ending with "%", indicating a percentage relative to itself; values from -100 to 100 ending in "%p", indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value. Represents a TranslateAnimation.https://developer.android.com/reference/android/view/animation/TranslateAnimation.html
??從官方文檔的描述中,我們可以知道:
位移動畫是一種水平或者豎直的運動,他有三種不同的單位表示:第一種是以-100%-100%之間的值來結尾,表示移動的距離相對自身;第二種是以-100%p-100%p之間的值,表示移動的距離是相對于父控件;一種是沒有任何后綴的浮點值,表示移動的絕對距離.這和上面roate中的情況是一樣的.

android:fromXDelta
    Float or percentage. Starting X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
    起始點的X軸坐標,可以是數值、百分數、百分數p三種形式,具體的已經在rotate里邊講過了,就是相對于View視圖左上角(原點)的偏移量。
android:toXDelta
    Float or percentage. Ending X offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element width (such as "5%"), or in percentage relative to the parent width (such as "5%p").
    結束點的X坐標。
android:fromYDelta
    Float or percentage. Starting Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").
    起始點的Y坐標。
android:toYDelta
    Float or percentage. Ending Y offset. Expressed either: in pixels relative to the normal position (such as "5"), in percentage relative to the element height (such as "5%"), or in percentage relative to the parent height (such as "5%p").
    結束點的Y坐標。
(4)scale縮放動畫

A resizing animation. You can specify the center point of the image from which it grows outward (or inward) by specifying pivotX and pivotY. For example, if these values are 0, 0 (top-left corner), all growth will be down and to the right. Represents a ScaleAnimation.
??一種伸縮動畫,你可以精確指定圖片縮放的中心點。

android:fromXScale
    Float. Starting X size offset, where 1.0 is no change.
    浮點值,起始的X軸方向上相對自身的縮放比例。(1.0表示沒有縮放,0.5表示縮小一倍,2表示放大一倍)
android:toXScale
    Float. Ending X size offset, where 1.0 is no change.
    結尾的X軸方向上相對于自身的縮放比例。
android:fromYScale
    Float. Starting Y size offset, where 1.0 is no change.
    起始方向上Y軸方向上相對于自身的縮放比例。
android:toYScale
    Float. Ending Y size offset, where 1.0 is no change.
    結尾方向上Y軸相對自身的縮放比例。
android:pivotX
    Float. The X coordinate to remain fixed when the object is scaled.
    縮放起點X軸坐標,可以是數值、百分數、百分數p,具體意義和 roate中的一致。
android:pivotY
    Float. The Y coordinate to remain fixed when the object is scaled.
    縮放起點Y軸坐標,取值及意義同pivotX
(5)set標簽的使用

set標簽是一系列動畫的組合,也就是說我們可以通過set讓一個View同時執行多張動畫,set標簽本身是沒有屬性的,但是他可以使用Animation類的屬性。當 android:shareInterpolator = true時,set標簽的屬性會作用于所有的子元素。(具體的Animation屬性我們后面會做進一步詳解)。

ELEMENTS:
    <set>
        A container that holds other animation elements (<alpha>, <scale>, <translate>, <rotate>) or other <set> elements. Represents an AnimationSet.
        attributes:
        android:interpolator 該屬性為一個指示器,指向一個動畫資源文件,該值必須是一個資源文件的引用(就像示例代碼中那樣指向一個包名)而不是一個類名。
            Interpolator resource. An Interpolator to apply on the animation. The value must be a reference to a resource that specifies an interpolator (not an interpolator class name). There are default interpolator resources available from the platform or you can create your own interpolator resource. See the discussion below for more about Interpolators.
        android:shareInterpolator 該屬上面已經交代過了。
            Boolean. "true" if you want to share the same interpolator among all child elements.

我們來看看一個組合動畫的例子

    <?xml version="1.0" encoding= "utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true">

        <alpha
            android:fromAlpha= "0.0"
            android:toAlpha= "1.0"
            android:duration= "3000" />

        <scale
            android:fromXScale= "0.0"
            android:toXScale= "1.0"
            android:fromYScale= "0.0"
            android:toYScale= "1.0"
            android:pivotX= "50%"
            android:pivotY= "50%"
            android:duration= "3000" />

        <rotate
            android:fromDegrees= "0"
            android:toDegrees= "720"
            android:pivotX= "50%"
            android:pivotY= "50%"
            android:duration= "3000"/>

         <translate
            android:startOffset= "3000"
            android:fromXDelta= "0"
            android:fromYDelta= "0"
            android:toXDelta= "85"
            android:toYDelta= "0"
            android:duration= "1000" />

        <alpha
            android:startOffset= "4000"
            android:fromAlpha= "1.0"
            android:toAlpha= "0.0"
            android:duration= "1000" />

    </set>
    在這個set標簽中,我們包含了上面四種不同的動畫,當我們對一張圖start()這個動畫后會同時執行前三種動畫,持續3S,之后執行平移動畫(startOffset= "3000",duration= "1000"),最后執行最后一個漸變動畫(startOffset= "4000").
(6)interpolator插值器

An interpolator is an animation modifier defined in XML that affects the rate of change in an animation. This allows your existing animation effects to be accelerated, decelerated, repeated, bounced, etc.
??An interpolator is applied to an animation element with the android:interpolator attribute, the value of which is a reference to an interpolator resource.
??All interpolators available in Android are subclasses of the Interpolator class. For each interpolator class, Android includes a public resource you can reference in order to apply the interpolator to an animation using the android:interpolator attribute. The following table specifies the resource to use for each interpolator:
??上面官方對于interpolator的解釋為:一種定義在XML文件中影響動畫變化速率的調節器,它允許你對目前的動畫做出加速、減速、重復、彈跳等效果。
??一個插值器通過android:interpolator屬性來作用于一個動畫,該屬性作用的值是一個interpolator資源文件,如上面示例中提到的android:interpolator="@[package:]anim/interpolator_resource"
??在Android中所有可用的interpolator都是Interpolator類的子類.(https://developer.android.com/reference/android/view/animation/Interpolator.html)對于interpolator類,Android包含一個你可以通過 android:interpolator 元素調用的公共資源文件,下面的這張表指定了指向各個interpolator的資源。

Interpolator對象                      資源ID                                        功能作用
AccelerateDecelerateInterpolator    @android:anim/accelerate_decelerate_interpolator     先加速再減速
AccelerateInterpolator            @android:anim/accelerate_interpolator                 加速
AnticipateInterpolator            @android:anim/anticipate_interpolator                 先回退一小步然后加速前進
AnticipateOvershootInterpolator      @android:anim/anticipate_overshoot_interpolator      先回退一小步然后加速前進,超出終點一小步后再回到終點
BounceInterpolator                @android:anim/bounce_interpolator                     最后階段彈球效果
CycleInterpolator                  @android:anim/cycle_interpolator                      周期運動
DecelerateInterpolator            @android:anim/decelerate_interpolator                 減速
LinearInterpolator                @android:anim/linear_interpolator                     勻速
OvershootInterpolator              @android:anim/overshoot_interpolator                  快速到達終點并超出一小步最后回到終點

我們可以創建一個插值器資源修改插值器的屬性來自定義個性化插值器,比如修改AnticipateInterpolator的加速速率,調整CycleInterpolator的循環次數等。為了完成這種需求,我們需要創建XML資源文件,然后將其放于/res/anim下,然后再動畫元素中引用即可。我們先來看一下幾種常見的插值器可調整的屬性:

<accelerateDecelerateInterpolator>      無
<accelerateInterpolator>                android:factor 浮點值,加速速率,默認為1
<anticipateInterploator>                android:tension 浮點值,起始點后退的張力、拉力數,默認為2
<anticipateOvershootInterpolator>      android:tension 同上; android:extraTension 浮點值,拉力的倍數,默認為1.5(2  * 1.5)
<bounceInterpolator>                    無
<cycleInterplolator>                    android:cycles 整數值,循環的個數,默認為1
<decelerateInterpolator>                android:factor 浮點值,減速的速率,默認為1
<linearInterpolator>                    無
<overshootInterpolator>                  浮點值,超出終點后的張力、拉力,默認為2

關于這幾種值得演示可以再次參考一下這篇博客http://blog.csdn.net/dmk877/article/details/52011155
舉個例子:
??我們在XML中定義一個Interpolator文件—— res/anim/my_overshoot_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:tension="7.0"
    />

動畫的XML文件中可以這么使用:

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@anim/my_overshoot_interpolator"  //一句代碼的事
        android:fromXScale="1.0"
        android:toXScale="3.0"
        android:fromYScale="1.0"
        android:toYScale="3.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="700" />
     這里我們修改了超出終點后張力、拉力的值為7.0,運行后會發現明顯回彈的效果會更加急促。

以上就是補間動畫在XML文件中的定義與使用,關于補間動畫在JAVA代碼中的使用可以參考這篇文章:http://blog.csdn.net/dmk877/article/details/51980734

三、屬性動畫(Property Animation)

https://developer.android.com/guide/topics/graphics/prop-animation.html#value-animator
1.屬性動畫與視圖動畫的區別
??前面我們說過Android動畫分為視圖動畫(View Animation)屬性動畫(Property Animation),現在我們來談談屬性動畫這個大類。首先我們分清楚屬性動畫和視圖動畫的區別,在谷歌的官方說明中的解釋如下:
??The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
??View動畫只提供了容納View對象的容器,如果你想作用于非View對象時,你必須自己通過代碼來實現。View動畫另一個不足是他只暴露了幾個方面的動效給View對象,諸如平移旋轉等,而不是View的背景顏色等等。
??官方翻譯比較繞,這里在翻譯一下,就是說補間動畫只能對一個View對象進行四種操作(平移、旋轉、透明度、縮放),而不能是一個對象的屬性,比如顏色,大小等,但是屬性動畫就可以
??Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
??另一個弊端是View動畫只能改變View繪制的地方,并不是真正的View本身。舉個例子,如果你做一個穿過屏幕的Button動效,Button的繪制時是正確的,但是Button的實際可以點擊的位置是沒有變的,所以你得通過自己的邏輯來解決這個問題(就是說你還是得點擊原來Button的位置才會有相應的響應事件)
??With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
??通過屬性動畫系統,這些約束將被徹底解除,你可以對任何對象(Views and non-Views)的任何屬性做動畫效果,并且對象自身也會被實際的改變。屬性動畫系統在具體實施動畫方面也是更加強大的。從更高層次來說,你可以選擇你想要的屬性來給其添加動畫,諸如顏色、位置或者尺寸,并且你可以通過多個插值器和定義多個動畫同步來實現特定方面的動畫。
2.具體類
??屬性動畫常用的類有:ValueAnimator, ObjectAnimator, or AnimatorSet.這三個類可以通過一張圖來很好的解釋

20160730191354538.png

可以看到,ValueAnimator和AnimatorSet是繼承自Animator類的,并且位于同一等級;ObjectAnimator和TimeAnimator是繼承自ValueAnimator類的,兩者位于同一等級。
(1)ValueAnimator
(https://developer.android.com/reference/android/animation/ValueAnimator.html)
??This class provides a simple timing engine for running animations which calculate animated values and set them on target objects. 該類通過通過計算動效值為運動的動畫提供了一個簡單的時間引擎,并將其作用于目標對象上面。
??The ValueAnimator class lets you animate values of some type for the duration of an animation by specifying a set of int, float, or color values to animate through. You obtain a ValueAnimator by calling one of its factory methods: **ofInt(), ofFloat(), **or ofObject()
??ValueAnimator類讓你在動畫運行期間通過指定int,float或者顏色值來確定動畫值。你可以通過調用ofInt(), ofFloat(), 或者 ofObject()這三個方法來獲得一個ValueAnimator。例如:

ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,100);
valueAnimator.setDuration(3000);

valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float value=(Float) animation.getAnimatedValue();
        ivImage.setTranslationX(value);
    }
});
valueAnimator.start();
    這段代碼中我們設置了一個動畫值從0變到100的,持續時間3S的動畫屬性,然后給這個ValueAnimator設置一個監聽器,
通過getAnimatedValue()取得動畫值并將其通過getAnimatedValue()設置給了ImageView對象在X軸的坐標,運行后會看
到這個ImageView在X軸上移動了100px.

啊,好吧,看過本人蹩手蹩腳的翻譯之后相信你已經是一頭霧水了,上面提到了ValueAnimator類的三個常用的方法:ofFloat,ofInt,ofObject.
①ValueAnimator ofInt (int... values)
Parameters
**values int: **A set of values that the animation will animate between over time.
??這是一組可變參數,當然參數的是Int類型。我們可以傳任何值進去,傳進去的值列表,就表示動畫播放時代的變化范圍,比如ofInt(3,9,6)就表示從數值3變化到數值9再變化到數值6。
Returns
ValueAnimator ** A ValueAnimator object that is set up to animate between the given values.
??返回一個ValueAnimator對象,給誰設置的值就返回誰。
②ValueAnimator ofFloat (float... values) 參數意義同上
Parameters
values float: A set of values that the animation will animate between over time.
Returns
ValueAnimator ** A ValueAnimator object that is set up to animate between the given values.
③ValueAnimator ofObject (TypeEvaluator evaluator, Object... values)
??這里第一個參數是TypeEvaluator類 (https://developer.android.com/reference/android/animation/TypeEvaluator.html)
關于這個動畫我們后面暫時用不到,我們后面再說。
??
ValueAnimator是計算動畫過程中變化的值,包含動畫的開始值,結束值,持續時間等屬性。但是這些值與我們的控件是無關的,要想把計算出來的值應用到對象上,必須為ValueAnimator注冊一個監聽器,該監聽器負責更新對象的屬性值。在實現這個監聽器的時候,可以通過getAnimatedValue()的方法來獲取當前動畫的值,拿到這個值后,我們就可以設置給這個對象的在X或者Y軸的偏移量等等,來讓圖像實現我們預定的動畫。

(2)ObjectAnimator
(https://developer.android.com/reference/android/animation/ObjectAnimator.html)
??相比于ValueAnimator,在開發中可能ObjectAnimator要比ValueAnimator用的多,因為ObjectAnimator可以直接操作對象的屬性,而不用像ValueAnimator那么麻煩的設置監聽器檢測值動畫值。
假如讓一個ImageView做旋轉的動畫,代碼可以這樣寫:

ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(ivImage,"rotation",0,360);
objectAnimator.setDuration(3000);
objectAnimator.start();
    這里第一個參數要求傳入一個object對象,即進行動畫的對象,在上面我們傳了一個ImageView。第二個參數是屬性的名字,因為做旋轉動畫所以這里傳的屬性的名字為“rotation”。后面就是可變參數了,這里我們傳的是0,360,表示讓ImageView旋轉360度,然后設置時長,調用start方法

關于ObjectAnimator類這里有很多方法:

ObjectAnimator ofFloat (Object target, String propertyName, float... values)
ObjectAnimator ofInt (T target, Property<T, Integer> xProperty, Property<T, Integer> yProperty,  Path path)
ObjectAnimator ofInt (T target, Property<T, Integer> property, int... values)
ObjectAnimator ofInt (Object target, String propertyName, int... values)
ObjectAnimator ofInt (Object target, String xPropertyName,  String yPropertyName,  Path path)
等等,這里就不做一一講解了.這里我們對經常用到的屬性做一個小的總結:
①translationX和translationY:表示在X軸或者Y軸方向上的位移
② scaleX和scaleY:表示在X軸或者Y軸方向上的縮放
③rotation、rotationX和rotationY:這三個屬性控制View對象圍繞支點進行2D和3D旋轉。
④ pivotX和pivotY:這兩個屬性控制著View對象的支點位置,圍繞這個支點進行旋轉和縮放變換處理。默認情況下,該支點的位置就是View對象的中心點。
⑤x和y:這是兩個簡單實用的屬性,它描述了View對象在它的容器中的最終位置,它是最初的左上角坐標和translationX和translationY值的累計和。
⑥ alpha:它表示View對象的alpha透明度。默認值是1(不透明),0代表完全透明(不可見)。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,462評論 25 708
  • 1 背景 不能只分析源碼呀,分析的同時也要整理歸納基礎知識,剛好有人微博私信讓全面說說Android的動畫,所以今...
    未聞椛洺閱讀 2,757評論 0 10
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,568評論 2 45
  • 我必須是你近旁的一株木棉 作為樹的形象跟你站在一起
    小鹿和小璐閱讀 179評論 0 1
  • 今天下午,我準備把家里的玻璃擦一擦。 我把抹布洗干凈,來到了窗臺上,就開始擦玻璃啦。我爬到窗臺上,用抹布上下來...
    谷佳琦閱讀 1,999評論 0 0