亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義View實(shí)現(xiàn)動(dòng)畫效果詳解

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

幀動(dòng)畫

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

<?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>

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

補(bǔ)間動(dòng)畫

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

旋轉(zhuǎn)動(dòng)畫

<?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>

透明度動(dòng)畫

<?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>

平移動(dòng)畫

<?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>

縮放動(dòng)畫

<?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>

屬性動(dòng)畫

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

ValueAnimator

ValueAnimator.ofFloat(0f, 1f)

創(chuàng)建一個(gè)從0逐漸變化到1點(diǎn)動(dòng)畫。

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)聽動(dòng)畫的狀態(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);

}

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

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

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

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.
}

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

public abstract void setInterpolator(TimeInterpolator value);

設(shè)置插值器,因?yàn)檫@個(gè)方法是Animator抽象類的。

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

相關(guān)文章

最新評(píng)論