【问题标题】:How to Stop animation when button is unpressed未按下按钮时如何停止动画
【发布时间】:2015-02-08 08:40:37
【问题描述】:

我希望在按下按钮时发生两件事 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:oneshot="false"> 而不是 "true" 因为我希望动画来回播放而不会停止直到按下按钮,但我需要它在手指从按钮。

【问题讨论】:

    标签: java android xml animation


    【解决方案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);
    }
    

    【讨论】:

    • 方法cancel();动画 Drawable 未定义:/
    • 把它改成stop():D
    • 兄弟你能告诉我在哪里可以获得安卓应用程序的源代码吗?
    【解决方案2】:

    下面的代码是您问题的完美解决方案。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      相关资源
      最近更新 更多