android動畫插值器

在android的動畫中,有一個插值器,他的作用是告訴動畫某個屬性(比如顏色的漸變)如何隨時間變化。它將以線性方式變化,還是以指數方式變化?它是否在開始時變化速度很快,然後逐漸變慢?使用方法是在需

要設置插值器的動畫xml文件中增加一個interpoplator屬性,如下:
android:interpoplator="@android:anim/accelerate_interpoplator"
android:fromAlpha="0.0" 
android:toAlpha="1.0"
android:duration="1000" />
 
這樣就可以使用了,在從完全透明變化爲不透明的過程中,你可以在res/anim/accelerate_interpoplator.xml中定義自己想要的效果,如變化速率等。



下面給出了幾個插值器的源碼,供參考:

AccelerateDecelerateInterpolator(); 
註解:An interpolator where the rate of change starts and ends slowly but accelerates through the middle.
源碼:public float getInterpolation(float input) {

        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;

    }





AccelerateInterpolator(float mFactor); 
註解:An interpolator where the rate of change starts out slowly and and then accelerates.
源碼: public float getInterpolation(float input) {

        if (mFactor == 1.0f) {

            return input * input;

        } else {

            return (float)Math.pow(input, mDoubleFactor);

        }

    }





CycleInterpolator(float cycles); 
註解:Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.
源碼:public float getInterpolation(float input) {

        return (float)(Math.sin(2 * mCycles * Math.PI * input));

    }





DecelerateInterpolator(); 
註解:An interpolator where the rate of change starts out quickly and and then decelerates.
源碼:public float getInterpolation(float input) {

        float result;

        if (mFactor == 1.0f) {

            result = (float)(1.0f - (1.0f - input) * (1.0f - input));

        } else {

            result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));

        }

        return result;

    }






AnticipateInterpolator(); 
註解:An interpolator where the change starts backward then flings forward.
源碼:public float getInterpolation(float t) {

        // a(t) = t * t * ((tension + 1) * t - tension)

        return t * t * ((mTension + 1) * t - mTension);

    }





AnticipateOvershootInterpolator(); 
註解:An interpolator where the change starts backward then flings forward and overshoots the target value and finally goes back to the final value.
源碼:public float getInterpolation(float t) {

        // a(t, s) = t * t * ((s + 1) * t - s)

        // o(t, s) = t * t * ((s + 1) * t + s)

        // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5

        // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0

        if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);

        else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);

    }

     private static float a(float t, float s) {

        return t * t * ((s + 1) * t - s);

    }

 

              private static float o(float t, float s) {

        return t * t * ((s + 1) * t + s);

    }

       public AnticipateOvershootInterpolator(float tension) {

        mTension = tension * 1.5f;

    }




BounceInterpolator(); 
註解:An interpolator where the change bounces at the end.
源碼:private static float bounce(float t) {

        return t * t * 8.0f;

    }

 

              public float getInterpolation(float t) {

        // _b(t) = t * t * 8

        // bs(t) = _b(t) for t < 0.3535

        // bs(t) = _b(t - 0.54719) + 0.7 for t < 0.7408

        // bs(t) = _b(t - 0.8526) + 0.9 for t < 0.9644

        // bs(t) = _b(t - 1.0435) + 0.95 for t <= 1.0

        // b(t) = bs(t * 1.1226)

        t *= 1.1226f;

        if (t < 0.3535f) return bounce(t);

        else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;

        else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;

        else return bounce(t - 1.0435f) + 0.95f;

    }





OvershootInterpolator(); 
註解:An interpolator where the change flings forward and overshoots the last value then comes back.
源碼:public float getInterpolation(float t) {

        // _o(t) = t * t * ((tension + 1) * t + tension)

        // o(t) = _o(t - 1) + 1

        t -= 1.0f;

        return t * t * ((mTension + 1) * t + mTension) + 1.0f;

    }





LinearInterpolator();
註解:An interpolator where the rate of change is constant
源碼:public float getInterpolation(float input) {

        return input;

    }



發佈了22 篇原創文章 · 獲贊 19 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章