java - 动画在触摸时开始,在未触摸时停止

标签 java android animation

所以我的问题是如何制作动画在触摸时开始,一旦没有触摸它就会播放另一个动画.. (对不起我的英语..)

main.java:

public class Main extends Activity {

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

        final ImageView is = (ImageView) findViewById(R.id.img1);
        is.setBackgroundResource(R.animator.animup);
        final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
        final ImageView iv = (ImageView) findViewById(R.id.img1);
        iv.setBackgroundResource(R.animator.anim);  
        final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
        is.setOnTouchListener(new OnTouchListener() {



                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) { 
                        anim.setVisible(true, true);
                        anim.start();

                        //perform your animation when button is touched and held
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        anim.setVisible(true, true);
                        animup.start();

                        //perform your animation when button is released
                    }
                    return false;




                }
            });

    };

    ;
  }

+另一个问题...... 已编辑

最佳答案

我认为这个link会帮助你的。

编辑

要实现触摸和释放,您必须使用 onTouchListener 而不是 onClickListener

下面是代码-

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //perform your animation when button is touched and held
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          //perform your animation when button is released
        }
    }
};

编辑2

else if block 中,您没有将 anim 的可见性设置为 false。我觉得这就是问题所在。将您的代码重写为这样 -

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            anim.setVisible(true,true);
            anim.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
         anim.stop(); //perform your animation when button is released
         animup.setVisible(true,true);
         animup.start();
       }
    }
};

关于java - 动画在触摸时开始,在未触摸时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18803805/

相关文章:

java - onClick 监听器在所有情况下都会导致应用程序崩溃

java - 因 POM 无效或丢失而失败

java - Hibernate:从未使用过查询缓存

java - 为什么包装类型未装箱而不是装箱原始类型?

java - 如何获取字符之间的特定字符串?

android - 删除 SeekBar 中的顶部和底部填充

android - 在Android中测量噪音

ios - 当用户用手指拾取时的动画表格 View 单元格(Swift 3)

ios - 滚动文本 - 自定义动画

python - 使用 matplotlib 从 pcolormesh 动画四边形网格