如何为ImageView制作振动动画

Nam*_* Vu 20 animation android vibration

我不知道这个动画.

我怎么能通过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>
Run Code Online (Sandbox Code Playgroud)

谢谢你的支持

Phi*_*ert 19

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

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" />
Run Code Online (Sandbox Code Playgroud)

cycle_5.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles="5" />
Run Code Online (Sandbox Code Playgroud)

摇动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
}
Run Code Online (Sandbox Code Playgroud)


Gee*_*kur 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();
Run Code Online (Sandbox Code Playgroud)


Meh*_*nki 5

在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" />
Run Code Online (Sandbox Code Playgroud)

在你的java文件中添加下面的方法

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

并在动画方法中传递您的视图

animateView(yourView);
Run Code Online (Sandbox Code Playgroud)