//方式1,加载动画文件
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
//方式2,代码方式
//也可以2者
// TranslateAnimation animation = new TranslateAnimation(0, getWidth() * 2-100,
// 0, getHeight() * 2-100);
animation.setDuration(2000);
animation.setRepeatCount(1);
animation.setFillAfter(true);
animation.setFillEnabled(true); //这个地方必须代码设置才不会返回到原地
// translateAnimation.setFillBefore(false);
animation.setRepeatMode(Animation.REVERSE);
animation.setFillAfter(true);
animation.setFillEnabled(true);
这个地方必须代码设置,动画结束后才不会返回到原地
view_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:shareInterpolator="true">
<translate
android:duration="2000"
android:fillAfter="true"
android:fillEnabled="true"
android:fillBefore="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="400"/>
</set>
这种方式有个弊端,view动画结束看上去好像是移位了,但是点击事件的时候,触发的点击事件还是在view原来的位置,动画后的位置不起作用
网友评论