Android的 - 借鉴线程线程、Android

2023-09-06 08:44:51 作者:你二的狠有范儿゜

我想画的东西repetitivelely每100毫秒或者当一个按钮pressed。对于按钮onclick事件它的作品的权利,但我不能让它在一个线程工作。以下是code:

的onclick

 按钮=(按钮)findViewById(R.id.button);    button.setOnClickListener(新View.OnClickListener(){        @覆盖        公共无效的onClick(查看视图){            绘制();        }    }); 

主题

 专用处理器处理器=新的处理程序();私人Runnable接口可运行=新的Runnable(){    公共无效的run()    {        绘制();        handler.postDelayed(这一点,1000);    }}; 

Draw方法

 私人无效抽奖(){    涂料粉刷=新的油漆();    INT I;    帆布帆布= holder.lockCanvas();    // Dibujo埃尔丰多    paint.setColor(getResources()的getColor(android.R.color.black));    canvas.drawRect(0,0,canvas.getWidth(),canvas.getHeight(),漆);    holder.unlockCanvasAndPost(画布);} 
Android的线程和线程池

解决方案

这是因为UI线程运行分开你的的Runnable 线程。像这样单独的线程不能互动。您将需要使用的AsyncTask 线程这将使你定期访问UI线程。

例如:

 私有类ExampleThread扩展的AsyncTask<太虚,太虚,太虚> {    @覆盖    在preExecute保护无效(){}    @覆盖    保护无效doInBackground(虚空...... PARAMS){         而(!isCancelled()){//保持下去,直到取消             尝试{                 视频下载(100); //延时100毫秒             }赶上(InterruptedException的E){                 Thread.interrupted();             }             publishProgress(); //运行onProgressUpdate()方法             如果(isCancelled())破; //早逃生,如果取消()被调用         }    }    @覆盖    保护无效onPostExecute(虚空...... PARAMS){}    @覆盖    保护无效onProgressUpdate(虚空...... PARAMS){        //在这里,您可以访问UI线程        绘制();    }} 

要启动线程:

  ExampleThread线程=新ExampleThread();thread.execute(); 

要停止线程:

  thread.cancel(); 

更多信息,并与AsyncTask的例子可以在发现这个问题。

I want to draw something repetitivelely each 100 milliseconds or when a button is pressed. For the button onclick event it works right, but I can't make it work in a thread. Following is the code:

onclick

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Draw();
        }
    });

thread

private Handler handler = new Handler();

private Runnable runnable = new Runnable()
{

    public void run()
    {
        Draw();
        handler.postDelayed(this, 1000);
    }
};

Draw method

private void Draw(){

    Paint paint = new Paint();
    int i;

    Canvas canvas = holder.lockCanvas();

    // Dibujo el fondo
    paint.setColor(getResources().getColor(android.R.color.black));
    canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

    holder.unlockCanvasAndPost(canvas);
}

解决方案

This is because the UI thread runs separately to your Runnable thread. Separate threads like this cannot interact. You will need to use an AsyncTask thread which will enable you to periodically access the UI thread.

For example:

private class ExampleThread extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {}

    @Override
    protected void doInBackground(Void... params) {
         while(!isCancelled()) { // Keep going until cancelled
             try {
                 Thread.sleep(100); // Delay 100 milliseconds
             } catch (InterruptedException e) {
                 Thread.interrupted();
             }
             publishProgress(); // Run onProgressUpdate() method
             if(isCancelled()) break; // Escape early if cancel() is called
         }

    }

    @Override
    protected void onPostExecute(Void... params) {}

    @Override
    protected void onProgressUpdate(Void... params) {
        // Here you can access the UI thread
        Draw();
    }
}

To start the thread:

ExampleThread thread = new ExampleThread();
thread.execute();

To stop the thread:

thread.cancel();

More information and examples with AsyncTask can be found on this question.