我正在尝试修改用于摄影机预览的SurfaceView,以显示覆盖的正方形。但是,永远不会调用扩展的SurfaceView的onDraw方法。
这是来源:
public class CameraPreviewView extends SurfaceView {
protected final Paint rectanglePaint = new Paint();
public CameraPreviewView(Context context, AttributeSet attrs) {
super(context, attrs);
rectanglePaint.setARGB(255, 200, 0, 0);
rectanglePaint.setStyle(Paint.Style.FILL);
rectanglePaint.setStrokeWidth(2);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawRect(new Rect(10,10,200,200), rectanglePaint);
Log.w(this.getClass().getName(), "On Draw Called");
}
}
public class CameraPreview extends Activity implements SurfaceHolder.Callback{
private SurfaceHolder holder;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// We remove the status bar, title bar and make the application fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// We set the content view to be the layout we made
setContentView(R.layout.camera_preview);
// We register the activity to handle the callbacks of the SurfaceView
CameraPreviewView surfaceView = (CameraPreviewView) findViewById(R.id.camera_surface);
holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters params = camera.getParameters();
params.setPreviewSize(width, height);
camera.setParameters(params);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
}
最佳答案
在android-developers Google网上论坛上找到了它。您只需添加:
setWillNotDraw(false)
给构造函数。现在,如果有人可以解释我的原因,将不胜感激。
关于android - 从未调用扩展SurfaceView的onDraw()方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2687015/