我正在构建一个surfaceView 类来显示来自相机的图像。我花了一段时间才让它工作,但我添加了一些 toasts、对话框和一些布尔变量来在 AnnotateSurface 类和 Activity 类中调试它。如果它是真的,我在 if 语句中使用其中一个来运行线程以将图像绘制到表面(这就是阻止它工作的原因)。
if(bitmapState){
drawSomething(canvas);
}
我已经让它工作了,但只有在我发现布尔值 bitmapState 被重置为 false 之后。它被初始化为 false,但我在 getBitmap 方法中将其更改为 true。这是编辑该布尔变量的唯一方法。
setBitmap 方法中的 toast 显示“true”,但是当我从 Activity 类调用 getStuff 方法时,它显示为 false,并且当我使用以下命令时,run 方法中的 if 语句也不起作用:
if (surfaceState == bitmapState == true) {
drawSomething(canvas);
}
这真的很令人困惑,因为所有其他布尔值都表现正确。如果有人有任何见解,我很想听听。
public class AnnotateSurface extends SurfaceView implements Callback, Runnable {
// create a holder to manage the surface of the view
SurfaceHolder ourHolder;
// Create a thread
Thread ourThread = null;
// Create a boolean to determine when to stop the animation
boolean isRunning = false;
boolean surfaceState = false;
boolean bitmapState = false;
public static Bitmap bmpIm = null;
Context c;
Canvas canvas;
int ws, hs;
public AnnotateSurface(Context context) {...
}
public AnnotateSurface(Context context, AttributeSet attrs) {...
}
public AnnotateSurface(Context context, AttributeSet attrs, int defStyle) {...
}
// create a method to pause the thread
public void pause() {...
}
public void resume() {...
}
// create a method to resume the thread
public void setBitmap(Bitmap b) {
bmpIm = b;
// bmpIm = AnnotateImage.bmpAnnotate;
// isRunning = true;
bitmapState = true;
Toast toast = new Toast(c);
Toast.makeText(c, Boolean.toString(bitmapState), Toast.LENGTH_LONG)
.show();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {...
}
@Override
public void surfaceCreated(SurfaceHolder holder) {...
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
surfaceState = false;
}
// @Override
public void drawSomething(Canvas canvas) {...
}
public String getstuff() {
String Data = "not drawing" + Boolean.toString(this.bitmapState)
+ Boolean.toString(surfaceState) + Boolean.toString(isRunning)
+ Integer.toString(ws) + Integer.toString(hs);
return Data;
}
public Bitmap getstuffImage() {...
}
@Override
public void run() {...
}
}