前言
众所周知,为了不ANR,不可以在UI线程上执行耗时的操作。所以为了效率,也为了通用,我把计算绘制参数的操作放到工作线程中去了。参考了这篇文章。
首先创建一个工作线程(workerThread),一个与workerThread关联的Handler(workerHandler),还有一个和UI线程关联的Handler(uiHandler)。在onDraw()
执行结束之后,会通过workerHander发消息,workerThread收到消息会去重新计算绘制参数。完成之后,会通过uiHandler发消息,UI线程收到消息之后调用invalidate()
,会导致onDraw()被执行。进入下一次循环。
这样就实现了,重新计算绘制参数在workerThread执行,invalidate()
在UI线程执行。在这里,handler充当信使的作用,它可以在其他线程向自己关联的线程发消息、post runnable等等。
HandlerThread知识
HandlerThread类的注释:
Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.
Handler类的注释
/**
* A Handler allows you to send and process {@link Message} and Runnable
* objects associated with a thread's {@link MessageQueue}. Each Handler
* instance is associated with a single thread and that thread's message
* queue. When you create a new Handler, it is bound to the thread /
* message queue of the thread that is creating it -- from that point on,
* it will deliver messages and runnables to that message queue and execute
* them as they come out of the message queue.
*
* <p>There are two main uses for a Handler: (1) to schedule messages and
* runnables to be executed as some point in the future; and (2) to enqueue
* an action to be performed on a different thread than your own.......
源码
public class LoadingView extends View {
/**
* A list that contains the cx of each point.
*/
private List<Float> cx;
/**
* The cy of every point is the same.
*/
private float cy;
/**
* The radius of every point is the same.
*/
private float radius;
/**
* The paints that used to draw each point.
*/
private List<Paint> paints;
/**
* The length that point transfer to en