androidのアニメーションを行う場合に、Interpolatorがいくつかあって、多くの場合は初速は早く、到達はゆっくりのイージングなどを利用して動きにメリハリをつけると思います。
このイージングを利用するのは、アニメーション利用する場合ですが、アニメーションの種類が少し少ない為(アルファや位置の移動など)で、そのほかのモーションを自分でつける時(現在、個人的にアコーディオンのようなインターフェースを作成したいのですが。。)イージングを他のアニメーションと同じようなイージングを実現したいです。
ただ、アニメーションのつける方法が、手動のため、イージングなどは自分で計算式を書くなどを利用する必要があります。
その中でイージングの計算だけは、Interpolatorが行なってくれるようになっています。
簡単には、
private Interpolator mInterpolator = new LinearInterpolator();
のようにInterpolatorを作成します。
mInterpolator.getInterpolation(α)
ここで、αのぶぶんには0~1のあたいが入ります。それだけで、現在の進捗を取得する事ができます。
説明だけではわかりにくいと思うので下記に具体例を示させていただきます。
次のような実験的なLogを用意します。
Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.1f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.2f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.3f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.4f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.5f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.6f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.7f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.8f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(0.9f))); Log.e("InterpolatorTest", Float.toString(mInterpolator.getInterpolation(1.0f)));
上記を実行すると下記の値が取得できます。
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
この値はlinearlayoutなので当たり前の値ではあります。
次にInterPolatorのクラスをAccelerateInterpolatorに変えてみます。
0.040000003
0.09
0.16000001
0.25
0.36
0.48999998
0.64000005
0.80999994
1.0
最初はゆっくりで、到着に近づくにつれてスピードがあがるのが確認できました。
次に、DecelerateInterpolatorを使ってみます。
0.19000006
0.35999995
0.51
0.64
0.75
0.84000003
0.90999997
0.96
0.99
1.0
最初にスピードが早くて、ゆっくり到着するのがわかります。
BounceInterpolatorなどでバウンドさせるのにも利用ができるようです。
上記を用いて、androidの標準のアニメーション以外にも自分の作るアニメーションにInterpolatorを適用する事ができるようになりました。
ぜひ、おためしくださいませ。