Android屬性動畫基礎(chǔ):TypeEvaluator

??本文簡單介紹一下TypeEvaluator,來了解一下它的用途。TypeEvaluator是一個接口,我們可以自定義該接口實例,并通過ValueAnimator的setEvaluator(TypeEvaluator)方法來控制動畫的更新計算表達式。如果您只是想利用屬性動畫操縱單一數(shù)值變化(如控制View的X或Y方向旋轉(zhuǎn)、X或Y平移等,或僅僅是操縱與對象無關(guān)的數(shù)值),那么你完全不必關(guān)心TypeEvaluator,通過插值器控制就好了,殺雞焉用牛刀啊,牙簽足以。但是如果您要同時操縱對象的多個屬性怎么辦,比如您想模擬小球運動,要控制X和Y兩個方向的坐標(biāo),而且由于兩個方向的加速度不一樣導(dǎo)致X和Y坐標(biāo)的計算方式也不一樣,難道您想通過動畫組合的方式來分別處理X、Y嗎,您當(dāng)然可以這么做,不過并不好,此外如果還要求您記錄運動軌跡,這種X、Y組合動畫就無能為力了。這種相對復(fù)雜的場景,TypeEvaluator就可以大展身手了,好了,先看一下源碼及api介紹

/**
 * Interface for use with the {@link ValueAnimator#setEvaluator(TypeEvaluator)} function. Evaluators
 * allow developers to create animations on arbitrary property types, by allowing them to supply
 * custom evaluators for types that are not automatically understood and used by the animation
 * system.
 *
 * @see ValueAnimator#setEvaluator(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);
}

??這個接口代碼看起來是相當(dāng)簡單,就一個方法,通過接口描述,我們可以知道,TypeEvaluator能夠支持我們創(chuàng)建支持任意屬性類型的動畫。如果您有看過谷歌對屬性動畫的描述,就該知道,屬性動畫能夠操縱任類型意屬性,如果操縱的是對象,系統(tǒng)只內(nèi)置了數(shù)值型計算方式,那么對象屬性如何更新計算?TypeEvaluator就是解決這個問題的,當(dāng)然,您也可以使用TypeEvaluator來計算基礎(chǔ)數(shù)值型數(shù)據(jù),這不是不可以的。evaluate方法就是更新計算方法,通過該方法計算出來的屬性值來更新動畫,參數(shù)fraction是插值器getInterpolation方法計算出來的時間因子(請參考Android屬性動畫基礎(chǔ):你是否真的了解插值器(TimeInterpolator));startValue和endValue分別表示每個動畫區(qū)間段的起始值和終點值。
??以我們之前說的模擬小球運動軌跡為示例,寫段簡單的使用方法(下一篇文章會給出完整示例及另一種模擬方式)

 // 描述小球運動軌跡坐標(biāo)
    private class Point {
        private float pointX;
        private float pointY;

        private Point(float pointX, float pointY) {
            this.pointX = pointX;
            this.pointY = pointY;
        }

        private float getPointX() {
            return pointX;
        }

        private float getPointY() {
            return pointY;
        }
    }

 // 模擬平拋運動軌跡的估值器
    private class MyTypeEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            float pointX = fraction * endValue.getPointX() + startValue.getPointX();
            float pointY = fraction * fraction * endValue.getPointY() + startValue.getPointY();
            Point point  = new Point(pointX, pointY);
            // ToDo 您可以在此記錄或做相應(yīng)操作
            return point;
        }
    }

    ValueAnimator animator = ValueAnimator.ofObject(new MyTypeEvaluator(), new Point(0, 0), new Point(300, 600)).setDuration(1000);
    animator.setInterpolator(new LinearInterpolator());
    animator .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point point = (Point) animation.getAnimatedValue();
               // ToDo 您也可以在此記錄或做相應(yīng)操作
            }
        });
    animator.start();

??這篇文章比較簡單,就到這,對于復(fù)雜屬性動畫,請不要忘記TypeEvaluator

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容