2013-07-24 54 views
0

我正在創建一個臉部識別應用程序,它在檢測到臉部時繪製矩形。目前,所有的計算都在主線程上完成,因爲它佔用了大量的CPU。 我決定在不同的線程上運行計算。現在的問題是,是否可以從後臺線程本身繪製畫布?或者我必須將參數發送到主線程,然後從那裏畫畫布?是否可以使用後臺線程繪製畫布?

回答

0

這是沒有問題的。它可以幫助您優化渲染。

5

是的,從另一個線程渲染你的畫布,而不是從UI線程渲染!這將改善和優化性能。

下面是一些代碼,我寫的,不只是這一點:

package com.webstorms.framework; 

import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.view.SurfaceView; 

public class RenderView extends WSObject implements Runnable { 

    Bitmap gameScreen; 
    SurfaceView surfaceView; 
    Thread gameloop; 
    boolean running; 

    int sleepTime; 
    int numberOfFramesSkipped; 
    int maxFrameSkips; 
    long beginTime; 
    long endTime; 
    long lastTime; 
    int differenceTime; 
    int framePeriod; 
    Canvas frameBuffer; 
    int frameCount; 

    int realFPS; 
    int setFPS; 

    /** 
    * This class is the game loop that will update and render the game. 
    * 
    */ 

    public RenderView(Game game, Bitmap gameScreen, int fps, int maxFrameSkips) { 
     super(game); 
     this.gameScreen = gameScreen; 
     surfaceView = new SurfaceView(game); 
     this.setFPS = fps; 
     this.framePeriod = 1000/this.setFPS; 
     this.maxFrameSkips = maxFrameSkips; 
     lastTime = System.currentTimeMillis(); 
     beginTime = System.currentTimeMillis(); 

    } 

    public SurfaceView getView() { 
     return this.surfaceView; 

    } 

    @Override 
    public void run() { 
     while(running) { 
      if(this.surfaceView.getHolder().getSurface().isValid()) { 

       beginTime = System.currentTimeMillis(); 
       this.getGame().getInput().update(); // Synchronize input and call all attached listeneres 
       this.getGame().getCurrentScreen().update(); 
       this.renderFrameBuffer(); 

       // Frame Per Second Count 
       frameCount++; 

       if(lastTime + 1000 < System.currentTimeMillis()) { 
        WSLog.d(Game.GAME_ENGINE_TAG, this, "REAL FPS: " + frameCount); 
        this.realFPS = frameCount; 
        lastTime = System.currentTimeMillis(); 
        frameCount = 0; 

       } 

       endTime = System.currentTimeMillis(); 
       differenceTime = (int) (endTime - beginTime); 
       sleepTime = (int) (framePeriod - differenceTime); 

       if(sleepTime > 0) { 
        try { 
         Thread.sleep(sleepTime); 

        } 
        catch (InterruptedException exception) { 
         exception.printStackTrace(); 

        } 

       } 
       else { 
        while(sleepTime < 0 && numberOfFramesSkipped < this.maxFrameSkips) { 
         WSLog.d(Game.GAME_ENGINE_TAG, this, "Game thread is only updating the update method and is not rendering anything"); 
         this.getGame().getCurrentScreen().update(); 
         sleepTime += framePeriod; 
         numberOfFramesSkipped++; 

        } 

       } 

      } 

     } 


    } 

    public int getRealFPS() { 
     return this.realFPS; 

    } 

    public int getSetFPS() { 
     return this.setFPS; 

    } 

    private void renderFrameBuffer() { 
     // Update the current virtual screen image 
     this.getGame().getCurrentScreen().render(); 
     // Render the current virtual screen to the real phone screen 
     frameBuffer = this.surfaceView.getHolder().lockCanvas(); 
     if(frameBuffer != null) { // Fix for mysterious bug (FATAL EXCEPTION: Thread) 
      frameBuffer.drawBitmap(this.gameScreen, null, this.getGame().getWSScreen().getGameScreendst(), null); 
      this.surfaceView.getHolder().unlockCanvasAndPost(frameBuffer); 

     } 
     else { 
      WSLog.e(Game.GAME_ENGINE_TAG, this, "Surface has not been created or otherwise cannot be edited"); 

     } 

    } 

    public void resume() { 
     this.running = true; 
     gameloop = new Thread(this); 
     gameloop.start();  

    } 

    public void pause() { 
     this.running = false; 
     running = false;       
     while(true) { 
      try { 
       gameloop.join(); 
       break; 
      } 
      catch (InterruptedException e) { 
       // retry 
      } 

     } 

    } 


} 

當創建的RenderView對象,通過第一個參數我們活動的一個參考。

而且這樣做在你的活動:

this.setContentView(this.renderView.getView()); 
+0

謝謝,這幫助了很多。 – Learner

+0

沒問題,很高興我能幫到你。 :) –