【问题标题】:How to continuously move object in Android Studio如何在 Android Studio 中连续移动对象
【发布时间】:2020-06-20 17:33:58
【问题描述】:

当一个Button被按住时如何在Android Studio中连续移动一个ImageView,但当它不再被点击时停止?换句话说:如何检测按钮是否“未点击”或直接检测它是否被按住。感谢您的帮助

【问题讨论】:

标签: java android button imageview


【解决方案1】:

为了检测按钮是否被按下/释放(向下/向上),使用了 TouchListener。

    imageView = findViewById(R.id.image);
    button = findViewById(R.id.button);
    root_layout = findViewById(R.id.root_layout); //parent layout

    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            float x = imageView.getX();
            if(x > root_layout.getWidth())
                x = 0;
            else
                x += 6; //Increase value of '6' to move the imageView faster
            imageView.setX(x);

            handler.postDelayed(this,0); //increase delay '0' if facing lag. 
            // This is the rate at which the x value of our imageView is being updated
            
        }
    };

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    handler.post(runnable); //Start moving imageView
                    break;
                case MotionEvent.ACTION_UP:
                    handler.removeCallbacks(runnable); //Stop moving imageView
                    break;
            }
        return true;
        }
    });

【讨论】:

  • 当我创建一个处理程序时,它说我需要实现一些方法。我该怎么办?
  • 您正在导入“java.util.logging.Handler”。而是导入'android.os.Handler'
【解决方案2】:

试试这个解决方案

第 1 步:- 需要在 res 文件夹中定义 anim 文件夹。

现在将 rotate.xml 文件放在 anim 文件夹中。

rotate.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>

第 2 步:- 以编程方式声明动画

// Animation
    Animation animFadein;

in onCreate()
// load the animation
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);

第 3 步:- 像这样检测用户触摸以启动和停止动画

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //start animation here
           imageView.startAnimation(animFadein);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // stop animation here
             imageView.stopAnimation(animFadein);
        }
        return true;
    }
});

【讨论】:

    猜你喜欢
    • 2011-07-26
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2016-08-22
    • 2017-02-09
    相关资源
    最近更新 更多