简体   繁体   中英

I am trying to make an animation that makes an imageView move up and down using Object animator and when I hit onClick() have it float away

I using object animator and whenever I use onClick() to start another animation even after I use cancel it becomes buggy and does a combination of the two animations. Could someone show me how to make an animation that moves an imageView up and down. Then onClick() brings the imageView from the bottom of the screen to the middle then moves it up and down and restarts onClick().

I've spent a lot of time trying to figure this out. I am new to android animation. Please help.

Try something like this:

boolean isAnimating = false;

public void onClick() {
    if (!isAnimating) {
        isAnimating = true;
        imageView.animate()
                .withLayer()
                .translationY(-100)
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        imageView.animate()
                                .withLayer()
                                .translationY(100)
                                .withEndAction(new Runnable() {
                                    @Override
                                    public void run() {
                                        isAnimating = false;
                                    }
                                });
                    }
                });
    }
}

The main point here is that isAnimating is creating a boolean lock, which doesn't allow restarting the animation while it's still running. When the animation finishes, isAnimating is set to false, allowing the animation to be run again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM