简体   繁体   English

动画代码在第一个动画完成之前开始

[英]Animation code starts before first animation is complete

I've tried setting up a simple coin flip animation for a coin flip activity.我尝试为抛硬币活动设置简单的抛硬币动画。 Therefore, I created two drawables (coin_heads and coin_tails) and set up this piece of code.因此,我创建了两个可绘制对象(coin_heads 和 coin_tails)并设置了这段代码。 I'm creating a random value and afterwards I'm having a simple if-statement to deceide whether heads or tails should appear.我正在创建一个随机值,然后我有一个简单的 if 语句来决定是否应该出现正面或反面。 Depending on that the drawable will be replaced.取决于此,drawable 将被替换。 I already got to know that whenever you replace the drawable with yourobject.setImageDrawable(ContextCompat.getDrawable(this,drawable));我已经知道每当你用yourobject.setImageDrawable(ContextCompat.getDrawable(this,drawable));替换 drawable 时yourobject.setImageDrawable(ContextCompat.getDrawable(this,drawable)); its rotation, scale etc. are being reseted (that's why I want to set the rotation and alpha to the values I animated it to before).它的旋转、缩放等正在被重置(这就是为什么我想将旋转和 alpha 设置为我之前为其设置动画的值)。

I thought it'd be easier to use the animate() method instead of XML-arguments, that's why I have a problem right now.我认为使用 animate() 方法而不是 XML 参数会更容易,这就是我现在遇到问题的原因。 Whenever I try to run this, it skips my first lines of每当我尝试运行它时,它都会跳过我的第一行

cimg.setAlpha(1f);
        cimg.animate().alphaBy(-1f).setDuration(800);

So, to describe what happens: As soon as I press my button to flip the coin, the Image's alpha is being set to 0 immediately and then animated backwards to 1 in time of 800ms.因此,为了描述会发生什么:只要我按下按钮翻转硬币,图像的 alpha 就会立即设置为 0,然后在 800 毫秒的时间内将动画向后设置为 1。 But I want it to animate to Alpha=0 in 800ms first and then animate it to 1 in 800ms again.但我希望它首先在 800 毫秒内将其动画化为 Alpha=0,然后再在 800 毫秒内将其动画化为 1。

Here is the code of the flip method (I've commented out a lot while debugging):这里是flip方法的代码(我在调试的时候注释掉了很多):

public void flip(View view) {
        TextView coin = (TextView) findViewById(R.id.coinField);
        ImageView cimg = (ImageView) findViewById(R.id.coinimage);


        double flip1 = Math.random();

        //start of animation
        //cimg.animate().scaleXBy(4f).setDuration(1000);
        //cimg.animate().scaleYBy(4f).setDuration(1000);
        //cimg.animate().rotationBy(180f).setDuration(1000);

//first animation
        cimg.setAlpha(1f);
        cimg.animate().alphaBy(-1f).setDuration(800);

//final animation

        if (flip1>=0.5) {
            coin.setText(R.string.heads);
            cimg.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.coin_heads));
            //cimg.setRotation(180f);
            cimg.setAlpha(0f);
            cimg.animate().alpha(1f).setDuration(800);


            //cimg.animate().rotationBy(180f).setDuration(1000);
            //cimg.animate().scaleXBy(0.25f).setDuration(1000);
            //cimg.animate().scaleYBy(0.25f).setDuration(1000);
        }
        else {
            coin.setText(R.string.tails);
            cimg.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.coin_tails));
            //cimg.setRotation(180f);
            cimg.setAlpha(0f);
            cimg.animate().alpha(1f).setDuration(800);
            //cimg.animate().rotationBy(180f).setDuration(1000);
            //cimg.animate().scaleXBy(0.25f).setDuration(1000);
            //cimg.animate().scaleYBy(0.25f).setDuration(1000);
        }


    }

I would be very happy if anybody has a solution for this as I have been struggling with this for hours now.如果有人对此有解决方案,我会非常高兴,因为我已经为此苦苦挣扎了几个小时。 I also tried to use Thread.sleep(1000) to ensure the animation is completed, but when doing so, it still skips the first animation, waits 1 second, and then does it's thing as usual.我也尝试使用Thread.sleep(1000)来确保动画完成,但是这样做时,它仍然跳过第一个动画,等待 1 秒,然后照常执行。

Edit:编辑:

I've set it up like this.我是这样设置的。 Now, when I press my button, nothing happens:现在,当我按下按钮时,什么也没有发生:

    public void flip(View view) {
        final TextView coin = (TextView) findViewById(R.id.coinField);
        final ImageView cimg = (ImageView) findViewById(R.id.coinimage);

        final double flip1 = Math.random();

        Animation anim = new Animation() {
            @Override
            protected Animation clone() throws CloneNotSupportedException {
                cimg.setAlpha(1f);
                cimg.animate().alpha(0f).setDuration(800);
                return super.clone();
            }
        };


        final Animation anim2 = new Animation() {
            @Override
            protected Animation clone() throws CloneNotSupportedException {
                if (flip1>=0.5) {
                    coin.setText(R.string.heads);
                    cimg.setImageDrawable(getDrawable(R.drawable.coin_heads));
                    cimg.setAlpha(0f);
                    cimg.animate().alpha(1f).setDuration(800);

                }
                else {
                    coin.setText(R.string.tails);
                    cimg.setImageDrawable(getDrawable(R.drawable.coin_tails));
                    //cimg.setRotation(180f);
                    cimg.setAlpha(0f);
                    cimg.animate().alpha(1f).setDuration(800);
                }
                return super.clone();
            }
        };

        anim2.setAnimationListener(null);


        Animation.AnimationListener list = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                anim2.start();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }



        };

        anim.setAnimationListener(list);
    }

Use an animation listener to start your second animation after the first one ends:使用动画侦听器在第一个动画结束后开始第二个动画:

    final Animation anim = new SomeTypeOfAnimation();
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
             //Start your other animation here...
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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