欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View實現(xiàn)動畫效果詳解

 更新時間:2023年02月02日 11:32:57   作者:dora  
這篇文章主要為大家詳細介紹了Android如何通過自定義View實現(xiàn)動畫效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

幀動畫

幀動畫就是給定一個完整動畫的所有關(guān)鍵幀,由大腦想象中間的變化過程的一種動畫。

<?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/ic_loading_0" android:duration="100"/>
    <item android:drawable="@drawable/ic_loading_1" android:duration="100"/>
    <item android:drawable="@drawable/ic_loading_2" android:duration="100"/> 
    <item android:drawable="@drawable/ic_loading_3" android:duration="100"/> 
    <item android:drawable="@drawable/ic_loading_4" android:duration="100"/> 
    <item android:drawable="@drawable/ic_loading_5" android:duration="100"/> 
    <item android:drawable="@drawable/ic_loading_6" android:duration="100"/>
    <item android:drawable="@drawable/ic_loading_7" android:duration="100"/>
</animation-list>

將這個xml文件放到drawable文件夾下,就可以引用上了。

補間動畫

補間動畫就是指定動畫開始和結(jié)束的狀態(tài),中間的變化,由計算機自動補齊。補間動畫有4種類型,平移動畫(Translate)、透明度動畫(Alpha)、旋轉(zhuǎn)動畫(Rotate)、縮放動畫(Scale)。

旋轉(zhuǎn)動畫

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
        android:fillAfter="true"
        android:fromDegrees="0"
        android:pivotX="50%" 
        android:pivotY="50%"
        android:repeatCount="1"
        android:toDegrees="180">
</rotate>

透明度動畫

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
       android:fromAlpha="1.0"
       android:toAlpha="0.5">
</alpha>

平移動畫

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
           android:fromXDelta="0"
           android:fromYDelta="0"
           android:toXDelta="100%"
           android:toYDelta="100%">
</translate>

縮放動畫

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"
       android:fromXScale="1.0"
       android:fromYScale="1.0"
       android:toXScale="1.2"
       android:toYScale="1.2"
       android:pivotX="50%"
       android:pivotY="50%">
</scale>

屬性動畫

屬性動畫,就是監(jiān)聽一個屬性的變化值,來完成的動畫。簡言之,就是不斷修改該對象的某個屬性值,然后讓動畫框架來監(jiān)聽它。

ValueAnimator

ValueAnimator.ofFloat(0f, 1f)

創(chuàng)建一個從0逐漸變化到1點動畫。

ObjectAnimator

new ObjectAnimator().addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationCancel(Animator animation) {
        super.onAnimationCancel(animation);
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        super.onAnimationRepeat(animation);
    }

    @Override
    public void onAnimationStart(Animator animation) {
        super.onAnimationStart(animation);
    }

    @Override
    public void onAnimationPause(Animator animation) {
        super.onAnimationPause(animation);
    }

    @Override
    public void onAnimationResume(Animator animation) {
        super.onAnimationResume(animation);
    }
});

監(jiān)聽動畫的狀態(tài)。

TypeEvaluator估值器

public interface TypeEvaluator<T> {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * <code>fraction</code> representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
     * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
     * and <code>t</code> is <code>fraction</code>.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value.
     * @param endValue   The end value.
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public T evaluate(float fraction, T startValue, T endValue);

}

使用估值器來計算兩個邊緣狀態(tài)的所有中間值。

ObjectAnimator.ofObject(target, propertyName, evaluator, values);

target參數(shù)為監(jiān)聽的對象,propertyName為監(jiān)聽的該對象的某個屬性的名字,也可以監(jiān)聽該屬性的setXxx()方法。evaluator傳入一個估值器,values為你要估值的所有關(guān)鍵的點的值,然后通過這些給定的點,來估計一定時間內(nèi)這個點在中間時刻的值。

Interpolator插值器

public interface Interpolator extends TimeInterpolator {
    // A new interface, TimeInterpolator, was introduced for the new android.animation
    // package. This older Interpolator interface extends TimeInterpolator so that users of
    // the new Animator-based animations can use either the old Interpolator implementations or
    // new classes that implement TimeInterpolator directly.
}

用它的實現(xiàn)類來決定動畫的變化速率,比如先快后慢、先慢后快、勻速等。比較常用的有LinearInterpolator(勻速)、AccelerateInterpolator(加速)、DecelerateInterpolator(減速)。ValueAnimator和ObjectAnimator都可以通過調(diào)用

public abstract void setInterpolator(TimeInterpolator value);

設(shè)置插值器,因為這個方法是Animator抽象類的。

到此這篇關(guān)于Android自定義View實現(xiàn)動畫效果詳解的文章就介紹到這了,更多相關(guān)Android自定義View內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論