Android动画-视图不刷新
这可能是一个非常简单的问题,我有一个动态添加的片段。该片段有一个带有单击事件的按钮,该按钮可设置视图在x轴上+50和y轴上+50的动画 动画工作正常,但问题是当我在动画后单击按钮时,它不工作,但单击事件仍保留在原始位置。如果这没有意义,我将发布一个链接到youtube视频,显示正在发生的事情。我只是想知道我是否有什么需要刷新的 这是我的密码:Android动画-视图不刷新,android,animation,Android,Animation,这可能是一个非常简单的问题,我有一个动态添加的片段。该片段有一个带有单击事件的按钮,该按钮可设置视图在x轴上+50和y轴上+50的动画 动画工作正常,但问题是当我在动画后单击按钮时,它不工作,但单击事件仍保留在原始位置。如果这没有意义,我将发布一个链接到youtube视频,显示正在发生的事情。我只是想知道我是否有什么需要刷新的 这是我的密码: public class AnimationFragment extends Fragment { public AnimationFragme
public class AnimationFragment extends Fragment {
public AnimationFragment() {
// Required empty public constructor
}
View view;
float curX, curY, newX, newY;
boolean animating = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
curX = 0; curY = 0;
view = inflater.inflate(R.layout.fragment_animation, container, false);
// get the button and add the onclick event
Button animateButton = (Button)view.findViewById(R.id.animateButton);
//
if (null != animateButton) {
animateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateButtonClick(v);
}
});
}
return view;
}
public void animateButtonClick(View view) {
// wait for the animation to be complete
if (animating) return;
animating = true;
// get the new x and new y
newX = curX + 50.0f;
newY = curY + 50.0f;
// get the layout that will be animated
final RelativeLayout animationFragment = (RelativeLayout)getView().findViewById(R.id.animationFragment);
Animation slide = new TranslateAnimation(
Animation.ABSOLUTE,curX,
Animation.ABSOLUTE, newX,
Animation.ABSOLUTE, curY,
Animation.ABSOLUTE, newY);
// add other animation attributes
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
// start the animation
animationFragment.startAnimation(slide);
slide.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// nothing
}
@Override
public void onAnimationEnd(Animation animation) {
// make the view enable
animating = false;
// sets the new x and new y
curX = newX;
curY = newY;
}
@Override
public void onAnimationRepeat(Animation animation) {
// nothing
}
});
}
}
尝试不使用:
slide.setFillAfter(true);
slide.setFillEnabled(true);
从文档中,setFillEnabled
如果fillEnabled为true,则动画将应用
菲尔比
塞菲尔弗
如果fillAfter为true,则表示此动画执行的变换
当它完成时将持续
首先尝试不使用,如果它有效并且希望在之前发生,请删除这两个
setFillAfter()/setFillEnabled()
方法。如果希望它发生在之后,请仅使用setFillAfter(true)
当我清除这些事件时,动画将无法工作,除非我在onAnimationEnd事件中设置view animationFragments x和y。这很有意义,因为它不是为视图设置动画,而是视图的位图。但奇怪的是它看起来还是垃圾<代码>animationFragment.setX(newX);animationFragment.setY(newY)代码>您已将问题标记为已解决,但您之前的评论表示问题未解决-我感到困惑。看看TranslateAnimation的参数,似乎你用错了: