在 Android 中绘制到 SurfaceView
我正在尝试在 SurfaceView
中进行一些非常简单的绘制,但我无法让它工作。没有例外,但我也没有看到任何结果。
更准确地说,我正在尝试创建一个 SurfaceView 并用单一颜色填充它。
我的代码如下:
public class SvetlinSurfaceViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceRenderer renderer = new SurfaceRenderer(this);
setContentView(renderer);
}
}
class SurfaceRenderer extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Svetlin Surface Renderer";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
public SurfaceRenderer(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
tryDrawing(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
tryDrawing(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
}
}
正如预期的那样,我得到了 Drawing... 输出两次。
有人能建议为什么我可能无法在 SurfaceView
上绘制吗?
I'm trying to do some very simple drawing into a SurfaceView
but I can't get it working. There are no exceptions, but I don't see any result either.
More precisely, I'm trying to create a SurfaceView
and fill it with a single colour.
Here goes my code:
public class SvetlinSurfaceViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceRenderer renderer = new SurfaceRenderer(this);
setContentView(renderer);
}
}
class SurfaceRenderer extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Svetlin Surface Renderer";
SurfaceView mSurfaceView;
SurfaceHolder mHolder;
public SurfaceRenderer(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
tryDrawing(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
tryDrawing(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
}
}
As expected, I'm getting Drawing... outputted twice.
Could anyone suggest why I might no be able to draw over the SurfaceView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我删除了代理类
SurfaceRenderer
并将其全部移至我的活动中。现在正在工作。这是固定代码:I removed the proxy class
SurfaceRenderer
and moved it all into my activity. It's working now. Here goes the fixed code: