动画后使Imageview消失(Making Imageview disappear After Animation)

编程入门 行业动态 更新时间:2024-10-08 18:39:41
动画后使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) { } });

更多推荐

本文发布于:2023-08-01 18:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   Imageview   Making   disappear   Animation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!