如何为ImageView创建震动动画

28

我对这个动画没有头绪。

我该如何像使用XML那样做呢?还是有其他解决方案?

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fillAfter="true"> 
    ......
</set>

感谢你的帮助


请查看这个答案:这对我非常有效 https://dev59.com/vmox5IYBdhLWcg3wFAY1#9449590 - rDroid
6个回答

33

这段代码可以使一个视图水平方向上晃动。

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_5"
    android:toXDelta="10" />

周期_5.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="5" />

震动ImageView的方法

public void onShakeImage() {    
   Animation shake;
   shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);

   ImageView image;
   image = (ImageView) findViewById(R.id.image_view);

   image.startAnimation(shake); // starts animation
}

setAnimation不会启动动画。 您需要使用startAnimation(shake)来实际开始动画。 - Eli

10

1) 振动或 2) 抖动 (使用属性动画)以下代码适用于我。

 ObjectAnimator rotate = ObjectAnimator.ofFloat(animateView, "rotation", 0f, 20f, 0f, -20f, 0f); // rotate o degree then 20 degree and so on for one loop of rotation.
// animateView (View object) 
        rotate.setRepeatCount(20); // repeat the loop 20 times
        rotate.setDuration(100); // animation play time 100 ms 
        rotate.start();

我的救星 @GeetThakur... 呵呵,简短而美妙! - gumuruh

6
在anim目录下创建shake.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:duration="70"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:toDegrees="0" />
<translate
    android:duration="70"
    android:fromXDelta="40"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="5"
    android:repeatMode="reverse"
    android:toXDelta="-40" />

在您的Java文件中添加以下方法:

public void animateView(View view){
    Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
    view.startAnimation(shake);
}

并将您的视图传递到动画方法中。
animateView(yourView);

3
在 anim 目录中创建动画文件:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromDegrees="-10"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="10" />



Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
your_view.startAnimation(shake);

2
这对我来说很顺利,就像我预期的一样。
private void Shake(View view)
{
    RotateAnimation rotate = new RotateAnimation(-5, 5,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(250);
    rotate.setStartOffset(50);
    rotate.setRepeatMode(Animation.REVERSE);
    rotate.setInterpolator(new CycleInterpolator(5));
    view.startAnimation(rotate);
}

1
你的解决方案符合我摇晃以打开奖励的需求。谢谢! - Sarith Nob

0

我已经为imageView创建了类似BUZZ的动画效果。我还添加了延迟,使其真正像Buzzy effect

  1. 在您的Activity中:

这里,animShakeandroid.view.animation

    animShake = AnimationUtils.loadAnimation(this, R.anim.shake)
    imageView.startAnimation(animShake)

不要忘记为BUZZ效果添加AnimationListener以实现小延迟。
animShake.setAnimationListener(object : Animation.AnimationListener {
        override fun onAnimationRepeat(animation: Animation?) {

        }

        override fun onAnimationEnd(animation: Animation?) {
            Handler().postDelayed({
                imageView.startAnimation(animShake)
            }, 1000)

        }

        override fun onAnimationStart(animation: Animation?) {

        }
    })
  • 仅包含translateshake XML文件:

     android:duration="75"
     android:fromXDelta="-18%"
     android:repeatCount="11"
     android:repeatMode="reverse"
     android:toXDelta="18%" />
    

  • 网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接