从 onDraw() 绘制到创建的位图
我正在尝试绘制位图,以便可以将自定义视图放入 imageView 中。onDraw 方法中的代码是:
public void onDraw(Canvas canvas) {
Bitmap drawGraph = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(drawGraph);
canvas.drawBitmap(drawGraph, 0, 0, bgPaint);
我的问题是,如果我尝试以这种方式使用位图,我只会得到黑屏。我知道如果我不尝试绘制位图,我的其余代码将按照显示的方式工作。
如果我注释掉该行
canvas.setBitmap(drawGraph);
那么一切都会正常工作,所以这就是问题所在,但我不知道为什么。
我哪里出错了?
I'm trying to draw to a Bitmap so I can put my custom view inside an imageView.The code within the onDraw method is:
public void onDraw(Canvas canvas) {
Bitmap drawGraph = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(drawGraph);
canvas.drawBitmap(drawGraph, 0, 0, bgPaint);
My problem is that if I try to use a Bitmap in this way, I just get a black screen. I know that the rest of my code works as it displays if I don't try to draw to a bitmap.
If I comment out the line
canvas.setBitmap(drawGraph);
Then everything works perfectly, so this is the problem but I dont know why.
where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实证明我确实必须创建第二个画布。我的工作代码如下,供可能需要它的人使用:
Turns out I did have to create a second canvas. My working code is below just for anyone who might need it:
我认为是
canvas
和canvas2
二分法。尝试只使用canvas2(参数)来绘制。I think is the
canvas
andcanvas2
dichotomy. Try to use only canvas2 (the parameter) to draw.AFAIK 最有效的方法是覆盖可绘制设置器。
AFAIK The most efficient way is to override drawable setters.