2013-07-24 2 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

문제 없으니 기꺼이 도와 드리겠습니다. :) –

관련 문제