我该如何让动画开始接触和停止再次感动? [英] How do I make animation start on touch and stop once touched again?

查看:120
本文介绍了我该如何让动画开始接触和停止再次感动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用我做的一切工作,但我想添加一个code,将停止动画,一旦我再次触摸屏幕。正如你可以在code看到我的动画开始播放,但我只能通过退出程序停止它,我想能够触摸屏第二次停止动画和声音的播放。

感谢你。

 进口android.app.Activity;
进口android.os.Bundle;
进口android.graphics.drawable.AnimationDrawable;
进口android.media.MediaPlayer;
进口android.view.MotionEvent;
进口android.widget.ImageView;公共类WigleActivity延伸活动{
/ **当第一次创建活动调用。 * /MediaPlayer的熔点;
AnimationDrawable动画;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    MP = MediaPlayer.create(这一点,R.raw.sexy);    ImageView的imageView1 =(ImageView的)findViewById(R.id.imageView1);
    imageView1.setBackgroundResource(R.anim.wanim);
    动画=(AnimationDrawable)imageView1.getBackground();
}保护无效的onPause(){
    super.onPause();    mp.release();
    完();
}公共布尔onTouchEvent(MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_DOWN){
    mp.start();
    animation.start();
    返回true;}
返回super.onTouchEvent(事件);
}}


解决方案

更改您onTouch code以下内容:

 公共布尔onTouchEvent(MotionEvent事件){
  如果(event.getAction()== MotionEvent.ACTION_DOWN){
    如果(animation.isRunning()){
       mp.stop();
       animation.stop();
    }其他{
       mp.start();
       animation.start();
    }
    返回true;  }
  返回super.onTouchEvent(事件);
}

I have an Android app I made and everything works but I want to add a code that would stop the animation once I touch the screen again. As you can see in the code I got the animation to start but i can only stop it by exiting the program, I would like to be able to touch the screen a second time to stop the animation and the sound playback.

Thank You.

import android.app.Activity;
import android.os.Bundle;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.view.MotionEvent;
import android.widget.ImageView;



public class WigleActivity extends Activity {
/** Called when the activity is first created. */

MediaPlayer mp;
AnimationDrawable animation;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.sexy);

    ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
    imageView1.setBackgroundResource(R.anim.wanim);
    animation = (AnimationDrawable) imageView1.getBackground();       
}

protected void onPause() {
    super.onPause();

    mp.release();
    finish();
}

public boolean onTouchEvent (MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
    mp.start();
    animation.start();
    return true;

}
return super.onTouchEvent(event);
}

}

解决方案

Change you onTouch code to the following:

public boolean onTouchEvent (MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    if (animation.isRunning()) {
       mp.stop();
       animation.stop();
    } else {
       mp.start();
       animation.start();
    }
    return true;

  }
  return super.onTouchEvent(event);
}

这篇关于我该如何让动画开始接触和停止再次感动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆