动画后使Imageview消失(Making Imageview disappear After Animation)
我有一个方法为具有指定参数的ImageView对象创建一个动画:
public void animateMove(float x, float y, int milsecs) { float origX = view.getX(); float origY = view.getY(); view.setVisibility(View.VISIBLE); Path linePath = new Path(); linePath.lineTo(x, y); ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", "translationY", linePath); anim.setDuration(milsecs); anim.start(); view.setVisibility(View.INVISIBLE); // this code is the problem view.setX(origX); view.setY(origY); }但是,当我调用setVisibility方法使ImageView不可见时,它会在动画发生的同时运行,因此实际上什么也看不到。 如果我删除这段代码,我可以看到视图的动画很好。
我怎样才能让这个方法创建一个动画并且只在整个动画完成后才将其变为不可见?
I have a method that creates an animation for an ImageView object with specified parameters:
public void animateMove(float x, float y, int milsecs) { float origX = view.getX(); float origY = view.getY(); view.setVisibility(View.VISIBLE); Path linePath = new Path(); linePath.lineTo(x, y); ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", "translationY", linePath); anim.setDuration(milsecs); anim.start(); view.setVisibility(View.INVISIBLE); // this code is the problem view.setX(origX); view.setY(origY); }However, when I call the setVisibility method to make the ImageView invisible, it runs at the same time as when the animation is occurring and so nothing can actually be seen. If I remove this piece of code, I can see the animation of the view just fine.
How can I have this method create an animation and turns it invisible only AFTER the whole animation is complete?
最满意答案
在anim.start()之前anim.start()添加到您的代码中:
anim.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { view.setVisibility(View.INVISIBLE); view.setX(origX); view.setY(origY); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } });Add this to your code before anim.start():
anim.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { view.setVisibility(View.INVISIBLE); view.setX(origX); view.setY(origY); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } });更多推荐
发布评论