首页 > 解决方案 > 动画后使 Imageview 消失

问题描述

我有一个方法可以为具有指定参数的 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 不可见时,它会在动画发生的同时运行,因此实际上什么都看不到。如果我删除这段代码,我可以很好地看到视图的动画。

我怎样才能让这个方法创建一个动画并在整个动画完成后才将其变为不可见?

标签: androidimageviewandroid-imageviewvisibility

解决方案


将此添加到您的代码之前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) {
        }
});

推荐阅读