如何为ImageView制作振动动画

时间:2013-02-04 04:23:26

标签: android animation vibrate

我不知道这个动画。

我怎么能通过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>

谢谢你的支持

7 个答案:

答案 0 :(得分:14)

您可以在下面的链接中查看振动动画的工作原理吗?

1)Shaking animation in android
2)Vibration animation

我希望它会对你有所帮助。

由于

答案 1 :(得分:11)

此代码在水平方向上抖动视图

  

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" />
  

cycle_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
}

答案 2 :(得分:5)

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();

答案 3 :(得分:1)

在动画目录中创建动画文件:

<?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);

答案 4 :(得分:0)

在动画目录

中创建 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);

答案 5 :(得分:0)

这对我来说很顺利

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);
}

答案 6 :(得分:0)

我确实为imageView创建了 BUZZ 之类的动画。在这里,我还添加了延迟,使其真正像嗡嗡声效果

  1. 在您的活动中:

在这里,animShakeandroid.view.animation

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

别忘了添加AnimationListener,以免造成BUZZ效果延迟

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?) {

        }
    })
  1. 动画shake仅具有translate的XML文件:

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