2015-02-07 77 views
1

当按下按钮时,我想要发生两件事 1.两张图像应该一遍又一遍地播放,给它一个动画和静态的外观。 2.循环中的声音。如何在未按下按钮时停止动画

当按钮未按下且手指向上时 1.声音应停止 2.动画应停止。

我用现在的方法显示在代码:

package com.example.newanimation; 

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

public class MainActivity extends Activity { 

    AnimationDrawable rocketAnimation; 

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

     ImageView rocketImage = (ImageView) findViewById(R.id.imageView1); 
     rocketImage.setBackgroundResource(R.drawable.ball_animation); 
     rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
    } 

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

} 

和XML就像

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/ball01" android:duration="200" /> 
    <item android:drawable="@drawable/ball02" android:duration="200" /> 
    <item android:drawable="@drawable/ball03" android:duration="200" /> 
</animation-list> 

我保持的android:单稳= “假”>而非“真正的“,因为我希望动画不停地播放,直到按钮被按下,但是当手指从按钮向上时,我需要它停止。

回答

1

onTouchEvent()中,检查MotionEvent.ACTION_UP以知道该按钮已被取消按下。

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
    if(rocketAnimation.isRunning()){ 
     rocketAnimation.stop(); 
    } 
    } 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    if(!rocketAnimation.isRunning()){ 
     rocketAnimation.start(); 
    } 
    } 
    return super.onTouchEvent(event); 
} 
+0

的方法取消();未定义为动画可绘制:/ – 2015-02-07 07:18:26

+0

将其更改为'stop()':D – Hemanth 2015-02-07 07:21:55

+0

那里完成了:/ – 2015-02-07 07:23:07

0

以下代码是针对您的问题的完美解决方案。

public class MainActivity extends Activity { 

    AnimationDrawable rocketAnimation; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageView rocketImage = (ImageView) findViewById(R.id.imageView1); 
     Button button = (Button) findViewById(R.id.button); 
     rocketImage.setBackgroundResource(R.drawable.roket); 
     rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 


     button.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        rocketAnimation.start(); 
        return true; 
       } else if (event.getAction() == MotionEvent.ACTION_UP) { 
        rocketAnimation.stop(); 
        return false; 
       } else 
        return false; 
      } 
     }); 
    } 
} 

在视频检查演示 https://www.youtube.com/watch?v=shQ8GOfeJew&feature=youtu.be

相关问题