如何在onTouch函数中使用while循环? (机器人)

时间:2013-08-31 19:01:50

标签: java android while-loop thread-sleep kindle

好吧,我一直在查找答案,但没有一个对我有用。我有一个按钮,当它是HELD DOWN时,假设继续在屏幕上移动图像。但出于某种原因,当我点击按钮时它只会让我的点燃。我认为线程是问题的一部分,但我不确定。这是代码:

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);
}

}
顺便说一句,我刚刚开始使用android,所以如果这是一个明显的问题,我很抱歉。谢谢!非常感谢。

2 个答案:

答案 0 :(得分:2)

首先,你正在进行UI线程休眠,这就是你的UI被冻结的原因。在这里,我创建了一个异步任务,我正在睡觉后台线程并在主线程上运行UI更新,如下面doInBackground方法中所示。这是你的解决方案 - :

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;
        }

    }

}

答案 1 :(得分:0)

创建第二个线程是对的,但你应该用一些动作填充它,因为你创建的线程是空的:) 并且你不需要在启动线程后调用run。

Thx Abhishek Birdawade为我做代码工作:)