How do I use a while loop with an onTouch function? (Android)

跟風遠走 提交于 2019-12-13 04:30:11

问题


Well, I have been looking up answers, but none have worked for me. I have a button that when it is HELD DOWN, is suppose to continue to move an image across the screen. But for some reason, it just keeps freezing my kindle when I tap the button. I think the thread is part of the problem, but i'm unsure. Here is the code:

package com.evilsea.darkages;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener, Runnable {

ImageView leftImageButton;

boolean leftButtonDown = false;

ImageView knight;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Thread thread1 = new Thread(this);
    thread1.start();
    this.run();
}

public void addListenerOnLeftButton() {

    leftImageButton = (ImageView) findViewById(R.id.left_button);
    leftImageButton.setOnTouchListener(leftButtonlistener);

    knight = (ImageView) findViewById(R.id.knight_image);

}

OnTouchListener leftButtonlistener = new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                leftButtonDown = true;
            try {
                while(leftButtonDown) {
                    Thread.sleep(10);
                    moveLeft(5);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;

            case MotionEvent.ACTION_UP:
                leftButtonDown = false;
            break;
        }

        return false;
    }
    };

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void run() {
    addListenerOnLeftButton();
}

public void moveLeft(int speed) throws InterruptedException {
    knight.setLeft(knight.getLeft() -speed);
}

}

BTW, I am just starting out w/ android, so I am sorry if this is an obvious question. Thanks! Much appreciated.


回答1:


Firstly, you are making a UI thread sleep that's why your UI is getting freeze.Here, i have created a async task where i am sleeping background thread and running a UI update on main thread as you can see below in doInBackground method.Here is your solution -:

public class MainActivity extends Activity {

    private ImageView leftImageButton;
    private boolean leftButtonDown = false;
    private ImageView knight;
    private AsyncTask asyncTask = null;

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

        leftImageButton = (ImageView) findViewById(R.id.imageView2);
        knight = (ImageView) findViewById(R.id.imageView1);
        leftImageButton.setOnTouchListener(new LeftImageListener());
    }

    public void startTask() {
        asyncTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                if (leftButtonDown) {
                    while (leftButtonDown) {
                        try {
                            Thread.sleep(10);
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        moveLeft(5);

                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

                                }
                            });

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return null;
            }

        }.execute();
    }

    public void moveLeft(int speed) throws InterruptedException {
        knight.setLeft(knight.getLeft() - speed);
    }

    public class LeftImageListener implements OnTouchListener {

        public LeftImageListener() {
            // TODO Auto-generated constructor stub
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                leftButtonDown = true;
                startTask();
                break;
            case MotionEvent.ACTION_UP:
                leftButtonDown = false;
                break;
            default:
                break;
            }

            return true;
        }

    }

}



回答2:


create a second thread was right but you should fill it with some action cause the thread you`ve created is an empty one :) and you dont need to call run after starting your thread.

Thx Abhishek Birdawade for doing the code work for me :)



来源:https://stackoverflow.com/questions/18551753/how-do-i-use-a-while-loop-with-an-ontouch-function-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!