【发布时间】:2018-05-13 18:50:57
【问题描述】:
我尝试使用动画将图像从一个位置移动到另一个位置,但是在将其移回原始位置后,如何将其自身停止到移动的位置。
【问题讨论】:
标签: android
我尝试使用动画将图像从一个位置移动到另一个位置,但是在将其移回原始位置后,如何将其自身停止到移动的位置。
【问题讨论】:
标签: android
要让图像在动画的最后一个位置,试试这个:
TranslationAnimation ta = new TranslateAnimation(fromX, toX, 0, 0);
ta.setDuration(1000);
ta.setFillAfter(true); // this will let the image in the last place of the Animation
imageView.startAnimation(ta);
【讨论】:
动画完成后,使用setFillAfter(true)方法,让最后的动画状态保持:
如果 fillAfter 为 true,则此动画执行的转换将在完成后持续存在。
Animation.setFillAfter/Before - Do they work/What are they for?
如果你需要做一些更具体的事情,你也可以设置一个动画监听器并在动画结束时移动你的对象:
animation1.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//change according to your needs
myView.setX(0);
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
【讨论】:
终于有办法变通了,正确的方法是setFillAfter(true),
如果你想在 xml 中定义你的动画,那么你应该这样做
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
你可以看到我已经在 set 标记中定义了 filterAfter="true",如果你尝试的话
在翻译标签中定义它不起作用,可能是框架中的错误!
然后在代码中
Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
someView.startAnimation(anim);
【讨论】:
见this blog post for a solution:
// first set the view's location to the end position
view.setLayoutParams(...); // set to (x, y)
// then animate the view translating from (0, 0)
TranslationAnimation ta = new TranslateAnimation(-x, -y, 0, 0);
ta.setDuration(1000);
view.startAnimation(ta);
【讨论】:
我相信您现在已经找到了答案(我刚刚找到了……所以我为其他人发帖)。 Android 似乎已经正式转向一个名为“Property Animation”的新动画框架。这从 Honeycomb (3.0) 开始可用。这将从根本上解决您的问题,因为它会为实际的属性值设置动画。
开发指南: http://developer.android.com/guide/topics/graphics/prop-animation.html
【讨论】: