我只使用 java 几个星期,我不知道发生了什么。
我正在制作一个画布,我从教程中复制粘贴了这段代码,但是它有一堆错误,我确信这些错误只会发生,因为我不知道如何实际设置代码。本教程适用于已经知道如何进行线程的人,我迷路了。我不知道如何解决这些错误。
即:
postInvalidate() “不是方法”之类的,即使它是内置的 android 函数......与 setWillNotDraw 和其他函数相同。
我非常困惑,我不知道如何解决这些错误。
package com.example.routedrawingtest;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
class PanelThread extends Thread {
private SurfaceHolder _surfaceHolder;
private DrawingPanel _panel;
private boolean _run = false;
public PanelThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) { //Allow us to stop the thread
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) { //When setRunning(false) occurs, _run is
c = null; //set to false and loop ends, stopping thread
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
postInvalidate();
}
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
_thread = new PanelThread(getHolder(), this); //Start the thread that
_thread.setRunning(true); //will make calls to
_thread.start(); //onDraw()
}
public void surfaceDestroyed(SurfaceHolder holder) {
try {
_thread.setRunning(false); //Tells thread to stop
_thread.join(); //Removes thread from mem.
} catch (InterruptedException e) {}
}