간단한 게임 엔진을 만들기 위해 스레드를 사용하여 캔버스에서 드로잉을 실험하고 있지만 설명 할 수없는 이상한 문제가 있습니다. 이 "게임"의 목적은 매초마다 캔버스에 원을 그리는 것입니다. 이 방법은 작동하지만, 원하는대로 작동하지 않습니다. 앱이 두 캔버스 사이를 전환하고 각 캔버스에 서클을 추가하는 것처럼 보이므로 같은 수의 서클로 매 초 두 캔버스 사이를 전환 할 수 있지만 다른 캔버스로 전환 할 수 있습니다. 캔버스에 놓으십시오.스레드로 드로잉하는 Android SurfaceView 캔버스
내가 뭘 잘못하고 있는지 모르겠지만, Treadding에 익숙하지 않은데, 내 안드로이드 장치의 코어 수와 관련이 있습니까?
내 코드는 아래에 표시되어 있으므로 threadth를 시작하고 매초마다 원을 그리는 animationthread에 연결되는 layoutfile을 사용하는 launchthread를 사용합니다. (당신은 touchevent를 무시할 수 있습니다, 아직 사용되지 않습니다).
이 프로젝트는 주요 launchthread에서 존재합니다
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.androidtesting.AnimationView
android:id="@+id/aview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
그리고 내부 Thread 클래스 내 서피스 뷰 SurfaceView 클래스 :
class AnimationView extends SurfaceView implements SurfaceHolder.Callback {
private boolean touched = false;
private float touched_x, touched_y = 0;
private Paint paint;
private Canvas c;
private Random random;
private AnimationThread thread;
public AnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
thread = new AnimationThread(holder);
}
class AnimationThread extends Thread {
private boolean mRun;
private SurfaceHolder mSurfaceHolder;
public AnimationThread(SurfaceHolder surfaceHolder) {
mSurfaceHolder = surfaceHolder;
paint = new Paint();
paint.setARGB(255,255,255,255);
paint.setTextSize(32);
}
@Override
public void run() {
while (mRun) {
c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
doDraw(c);
sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
private void doDraw(Canvas canvas) {
//clear the canvas
//canvas.drawColor(Color.BLACK);
random = new Random();
int w = canvas.getWidth();
int h = canvas.getHeight();
int x = random.nextInt(w-50);
int y = random.nextInt(h-50);
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
int size = 20;
canvas.drawCircle(x,y,size,paint);
canvas.restore();
}
public void setRunning(boolean b) {
mRun = b;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
touched_x = event.getX();
touched_y = event.getY();
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
touched = true;
break;
case MotionEvent.ACTION_MOVE:
touched = true;
break;
default:
touched = false;
break;
}
return true;
}
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
코드에 AnimationView2가 있어야합니까? 수정 된 버전을 주시면 감사하겠습니다. – RichieHH
(ps는 샘플 예제에서 코드를 복사 할 때 (가볍게 수정 된 LunarLander 임) 버그 추적을 쉽게하기 때문에 상태를 표시하는 것이 가장 좋습니다)). – RichieHH