java 在 Android 中绘制到 SurfaceView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6690898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing to a SurfaceView in Android
提问by Albus Dumbledore
I'm trying to do some very simpledrawing into a SurfaceView
but I can't get it working. There are no exceptions, but I don't see any result either.
我正在尝试将一些非常简单的绘图绘制到 a 中,SurfaceView
但无法正常工作。没有例外,但我也没有看到任何结果。
More precisely, I'm trying to create a SurfaceView
and fill it with a single colour.
更准确地说,我正在尝试创建一个SurfaceView
并用单一颜色填充它。
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
?
谁能建议为什么我可能无法绘制SurfaceView
?
回答by Albus Dumbledore
I removed the proxy class SurfaceRenderer
and moved it all into my activity. It's working now. Here goes the fixed code:
我删除了代理类SurfaceRenderer
并将其全部移到我的活动中。它现在正在工作。这是固定代码:
public class SvetlinSurfaceViewTestActivity
extends Activity
implements SurfaceHolder.Callback {
private static final String TAG = "Svetlin SurfaceView";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView view = new SurfaceView(this);
setContentView(view);
view.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
tryDrawing(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, 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) {
Random random = new Random();
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
}
}