얼굴을 인식했을 때 직사각형을 그리는 얼굴 인식 앱을 만들고 있습니다. 현재 모든 계산은 주 스레드에서 수행되므로 많은 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. 한 번에 여러 캔버스를 그릴 수 있습니까?
- 2. 캔버스를 사용하여 텍스트를 그릴 때 안드로이드가 발생했습니다.
- 3. 왜 내 캔버스를 그릴 수 없습니까?
- 4. Android : 보조 캔버스를 사용하여 기존 캔버스 위에 그릴 수 있나요?
- 5. 투명 캔버스를 만들 수 있습니까?
- 6. clearRect가 세로선을 그릴 때 캔버스를 지우지 않습니다.
- 7. 캔버스를 사용하여 이미지를 조작 할 수 있습니까?
- 8. mySQL 데이터를 사용하여 캔버스를 만들 수 있습니까?
- 9. 런처가 Android에서 배경 무늬를 그릴 책임이 있습니까?
- 10. jquery에서 캔버스를 사용하지 않고 mousemove로 선을 그릴 수 있습니다.
- 11. Raphael.js를 사용하여 이미지 그릴
- 12. 나사로 - 사이클 중에 그릴 수 있습니까?
- 13. 투명하게 그릴 수 없음 구성 요소 배경
- 14. 어떻게 캔버스를 파이썬과 함께 사용할 수 있습니까?
- 15. 카메라보기로 그릴 수 있습니까?
- 16. drawContours를 사용하여 점을 그릴 수 있습니까?
- 17. android.graphics.Path를 사용하여 도트를 그릴 수 있습니까?
- 18. Selenium Python을 사용하여 캔버스에 그릴 수 있습니까?
- 19. sparkTable을 사용하여 막대 그래프를 그릴 수 있습니까?
- 20. SFML을 사용하여 커브를 그릴 수 있습니까?
- 21. CSS3를 사용하여 대각선을 그릴 수 있습니까?
- 22. iOS에서 이미지로 개요를 그릴 수 있습니까?
- 23. 캔버스를 새로 고침하여 새 객체를 그릴 수 있습니다.
- 24. Java : 투명 TextArea + 배경 그릴
- 25. 캔버스를 사용하여 비디오를 재생하십시오.
- 26. 배경 작업자 스레드를 사용하여 ListView 렌더링
- 27. 이 Java GUI API를 사용하여 어떻게 그릴 수 있습니까?
- 28. 배경 이미지가있는 캔버스를 다운로드하는 방법은 무엇입니까?
- 29. 배경 스레드를 시작하는 방법?
- 30. Eclipse를 사용하여 실행중인 스레드를 찾을 수 있습니까?
감사합니다. – Learner
문제 없으니 기꺼이 도와 드리겠습니다. :) –