1

我有问题。单击“ImageView”后,我想制作可绘制的动画。我找到了我的问题的答案,但我没有找到。

我的代码如下所示:

import java.util.Locale;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class Talking extends Activity implements TextToSpeech.OnInitListener  {

        private TextToSpeech tts;
        private Button button1;
        private EditText editText1;
        private AnimationDrawable penguinAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.talking);

        tts = new TextToSpeech(this, this);
        button1 = (Button) findViewById(R.id.button1);
        editText1 = (EditText) findViewById(R.id.editText1);

        // button on click event
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });

        ImageView penguin = (ImageView) findViewById(R.id.imageView1);
        penguin.setBackgroundResource(R.drawable.penguin_anim);
        penguinAnimation = (AnimationDrawable) penguin.getBackground();



    }
    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }


    protected void speakOut() {
        // TODO Auto-generated method stub
        String text = editText1.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

    }

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub


        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                button1.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }
    public boolean onTouchEvent(MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_DOWN) {
            penguinAnimation.start();
            return true;
          }
          return super.onTouchEvent(event);
        }

}

我使用这个http://developer.android.com/guide/topics/graphics/drawable-animation.html制作了可绘制动画

4

2 回答 2

1

您应该调用penguinAnimation.start()inspeakOut()方法来启动应用程序中的动画。

于 2013-04-01T09:31:49.323 回答
1

添加

 penguinAnimation.start()

 speakOut() 

方法,从ImageView ClickListener.

于 2013-04-01T09:34:40.977 回答