[转]postinvalidate、postinvalidateOnAnimation和invalidate的区别 您所在的位置:网站首页 mesaure [转]postinvalidate、postinvalidateOnAnimation和invalidate的区别

[转]postinvalidate、postinvalidateOnAnimation和invalidate的区别

2023-03-09 19:37| 来源: 网络整理| 查看: 265

android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。 Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。   Android程序中可以使用的界面刷新方法有两种,分别是利用Handler和利用postInvalidate()来实现在线程中刷新界面。 利用invalidate()刷新界面   实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新;而在线程中通过sendMessage发送界面更新消息。 Java代码

// 在onCreate()中开启线程 new Thread( new GameThread()).start();、 // 实例化一个handler Handler myHandler = new Handler() { // 接收到消息后处理 public void handleMessage(Message msg) { switch (msg.what) { case Activity01.REFRESH: mGameView.invalidate(); // 刷新界面 break ; } super .handleMessage(msg); } }; class GameThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = Activity01.REFRESH; // 发送消息 Activity01.this .myHandler.sendMessage(message); try { Thread.sleep(100 ); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } [java] view plaincopy // 在onCreate()中开启线程 new Thread(new GameThread()).start();、 // 实例化一个handler Handler myHandler = new Handler() { // 接收到消息后处理 public void handleMessage(Message msg) { switch (msg.what) { case Activity01.REFRESH: mGameView.invalidate(); // 刷新界面 break; } super.handleMessage(msg); } }; class GameThread implements Runnable { public void run() { while (!Thread.currentThread().isInterrupted()) { Message message = new Message(); message.what = Activity01.REFRESH; // 发送消息 Activity01.this.myHandler.sendMessage(message); try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } 复制代码

使用postInvalidate()刷新界面 使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。 再来看postInvalidateOnAnimation的执行过程 InvalidateOnAnimationRunnable本质上是一个Runnable ,run()方法中实际执行view.invalidate方法,也就是说现在的问题是run()怎么执行的,什么时候执行的?! 这里将InvalidateOnAnimationRunnable 做为参数传给了mChoreographer对象,也就是这里的逻辑就完成了,剩下的invalidate任务交给了mChoreographer。 其中mPosted为true时表示已经通知了mChoreographer来处理刷新的任务,但任务还没有真正执行,也就在有新的view刷新任务添加时,不再重复通知了。 c.run(frameTimeNanos);这里也就是对应InvalidateOnAnimationRunnable的run()方法。 c是CallbackRecord的实例,CallbackRecord里拥有InvalidateOnAnimationRunnable的实例,即下面代码中的action; c.run(frameTimeNanos);中有个时间参数,但((Runnable)action).run();是没有用到的,可以不关心。 所以现在找定义执行该函数时间的地方。 doCallbacks被doFrame方法调用,doFrame方法执行是在handleMessage中: 而发送MSG_DO_FRAME消息的代码为: 那么执行时间就由nextFrameTime来决定了。也就是从当前时间开始的10ms内(理论情况)。 这里是没有使用Vsync同步机制的情况,使用Vsync会增加一些另外的操作,但是最终执行逻辑是一样的,详细的分析可参考Android系统Choreographer机制实现过程。 总结一下: postInvalidate方法是将任务添加到队列中排队后立即执行的,而postInvalidateOnAnimation依赖上一帧动画的的执行时间,因为动画的刷新是存在一个频率的,直到下一帧动画的时间才会真正执行刷新操作。 而postIfNeededLocked()干的事情就是把mInvalidateOnAnimationRunnable作为Choreographer.CALLBACK_ANIMATION(这个类型的task会在mesaure/layout/draw之前被运行)的Task 交给 UI线程的Choreographer. View的invalidate会进一步触发ViewRootImpl的invalidateChildInParent()->invalidate(): 不过虽然没有发出去,不代表这次invalidate就不会生效,因为前面的invalidate()里已经设置了mDirty了:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有