shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"
android:fromXDelta="0"
android:interpolator="@anim/cycle_7"
android:toXDelta="10" />
cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />
注:以上是實現左右晃動動畫的配置文件
android:interpolator的解釋
interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重複等。
通俗點說,動畫的進度使用 Interpolator
控制。Interpolator 定義了動畫的變化速度,可以實現勻速、正加速、負加速、無規則變加速等。Interpolator 是基類,封裝了所有 Interpolator 的共同方法,它只有一個方法,即 getInterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了幾個 Interpolator
子類,實現了不同的速度曲線,如下:
AccelerateDecelerateInterpolator 在動畫開始與介紹的地方速率改變比較慢,在中間的時侯加速
AccelerateInterpolator 在動畫開始的地方速率改變比較慢,然後開始加速
CycleInterpolator 動畫循環播放特定的次數,速率改變沿着正弦曲線
DecelerateInterpolator 在動畫開始的地方速率改變比較慢,然後開始減速
LinearInterpolator 在動畫的以均勻的速率改變
對於 LinearInterpolator ,變化率是個常數,即 f (x) = x.
public float getInterpolation(float input) {
return input;
}
Interpolator其他的幾個子類,也都是按照特定的算法,實現了對變化率。還可以定義自己的 Interpolator 子類,實現拋物線、自由落體等物理效果。