我的TranslateAnimation有效,但在动画结束时,它会跳回到原始位置。
LinearLayout rl = (LinearLayout) findViewById(R.id.navPanel);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 3.0f
);
animation.setDuration(500);
rl.startAnimation(animation);
如何在终点位置保持?
答案 0 :(得分:8)
旧的(3.0之前版本)动画API表现得有些奇怪 - 当它动画时,它只是转换绘制视图的位置,但实际上并不会相对于父视图移动视图。
为了在动画完成后让视图保持不变,您可以在Animation.AnimationListener
上设置TranslateAnimation
。在侦听器的onAnimationEnd()
方法中,您可以通过操纵视图上的LayoutParams
将视图移动到最终的静止位置。
答案 1 :(得分:8)
使用animation.setFillAfter(true)
让动画在翻译结束时停止播放。
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
答案 2 :(得分:3)
同样的问题https://stackoverflow.com/a/6519233/1253065
使用 animation.setFillAfter(true),animation.setFillEnabled(true)
答案 3 :(得分:1)
为避免重复代码,您可以继承AnimatorListenerAdapter并创建自己的AnimatorListener类,在动画后重新附加视图,然后在需要的地方使用它而不是默认的AnimatorListener。
班级:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
public class PersistViewAnimatorListener extends AnimatorListenerAdapter {
private View view; // keep a reference to the view
public PersistViewAnimatorListener(View view) {
this.view = view;
}
@Override public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// re-attach the view to its parent at its new location
if (view == null) { return; } // defensive, the view may have been removed while it was animated, do nothing in this case
ViewGroup parent = (ViewGroup) view.getParent(); // get the parent
if (parent == null) { return; } // defensive
int index = parent.indexOfChild(view); // get the index of the view
parent.removeView(view); // remove the view
if (parent.getClass().equals(RelativeLayout.class)) { // RELATIVE LAYOUT
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(view.getLayoutParams()); // add the view back at its index with new layout parameters
layoutParams.setMargins(Math.round(view.getX()), Math.round(view.getY()), 0, 0); // left, top, right, bottom; round the float to int
parent.addView(view, index, layoutParams);
} else if (parent.getClass().equals(FrameLayout.class)) { // FRAME LAYOUT
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(view.getLayoutParams()); // the same for FrameLayout
layoutParams.setMargins(Math.round(view.getX()), Math.round(view.getY()), 0, 0);
parent.addView(view, index, layoutParams);
} else { // currently no further layouts supported, implement these here
throw new UnsupportedOperationException("PersistViewAnimatorListener is currently only supported where the parent is either RelativeLayout or FrameLayout.");
}
view.setTranslationX(0); view.setTranslationY(0); // reset translated values from the animation
}
}
以及如何使用它:
view.animate().yBy(height).setListener(new PersistViewAnimatorListener(view)); // call your custom listener in .setListener() and pass in the view
此解决方案假设您正在为父级使用RelativeLayout或FrameLayout。此外,它只关心位置,而不是大小或其他可以动画的属性。