线程在Android的UI更新

线程在Android的UI更新

本文介绍了线程在Android的UI更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始与Android的开发和更新UI真的窃听我:/

I've just started with android development and updating the UI is really bugging me :/

这是我所得到的工作,到目前为止 -

This is what I've got working so far -



package projects.Move;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Color;

public class Move extends Activity {

    private float y = 0;
    private long now = 0;
    private float delay = 75;
    private Paint paint = new Paint();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SimpleMotion(this));
        paint.setColor(Color.BLACK);
    }
    private class SimpleMotion extends View {

        public SimpleMotion(Context context) {
            super(context);
        }

        @Override protected void onDraw(Canvas canvas) {

            float x = canvas.getWidth() / 2;
            canvas.drawColor(Color.WHITE);
            canvas.drawCircle(x, y, 30, paint);
            if(System.currentTimeMillis() - now > delay) y++;
            invalidate();
        }
    }
}

它工作正常,但每个人都表示,做你的图形在主线程,所以我想(和失败),以将其传递给另一个线程。麻烦的是,我完全不知道如何,因为我真的从来没有使用线程。

It works fine but everybody says that doing your graphics in the main thread, so I'm trying (and failing) to pass it off to another thread. Trouble is, I have absolutely no idea how since really I've never used Threads.

这是谷歌提供使用主题似乎没有例子是非常清楚的,我不能真正按照它为我想做的事情。请问有人在这里给我的我怎么能做到什么,我在这里做有效地使用线程最基本的例子吗?

The examples that Google gives on using Threads doesn't seem to be very clear and I couldn't really follow it for what I want to do. Could I ask somebody out here to give me the most basic example of how I could do what I'm doing here efficiently using Threads?

在此先感谢:)

推荐答案

嗯,我想有一些混乱怎么回事。你必须从主线程做你的图形用户界面的更新(也称为GUI线程) - 否则,你也得到类似异常,布拉布拉已经泄露视图

Well, I guess there is some confusion going on here. You HAVE TO do your GUI updates from the main thread (also called the GUI thread) - otherwise you well get something like "Exception, blabla has leaked a view".

我猜有误解的是昂贵的操作,如网络,应在不同的线程比主线程来完成。而如果你想更新从网线的图形用户界面,你会做的ArtWorkAD说(或他联系说的)。

I guess what have misunderstood is that expensive operations, such as networking, should be done in a different thread than the main thread. And if you would like to update the GUI from the network thread you would do as ArtWorkAD says (or his links says).

所以,你想要做什么,你可以用类似与以下替换您SimpleMotion类实现的:

So for what you want to do, you could achieve with something like replacing your SimpleMotion class with the following:

private class SimpleMotion extends View {

        public SimpleMotion(Context context) {
            super(context);

            new Thread(new Runnable() {
                public void run() {
                    while(true){
                        try {
                            Thread.sleep(75);
                            y++;
                            postInvalidate();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
              }).start();
        }

        @Override protected void onDraw(Canvas canvas) {

            float x = canvas.getWidth() / 2;
            canvas.drawColor(Color.WHITE);
            canvas.drawCircle(x, y, 30, paint);
//            if(System.currentTimeMillis() - now > delay) y++;
//            invalidate();
        }
    }

使用具有的OnDraw 无效()()的旧code 你会continously被重绘即使没有改变它的图形用户界面。

With your old code of having invalidate() in onDraw() you would continously be redrawing the gui even while there are no change to it.

新的code中的重要组成部分,是 postInvalidate()。这样就可以告诉GUI线程 - 从另一个线程 - 重绘GUI

The important part of the new code is postInvalidate(). This makes it possible to tell the GUI thread - from another thread - to redraw the GUI.

这篇关于线程在Android的UI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:44