4
我想創建一個簡單的Android應用程序,它將在我觸摸屏幕的地方繪製圓圈。它與View(緩慢)一起工作,但與SurfaceView不兼容。結果很奇怪 - 點擊時,整個圖像正在移動。我也嘗試從另一個線程調用繪圖函數,但結果相同。還發現了另一個例子,這個奇怪的行爲: http://android-er.blogspot.com/2010/05/android-surfaceview.html 我使用Android 2.3.3,API級別10.任何幫助,將不勝感激。Android的SurfaceView閃爍
package com.samsung.sketchbook;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SketchBook extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BookView bookView = new BookView(this);
setContentView(bookView);
}
class BookView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Paint paint = new Paint();
private float x, y;
public BookView(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
paint.setColor(Color.WHITE);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("BookView", "surfaceCreated!");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
x = motionEvent.getX();
y = motionEvent.getY();
Canvas canvas = holder.lockCanvas(null);
canvas.drawCircle(x, y, 3, paint);
holder.unlockCanvasAndPost(canvas);
return true;
}
}
}
老兄,你解決了這個問題嗎? – MeLight