0

Eh, I want to show my text after the animation has played. In this code, it executes both the animation and the text at the same time, so how do I wait till after the animation has played all the way through to show the text? Or how do I hide the text until the animation has played?

package com.example.magic8ballers;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Magic8Ballers extends Activity {
    MediaPlayer song; //song reference
    TextView answer; // TextView reference
    Button button; // Button reference
    Magic8BallersCode m8b; // M8B reference
    // Animation object
    Animation an;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magic8_ballers);

        // associate answer with TextView object with id textView1
        answer = (TextView) findViewById(R.id.textView1);
        // associate button with Button object with id button
        button = (Button) findViewById(R.id.button1);
        button.setText("Shake It!");
        //Create new Magic8Ball object
        m8b = new Magic8BallersCode();
        song = MediaPlayer.create(Magic8Ballers.this, R.raw.magic);


        //Shake it button animation
        Button button = (Button) findViewById(R.id.button1);//reference to the        
        button that shakes it and provides prediction
        final ImageView imgView =
                (ImageView) findViewById(R.id.imageView1);//reference to the animation image

        //Music Buttons                                                     
        Button btnSound = (Button) findViewById(R.id.button2);//reference to the 
        button that turns on the song
        Button btnSound2 = (Button) findViewById(R.id.button3);//reference to the     
        button that turns off the song

        //Animation
        final Animation an = AnimationUtils.loadAnimation(Magic8Ballers.this,
                R.anim.animation);

        //Button for animation
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                imgView.startAnimation(an);
                String prediction = m8b.getAnswer();
                // Set the label's text with the new prediction
                answer.setText(prediction);
            }
        });
    }
}
4

2 に答える 2

0

アニメーションリスナーを追加する必要があります。

an.setAnimationListener(new Animation.AnimationListener() {

            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            public void onAnimationEnd(Animation animation) {


                // ADD YOU TEXT HERE
            }
        });

アニメーションの再生が終了した後、リスナーはテキストを追加します。

于 2012-12-27T01:16:55.557 に答える
0

で試すことができますsetAnimationListener method。「アニメーション リスナーは、アニメーションから通知を受け取ります。通知は、アニメーションの終了や繰り返しなど、アニメーション関連のイベントを示します。」-- 「developer.android.com」。

  • 例:

     an.setAnimationListener( new AnimationListener()
    
    { 
    
        public void onAnimationStart(Aninmation a){
             //Notifies the start of the animation.
          }
    
        public void onAnimationRepeat(Animation a){
            //Notifies the repetition of the animation.
           }
    
        public void onAnimationEnd(Animation a){
            //Notifies the end of the animation.
            //show your text here
        }
    
    });
    
于 2012-12-27T01:22:03.733 に答える