FrameLayout main = (FrameLayout) findViewById(R.id.main);
// 移動アニメーション
TranslateAnimation2 translateAnimation = new TranslateAnimation2(1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeInOutQuart);
translateAnimation.setDuration(1200);
translateAnimation.setFillAfter(true);
main.startAnimation(translateAnimation);
TranslateAnimation2クラスとか
Easingクラスとかいるからね
public class Easing {
public static String easeInQuad = "easeInQuad";
public static String easeOutQuad = "easeOutQuad";
public static String easeInOutQuad = "easeInOutQuad";
public static String easeInCubic = "easeInCubic";
public static String easeOutCubic = "easeOutCubic";
public static String easeInOutCubic = "easeInOutCubic";
public static String easeInQuart = "easeInQuart";
public static String easeOutQuart = "easeOutQuart";
public static String easeInOutQuart = "easeInOutQuart";
public static String easeInQuint = "easeInQuint";
public static String easeOutQuint = "easeOutQuint";
public static String easeInOutQuint = "easeInOutQuint";
public static String easeInSine = "easeInSine";
public static String easeOutSine = "easeOutSine";
public static String easeInOutSine = "easeInOutSine";
public static String easeInExpo = "easeInExpo";
public static String easeOutExpo = "easeOutExpo";
public static String easeInOutExpo = "easeInOutExpo";
public static String easeInCirc = "easeInCirc";
public static String easeOutCirc = "easeOutCirc";
public static String easeInOutCirc = "easeInOutCirc";
public static String easeInElastic = "easeInElastic";
public static String easeOutElastic = "easeOutElastic";
public static String easeInOutElastic = "easeInOutElastic";
public static String easeInBack = "easeInBack";
public static String easeOutBack = "easeOutBack";
public static String easeInOutBack = "easeInOutBack";
public static String easeInBounce = "easeInBounce";
public static String easeOutBounce = "easeOutBounce";
public static String easeInOutBounce = "easeInOutBounce";
public static float move(float t, String easing) {
if (easing.equals(easeInQuad)) {
return easeInQuad(t);
} else if (easing.equals(easeOutQuad)) {
return easeOutQuad(t);
} else if (easing.equals(easeInOutQuad)) {
return easeInOutQuad(t);
} else if (easing.equals(easeInCubic)) {
return easeInCubic(t);
} else if (easing.equals(easeOutCubic)) {
return easeOutCubic(t);
} else if (easing.equals(easeInOutCubic)) {
return easeInOutCubic(t);
} else if (easing.equals(easeInQuart)) {
return easeInQuart(t);
} else if (easing.equals(easeOutQuart)) {
return easeOutQuart(t);
} else if (easing.equals(easeInOutQuart)) {
return easeInOutQuart(t);
} else if (easing.equals(easeInQuint)) {
return easeInQuint(t);
} else if (easing.equals(easeOutQuint)) {
return easeOutQuint(t);
} else if (easing.equals(easeInOutQuint)) {
return easeInOutQuint(t);
} else if (easing.equals(easeInSine)) {
return easeInSine(t);
} else if (easing.equals(easeOutSine)) {
return easeOutSine(t);
} else if (easing.equals(easeInOutSine)) {
return easeInOutSine(t);
} else if (easing.equals(easeInExpo)) {
return easeInExpo(t);
} else if (easing.equals(easeOutExpo)) {
return easeOutExpo(t);
} else if (easing.equals(easeInOutExpo)) {
return easeInOutExpo(t);
} else if (easing.equals(easeInCirc)) {
return easeInCirc(t);
} else if (easing.equals(easeOutCirc)) {
return easeOutCirc(t);
} else if (easing.equals(easeInOutCirc)) {
return easeInOutCirc(t);
} else if (easing.equals(easeInElastic)) {
return easeInElastic(t);
} else if (easing.equals(easeOutElastic)) {
return easeOutElastic(t);
} else if (easing.equals(easeInOutElastic)) {
return easeInOutElastic(t);
} else if (easing.equals(easeInBack)) {
return easeInBack(t);
} else if (easing.equals(easeOutBack)) {
return easeOutBack(t);
} else if (easing.equals(easeInOutBack)) {
return easeInOutBack(t);
} else if (easing.equals(easeInBounce)) {
return easeInBounce(t);
} else if (easing.equals(easeOutBounce)) {
return easeOutBounce(t);
} else if (easing.equals(easeInOutBounce)) {
return easeInOutBounce(t);
}
return t;
}
public static float easeInQuad(float t) {
return easeInQuad(t, 0f, 1f, 1f);
}
public static float easeOutQuad(float t) {
return easeInQuad(t, 0f, 1f, 1f);
}
public static float easeInOutQuad(float t) {
return easeInOutQuad(t, 0f, 1f, 1f);
}
public static float easeInCubic(float t) {
return easeInCubic(t, 0f, 1f, 1f);
}
public static float easeOutCubic(float t) {
return easeOutCubic(t, 0f, 1f, 1f);
}
public static float easeInOutCubic(float t) {
return easeInOutCubic(t, 0f, 1f, 1f);
}
public static float easeInQuart(float t) {
return easeInQuart(t, 0f, 1f, 1f);
}
public static float easeOutQuart(float t) {
return easeOutQuart(t, 0f, 1f, 1f);
}
public static float easeInOutQuart(float t) {
return easeInOutQuart(t, 0f, 1f, 1f);
}
public static float easeInQuint(float t) {
return easeInQuint(t, 0f, 1f, 1f);
}
public static float easeOutQuint(float t) {
return easeOutQuint(t, 0f, 1f, 1f);
}
public static float easeInOutQuint(float t) {
return easeInOutQuint(t, 0f, 1f, 1f);
}
public static float easeInSine(float t) {
return easeInSine(t, 0f, 1f, 1f);
}
public static float easeOutSine(float t) {
return easeOutSine(t, 0f, 1f, 1f);
}
public static float easeInOutSine(float t) {
return easeInOutSine(t, 0f, 1f, 1f);
}
public static float easeInExpo(float t) {
return easeInExpo(t, 0f, 1f, 1f);
}
public static float easeOutExpo(float t) {
return easeOutExpo(t, 0f, 1f, 1f);
}
public static float easeInOutExpo(float t) {
return easeInOutExpo(t, 0f, 1f, 1f);
}
public static float easeInCirc(float t) {
return easeInCirc(t, 0f, 1f, 1f);
}
public static float easeOutCirc(float t) {
return easeOutCirc(t, 0f, 1f, 1f);
}
public static float easeInOutCirc(float t) {
return easeInOutCirc(t, 0f, 1f, 1f);
}
public static float easeInElastic(float t) {
return easeInElastic(t, 0f, 1f, 1f);
}
public static float easeOutElastic(float t) {
return easeOutElastic(t, 0f, 1f, 1f);
}
public static float easeInOutElastic(float t) {
return easeInOutElastic(t, 0f, 1f, 1f);
}
public static float easeInBack(float t) {
return easeInBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeOutBack(float t) {
return easeOutBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeInOutBack(float t) {
return easeInOutBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeInBounce(float t) {
return easeInBounce(t, 0f, 1f, 1f);
}
public static float easeOutBounce(float t) {
return easeOutBounce(t, 0f, 1f, 1f);
}
public static float easeInOutBounce(float t) {
return easeInOutBounce(t, 0f, 1f, 1f);
}
/**/
public static float easeInQuad(float t, float b, float c, float d) {
return c * (t /= d) * t + b;
}
public static float easeOutQuad(float t, float b, float c, float d) {
return -c * (t /= d) * (t - 2f) + b;
}
public static float easeInOutQuad(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1)
return c / 2f * t * t + b;
return -c / 2f * ((--t) * (t - 2f) - 1) + b;
}
public static float easeInCubic(float t, float b, float c, float d) {
return c * (t /= d) * t * t + b;
}
public static float easeOutCubic(float t, float b, float c, float d) {
return c * ((t = t / d - 1f) * t * t + 1f) + b;
}
public static float easeInOutCubic(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1)
return c / 2f * t * t * t + b;
return c / 2f * ((t -= 2f) * t * t + 2f) + b;
}
public static float easeInQuart(float t, float b, float c, float d) {
return c * (t /= d) * t * t * t + b;
}
public static float easeOutQuart(float t, float b, float c, float d) {
return -c * ((t = t / d - 1f) * t * t * t - 1f) + b;
}
public static float easeInOutQuart(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1f)
return c / 2f * t * t * t * t + b;
return -c / 2f * ((t -= 2f) * t * t * t - 2f) + b;
}
public static float easeInQuint(float t, float b, float c, float d) {
return c * (t /= d) * t * t * t * t + b;
}
public static float easeOutQuint(float t, float b, float c, float d) {
return c * ((t = t / d - 1f) * t * t * t * t + 1f) + b;
}
public static float easeInOutQuint(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1f)
return c / 2f * t * t * t * t * t + b;
return c / 2f * ((t -= 2f) * t * t * t * t + 2f) + b;
}
public static float easeInSine(float t, float b, float c, float d) {
return (float) (-c * Math.cos(t / d * (Math.PI / 2f)) + c + b);
}
public static float easeOutSine(float t, float b, float c, float d) {
return (float) (c * Math.sin(t / d * (Math.PI / 2f)) + b);
}
public static float easeInOutSine(float t, float b, float c, float d) {
return (float) (-c / 2f * (Math.cos(Math.PI * t / d) - 1) + b);
}
public static float easeInExpo(float t, float b, float c, float d) {
return (float) ((t == 0) ? b : c * Math.pow(2, 10f * (t / d - 1f)) + b);
}
public static float easeOutExpo(float t, float b, float c, float d) {
return (float) ((t == d) ? b + c : c * (-Math.pow(2, -10f * t / d) + 1f) + b);
}
public static float easeInOutExpo(float t, float b, float c, float d) {
if (t == 0)
return b;
if (t == d)
return b + c;
if ((t /= d / 2f) < 1f)
return (float) (c / 2f * Math.pow(2, 10f * (t - 1f)) + b);
return (float) (c / 2f * (-Math.pow(2, -10f * --t) + 2f) + b);
}
public static float easeInCirc(float t, float b, float c, float d) {
return (float) (-c * (Math.sqrt(1f - (t /= d) * t) - 1f) + b);
}
public static float easeOutCirc(float t, float b, float c, float d) {
return (float) (c * Math.sqrt(1 - (t = t / d - 1) * t) + b);
}
public static float easeInOutCirc(float t, float b, float c, float d) {
if ((t /= d / 2) < 1)
return (float) (-c / 2 * (Math.sqrt(1 - t * t) - 1) + b);
return (float) (c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b);
}
public static float easeInElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d) == 1)
return b + c;
p = d * 0.3f;
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
return (float) (-(a * Math.pow(2, 10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p)) + b);
}
public static float easeOutElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d) == 1)
return b + c;
p = d * 0.3f;
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
return (float) (a * Math.pow(2, -10f * t) * Math.sin((t * d - s) * (2f * Math.PI) / p) + c + b);
}
public static float easeInOutElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d / 2) == 2)
return b + c;
p = d * (0.3f * 1.5f);
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
if (t < 1)
return (float) (-0.5f * (a * Math.pow(2, 10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p)) + b);
return (float) (a * Math.pow(2, -10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p) * 0.5f + c + b);
}
public static float easeInBack(float t, float b, float c, float d, float s) {
return c * (t /= d) * t * ((s + 1f) * t - s) + b;
}
public static float easeOutBack(float t, float b, float c, float d, float s) {
return c * ((t = t / d - 1f) * t * ((s + 1f) * t + s) + 1f) + b;
}
public static float easeInOutBack(float t, float b, float c, float d, float s) {
if ((t /= d / 2f) < 1)
return c / 2f * (t * t * (((s *= (1.525f)) + 1f) * t - s)) + b;
return c / 2f * ((t -= 2f) * t * (((s *= (1.525f)) + 1f) * t + s) + 2f) + b;
}
public static float easeInBounce(float t, float b, float c, float d) {
return c - easeOutBounce(d - t, 0, c, d) + b;
}
public static float easeOutBounce(float t, float b, float c, float d) {
if ((t /= d) < (1f / 2.75f)) {
return c * (7.5625f * t * t) + b;
} else if (t < (2f / 2.75f)) {
return c * (7.5625f * (t -= (1.5f / 2.75f)) * t + 0.75f) + b;
} else if (t < (2.5f / 2.75f)) {
return c * (7.5625f * (t -= (2.25f / 2.75f)) * t + 0.9375f) + b;
} else {
return c * (7.5625f * (t -= (2.625f / 2.75f)) * t + 0.984375f) + b;
}
}
public static float easeInOutBounce(float t, float b, float c, float d) {
if (t < d / 2f)
return easeInBounce(t * 2f, 0, c, d) * 0.5f + b;
return easeOutBounce(t * 2f - d, 0, c, d) * 0.5f + c * 0.5f + b;
}
}
そんでもって
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
public class TranslateAnimation2 extends Animation {
private int mFromXType = ABSOLUTE;
private int mToXType = ABSOLUTE;
private int mFromYType = ABSOLUTE;
private int mToYType = ABSOLUTE;
private float mFromXValue = 0.0f;
private float mToXValue = 0.0f;
private float mFromYValue = 0.0f;
private float mToYValue = 0.0f;
private float mFromXDelta;
private float mToXDelta;
private float mFromYDelta;
private float mToYDelta;
private String easing;
/**
* Constructor to use when building a TranslateAnimation from code
*
* @param fromXDelta
* Change in X coordinate to apply at the start of the animation
* @param toXDelta
* Change in X coordinate to apply at the end of the animation
* @param fromYDelta
* Change in Y coordinate to apply at the start of the animation
* @param toYDelta
* Change in Y coordinate to apply at the end of the animation
*/
public TranslateAnimation2(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,String _easing) {
mFromXValue = fromXDelta;
mToXValue = toXDelta;
mFromYValue = fromYDelta;
mToYValue = toYDelta;
mFromXType = ABSOLUTE;
mToXType = ABSOLUTE;
mFromYType = ABSOLUTE;
mToYType = ABSOLUTE;
easing = _easing;
setInterpolator(new LinearInterpolator());
}
/**
* Constructor to use when building a TranslateAnimation from code
*
* @param fromXType
* Specifies how fromXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param fromXValue
* Change in X coordinate to apply at the start of the animation.
* This value can either be an absolute number if fromXType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param toXType
* Specifies how toXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param toXValue
* Change in X coordinate to apply at the end of the animation.
* This value can either be an absolute number if toXType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param fromYType
* Specifies how fromYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param fromYValue
* Change in Y coordinate to apply at the start of the animation.
* This value can either be an absolute number if fromYType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param toYType
* Specifies how toYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param toYValue
* Change in Y coordinate to apply at the end of the animation.
* This value can either be an absolute number if toYType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
public TranslateAnimation2(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue,String _easing) {
mFromXValue = fromXValue;
mToXValue = toXValue;
mFromYValue = fromYValue;
mToYValue = toYValue;
mFromXType = fromXType;
mToXType = toXType;
mFromYType = fromYType;
mToYType = toYType;
setInterpolator(new LinearInterpolator());
easing = _easing;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float dx = mFromXDelta;
float dy = mFromYDelta;
if (mFromXDelta != mToXDelta) {
dx = mFromXDelta + ((mToXDelta - mFromXDelta) * Easing.move(interpolatedTime,easing));
}
if (mFromYDelta != mToYDelta) {
dy = mFromYDelta + ((mToYDelta - mFromYDelta) * Easing.move(interpolatedTime,easing));
}
t.getMatrix().setTranslate(dx, dy);
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);
mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);
mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);
mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);
}
}
つかうときはこげな感じ
FrameLayout main = (FrameLayout) findViewById(R.id.main);
//てけとーなレイアウトとかを
TranslateAnimation2 translateAnimation = new TranslateAnimation2(-1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeOutBounce);
translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(true);
main.startAnimation(translateAnimation);
こんな風にすると、びっくりただのアニメーションにバウンスついたりする。これはJavaScriptでもよくある手法だよぬ!
TranslateAnimation2(-1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeOutBounce);
ポイントは上の一行ですね。
クラスファイルを2つ使用するだけでいけるので、ちょびっとアニメーションしたいときなんか簡単でいいですよね。
参考サイトhttp://andante.in/i/java/%E5%89%8D%E5%9B%9E%E4%BD%9C%E3%81%A3%E3%81%9Feasing%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%80%81translate%E3%82%A2%E3%83%8B%E3%83%A1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AB%E3%82%A4%E3%83%BC/