1 2 3 4 5 6 7 |
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クラスとかいるからね
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
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; } } |
そんでもって
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
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); } } |
つかうときはこげな感じ
1 2 3 4 5 6 7 8 |
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つ使用するだけでいけるので、ちょびっとアニメーションしたいときなんか簡単でいいですよね。