android实现view震动效果

要实现view的震动效果,只需要对需要的view添加动画即可。步骤如下:

在res下创建 anim 文件夹。并创建两个文件。如下:

shake.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:fromXDelta="0"
    android:toXDelta="10"
    android:interpolator="@anim/cycle_7"/>

cycle_7.xml:

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

然后就是使用了。在所需位置。对目标view设置如下:

val shake = AnimationUtils.loadAnimation(this, R.anim.shake);
tv.startAnimation(shake);

END

猜你喜欢

转载自blog.csdn.net/lixinxiaos/article/details/128459184