我正在創建一個臉部識別應用程序,它在檢測到臉部時繪製矩形。目前,所有的計算都在主線程上完成,因爲它佔用了大量的CPU。 我決定在不同的線程上運行計算。現在的問題是,是否可以從後臺線程本身繪製畫布?或者我必須將參數發送到主線程,然後從那裏畫畫布?是否可以使用後臺線程繪製畫布?
0
A
回答
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());
相關問題
- 1. 是否可以在HTML5畫布中繪製Unity畫布?
- 2. 是否可以使用vanilla JavaScript創建和繪製畫布?
- 3. Android SurfaceView使用線程繪製畫布
- 4. 用畫布繪製線條
- 5. 是否可以繪製動畫?
- 6. 是否可以在Framer JS中使用'畫布'繪圖功能?
- 7. 畫布在繪製後更改線寬
- 8. 是否可以繪製比當前屏幕更大的畫布?
- 9. 是否可以在WebGL內容上繪製透明畫布?
- 10. 是否可以在元素內的畫布上繪製?
- 11. 使用WPF在畫布上繪製線
- 12. 畫布,繪製線段
- 13. 繪製在畫布光線
- 14. 是否可以在後臺使用WatchConnectivity
- 15. 是否可以使用SFML繪製曲線?
- 16. 繪製文本在後臺線程IOS
- 17. 在控制檯上繪製HTML5畫布
- 18. 使用HTML5畫布繪製方程
- 19. 是否可以使用Java gui畫布並跟蹤鼠標在畫布上繪製的內容?
- 20. 使用畫布繪製Rects
- 21. 在畫布中繪製動畫曲線
- 22. 防止使用SurfaceView繪製畫布以外的畫布
- 23. HTML5用畫布繪製,旋轉線?
- 24. 用畫布繪製流暢的線條
- 25. Silverlight - 是否可以在後臺線程上創建UI元素?
- 26. 是否可以強制自動屬性使用只讀後臺?
- 27. 線程是否可以調用線程?
- 28. 是否可以使用node-webkit創建後臺應用程序?
- 29. 如何使用畫布動畫繪製曲線?
- 30. 是否可以使用Matplotlib繪製隱式方程?
謝謝,這幫助了很多。 – Learner
沒問題,很高興我能幫到你。 :) –