我正在使用以下代码,我有一个高度为 50dp 的相对布局,我希望它为其设置动画并移动到屏幕上方。这个动画效果很好。
但是当我把它带回来时,它猛烈地震动。
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -100);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) locationView.getLayoutParams();
params.topMargin -= 100;
locationView.setLayoutParams(params);
locationView.setVisibility(View.GONE);
}
});
Bringing back code
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, +100);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
locationView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) locationView.getLayoutParams();
params.topMargin += 100;
locationView.setLayoutParams(params);
}
});
locationView.startAnimation(anim);
最佳答案
我本来期待一些不同的东西
这会将 View 顶部从 0
转换为 -100px
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -100);
和onAnimationEnd
只是将其可见性更改为GONE
,而不更改其布局参数。要将其恢复,您必须将 View 顶部从 -100px
转换为 0
TranslateAnimation anim = new TranslateAnimation(0, 0, -100, 0);
在HoneyComb之前,如果 View 不可见,动画将不会启动,并且onAnimationStart也不会被触发,所以你应该调用
locationView.setVisibility(View.VISIBLE);
之前view.startAnimation(anim);
关于java - 将 View 平移到屏幕边界之外并将其动画回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355174/