3

Let me start by saying that the answers I found were about the visibility being set to "gone" instead of "invisible" in the XML file. Unfortunately, it doesn't work for me. I didn't find another solution this the problem, and as I am new to this it might be a very stupid mistake I am not seeing.

This is the java method I call to start the animation:

public void animateScoreAddition(int value){
    labelAddition.setText("+" + value);
    Animation slide = AnimationUtils.loadAnimation(game, R.anim.anim_slide);

    slide.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            labelAddition.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            labelAddition.setVisibility(View.INVISIBLE);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);

            marginTop = (int) ConversionDpPx.convertDpToPixel(10, game);
            int marginRight = (int) ConversionDpPx.convertDpToPixel(10, game);
            lp.setMargins(0, marginTop, marginRight, 0);
            labelAddition.setLayoutParams(lp);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    labelAddition.startAnimation(slide);

}

I made a thread for the game loop, and in the thread call the runOnUiThread() method to avoid exceptions when redrawing the board. This happens in another class I called GameController, GameCanvas being class handling the view:

    game.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    game.getGameCanvas().updateScores();

                    if(previousScore < score){
                        game.getGameCanvas().animateScoreAddition(score-previousScore);
                        previousScore = score;
                    }
                    if(animationNeeded) {
                        game.getGameCanvas().invalidate();
                        soundPool.play(soundMerge, 1, 1, 0, 0, 1);

                        try {
                            Thread.sleep(150);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    cancelAnimations();
                    game.getGameCanvas().invalidate();

                }
            });

This is the animation XML file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:shareInterpolator="true">
<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="600"
    android:repeatCount="0"
    android:startOffset="0" />
<translate
    android:duration="500"
    android:fromXDelta="72%p"
    android:toXDelta="72%p"
    android:fromYDelta="-10"
    android:toYDelta="-35"
    android:startOffset="0" />
</set>

And this is the TextView defined in the main XML file:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+0"
    android:id="@+id/labelAddition"
    android:textColor="#F67C5F"
    android:visibility="invisible"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_toStartOf="@+id/score" />

As you can see, the visibility is set to "invisible" instead of "gone". You might think the problem comes from the view being set to "visible" in the listener, but even when I do it before calling the startAinmation() function, it gives exactly the same result.

Also worth mentioning that I used a Log.d(...) message to make sure the method is called the first time, and surely enough it is. It goes in the listener as expected. It just doesn't do anything.

zuokuok
  • 293
  • 4
  • 16

0 Answers0