安卓仿IOS刪除抖動動畫
阿新 • • 發佈:2019-01-30
public class ShakeAnimation { public boolean mNeedShake = false; //是否要播放抖動動畫,預設否 private static final int ICON_WIDTH = 80; private static final int ICON_HEIGHT = 94; private static final float DEGREE_0 = 1.8f; private static final float DEGREE_1 = - 2.0f; private static final float DEGREE_2 = 2.0f; private static final float DEGREE_3 = - 1.5f; private static final float DEGREE_4 = 1.5f; private static final int ANIMATION_DURATION = 100; private int mCount = 0; private static final ShakeAnimation instance = new ShakeAnimation(); private ShakeAnimation(){} public static final ShakeAnimation getInstance() { return instance; } // 晃動動畫 public void shakeAnimation(final View v) { DisplayMetrics dm = new DisplayMetrics(); float mDensity = dm.density; float rotate; int c = mCount++ % 5; if (c == 0) { rotate = DEGREE_0; } else if (c == 1) { rotate = DEGREE_1; } else if (c == 2) { rotate = DEGREE_2; } else if (c == 3) { rotate = DEGREE_3; } else { rotate = DEGREE_4; } final RotateAnimation ra1 = new RotateAnimation(rotate, - rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2); final RotateAnimation ra2 = new RotateAnimation(- rotate, rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2); ra1.setDuration(ANIMATION_DURATION); ra2.setDuration(ANIMATION_DURATION); // 設定旋轉動畫的監聽 ra1.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { if (mNeedShake) { ra1.reset(); // 重置動畫 v.startAnimation(ra2); // 第一個動畫結束開始第二個旋轉動畫 } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } }); ra2.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { if (mNeedShake) { ra2.reset(); v.startAnimation(ra1);// 第二個動畫結束開始第一個旋轉動畫 } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } }); v.startAnimation(ra1); } }