简体   繁体   English

同步的Android表面视图

[英]Synchronized Android surface view

I am kind of new to java and android. 我是Java和android的新手。 Now I am using the canvas to draw on the surface view because the faster speed than customer view. 现在,我使用画布在表面视图上绘制,因为速度比客户视图快。 But when it comes to synchronized the surface view in android I am getting kind of confuse 但是当涉及到在Android中同步表面视图时,我有点困惑

I take this link as the reference http://android-er.blogspot.com/2010/05/android-surfaceview.html also the android lunar lander 我将此链接作为参考http://android-er.blogspot.com/2010/05/android-surfaceview.html也是android月球着陆器

I have two version that slightly modified form example when I write the surfaceview rendering thread run method part, but I am not sure which one is more correct in concept. 当我编写Surfaceview渲染线程运行方法部分时,我有两个版本对表单示例进行了稍微修改,但是我不确定哪一个在概念上更正确。 They both works, But I just want to clear my concepts. 它们都起作用,但是我只想清除我的概念。

Thank for any help and suggestions in advanced and if i have ask question in wrong way please also corrected me :) 感谢您的高级帮助和建议,如果我以错误的方式提出问题,请也纠正我:)

version1 : 版本1:

protected void run()
{
  while (isRunning) 
 {       
          if(RenderThreadSurfaceHolder.getSurface().isValid())
          {  
            Canvas Draw = null;
            try 
            {
              Draw =RenderThreadSurfaceHolder.lockCanvas(null);
              synchronized (RenderThreadSurfaceHolder){     
                  if(Draw!=null)
                  {                                                                       
                       RenderThreadSurfaceView.onDraw(Draw);
                  }  
              }
            }
            finally{                    
              if(Draw!=null) 
              {                                                    
                      RenderThreadSurfaceHolder.unlockCanvasAndPost(Draw);
              }
            }
        }
    }
}  

version 2 : 版本2:

protected void run()
{
  while (isRunning) 
 {       
          Canvas Draw = null;  
                try 
                {    

                     synchronized (RenderThreadSurfaceHolder) 
                     {       
                             if(RenderThreadSurfaceHolder.getSurface().isValid())
                             { 

                                   Draw=RenderThreadSurfaceHolder.lockCanvas(null);
                                   if(Draw!=null)
                                   {   
                                      RenderThreadSurfaceView.onDraw(Draw);
                                   } 

                             }  
                     }
                 }   
                 finally 
                 {

                             if (Draw != null) 
                             {
                                 RenderThreadSurfaceHolder.unlockCanvasAndPost(Draw);
                             }
                 }
    }
}  

Edit 编辑

In additional synchronized the surface view I also want to add on a lock to control the data that share data between Activity and OnDraw method Because I Think synchronized lots of data and lots process function is too much overhead So I want to just synchronized a boolean , But What I do research on the Internet say that Synchronized boolean is really not a good idea //still confusion me a lot such as 在附加的同步表面视图中,我还想添加一个锁来控制在Activity和OnDraw方法之间共享数据的数据,因为我认为同步的大量数据和大量的流程函数的开销太大,因此我想同步一个布尔值,但是我在互联网上所做的研究表明,同步布尔值确实不是一个好主意//仍然让我感到困惑,例如

synchronized (Check) 
{
   Check=true;   
}

But if I do something like below 但是如果我做下面的事情

private static lock Lock; //declare in the Activity

public synchronized void setlock(boolean newlock) 
{
  if (newlock!=lock){
    lock = newlock;
  } 
}

public synchronized boolean isTrue() {
    return lock;
}

Is that still not a good idea? 那还是一个好主意吗?

My concept is want to add the lock between the process function and OnDraw method : 我的概念是要在过程函数和OnDraw方法之间添加锁:

This is a very subjective question, but I would go with 1. 这是一个非常主观的问题,但我会选择1。

The first approach has a tighter synchronized(RenderThreadSurfaceHolder) ; 第一种方法具有更紧密的synchronized(RenderThreadSurfaceHolder) less code requires synchronization and this can help avoid blocking issues. 更少的代码需要同步,这可以帮助避免阻塞问题。

Besides that, it seems to be mostly ordering. 除此之外,它似乎主要是订购。 In solution 1 it also makes sense to call if(RenderThreadSurfaceHolder.getSurface().isValid()) as soon as possible, because if the surface is invalid then nothing can happen anyways. 在解决方案1中,尽快调用if(RenderThreadSurfaceHolder.getSurface().isValid())也很有意义,因为如果表面无效,则无论如何都不会发生任何事情。 This avoids unnecessary if/else checks. 这样可以避免不必要的if / else检查。

yeah, synchronization doesn't work on a boolean. 是的,同步不适用于布尔值。 The reason is that BOOLEAN.TRUE and BOOLEAN.FALSE are static constants. 原因是BOOLEAN.TRUE和BOOLEAN.FALSE是静态常量。

The idea is that when you synchronize a reference variable, any other threads that try to use that must wait until the currently running synchronized block ends. 这个想法是,当您同步参考变量时,任何其他尝试使用的线程都必须等到当前运行的同步块结束。 As your boolean references either BOOLEAN.TRUE or BOOLEAN.FALSE, as soon as you synchronize on a second boolean you will start to see issues. 当布尔值引用BOOLEAN.TRUE或BOOLEAN.FALSE时,一旦在第二个布尔值上进行同步,就会开始发现问题。

continue to synchronize on the RenderThreadSurfaceHolder , that is how I do it and how I have seen it done. 继续在RenderThreadSurfaceHolder上进行同步,这就是我的操作方式以及我看到的方式。

Why would you want to add a lock between drawing and updating? 为什么要在绘图和更新之间添加锁? Maybe if I knew the reason, I could help ya out with another approach. 也许如果我知道原因,我可以用另一种方法帮助您。 Also, That will just add overhead, remember you want to keep your game loop as tight as possible. 另外,这只会增加开销,请记住您要保持游戏循环尽可能紧密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM