现在的位置: 首页 > 架构设计 > 正文

surfaceDestroyed什么时候被调用

2019年12月23日 架构设计 ⁄ 共 938字 ⁄ 字号 评论关闭

  今天看别人的代码,突然有个疑问,surfaceDestroyed这个函数什么时候被调用呢?

  上网搜了一番,基本都说是surface被销毁的时候,才会调用surfaceDestroyed。问题又来了surface什么时候被销毁呢,大家都知道surface是surfaceview的一个专门用于绘图的接口,所以当surfaceview的对象销毁时,surface一定也销毁,这时surfaceDestroyed一定会调用。

  可是,我看的那个程序明明没有销毁surfaceview了啊。分步调试了一下结果发现一个现象:

  有两个继承surfaceview的类各自是 svA和svB这连个类分别定义了各自的对象AView和BView。如果如今activity设置的是AView这个对象,也就是说如今用AView的surface呢。如果这时在activity运行一下代码:

  setContentView(Bview);

  这时,Aview中的surfaceDestroyed就会被调用,由于如今Bview的surface要接管Aview中的surface,Aview的surface就会销毁,这时必定要调用A的surfaceDestroyed函数。

  在这里仅仅是说明一种surfaceDestroyed被调用的情况,由于个人能力有限,仅仅能写到自己发现的一些东西。这时第一次在论坛上写一些东西,希望以后能坚持下去,在接受别人帮助的同一时候来帮助别人。

  最后把android帮助文档surfaceDestroyed调用条件,老外写的简单易懂的原文发过来。

  This is called immediately before a surface is being destroyed. After returning from this call, you should no longer try to access this surface. If you have a rendering thread that directly accesses the surface, you must ensure that thread is no longer touching the Surface before returning from this function

抱歉!评论已关闭.