1、首先在你的res目录下新建anim子目录,并在anim目录下新建两个文件:
(1)cycle.xml文件,控制循环次数
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="3" /> <!-- 循环次数 -->
(2)shake.xml文件(位移/平移:translate),设置起始的位移范围、效果时间、循环次数
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="200"
android:interpolator="@anim/cycle">
<!--.
fromXDelta:x轴起点抖动位置
toXDelta:x轴终点抖动位置
duration:循环播放的时间
interpolator:循环不放设置(次数)
-->
</translate>
2、给控件设置动画属性
Animation shake = AnimationUtils.loadAnimation(requireContext(), R.anim.shake);
errorMsg.setText(helpString); //errorMsg为TextView
errorMsg.setAnimation(shake);