2
내 main.xml 레이아웃에서 작성한 SurfaceView에서 그리려는 간단한 코딩 시도를하고 있습니다. 배경색을 변경하고 아이콘을 잘 표시 할 수 있지만 그릴 때 오류가 발생합니다. 나는 초보자이다. 그래서 나는 뭔가를 놓치고있다. 도움의 힌트를 빌려줘, 고마워!SurfaceView로 그리기 할 때 응용 프로그램 오류가 발생했습니다.
main.xml에
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<SurfaceView android:id="@+id/Paper"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</SurfaceView>
</LinearLayout>
여기에 코드;
package com.example.SurfaceViewTest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Paint paint;
Bitmap mDrawing;
boolean mRun;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) this.findViewById(R.id.Paper);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mSurfaceView.setBackgroundColor(Color.rgb(0, 255, 0));
mRun=true;
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
Thread thread = new Thread(){
public void doDraw(Canvas c){
mDrawing = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565);
c.setBitmap(mDrawing);
paint = new Paint();
paint.setColor(Color.rgb(255, 255,255));
c.drawLine(1,1,200,300, paint);
}
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
};
}
업데이트 :이 작품 덕분에 그것을 가지고
좋아!
package com.example.SurfaceViewTest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
Bitmap mDrawing;
Canvas tempCanvas = new Canvas();
Paint paint;
boolean mRun;
int intCanvasWidth, intCanvasHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) this.findViewById(R.id.Paper);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
intCanvasWidth = width;
intCanvasHeight = height;
mDrawing = Bitmap.createBitmap(intCanvasWidth, intCanvasHeight,
Bitmap.Config.RGB_565);
paint = new Paint();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (thread.getState() == Thread.State.TERMINATED) {
thread = new Thread();
}
mRun = true;
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mRun = false;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
Thread thread = new Thread() {
public void doDraw(Canvas c) {
tempCanvas.setBitmap(mDrawing);
paint.setColor(Color.rgb(255, 255,255));
tempCanvas.drawLine(1,1,200,300, paint);
c.drawBitmap(mDrawing, 0, 0, null);
}
@Override
public void run() {
Canvas c;
while (mRun) {
c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
};
}
답장을 보내 주셔서 감사합니다.하지만 새 스레드에 빠진 부분이있는 것 같습니다. 내가 안드로이드와 자바 모두에서 초보자 인 곳을 지적 해 주시겠습니까? – DKDiveDude
Eclipse를 사용하고 있습니까? 그렇지 않으면 정말로해야합니다. 브래킷이없는 부분을 대략적으로 지적합니다 (끝 부분에 있음). – fredley
감사합니다, 그것은 그때의 모든 길입니다. ";" 필드 선언 또는 "}"을 완료하십시오. 둘 다 오류를 제거하지 않습니다. – DKDiveDude