如何在 Android 中将画布背景更改为图像?
我有下面的代码可以保存用户的绘图。它是我的绘画应用程序的一部分。现在,画布的背景颜色是白色,我希望它是一个图像。
例如,我有一张房子的图像,我想在它上面画一些东西。
@Override
public void run() {
Canvas canvas = null;
while (_run){
if(isDrawing == true){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0xffffffff);
commandManager.executeAll(c,previewDoneHandler);
previewPath.draw(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
非常感谢您的帮助! :)
I have the code below that saves a user's drawing. It's a part of my paint app. Now, the canvas' background color is white and I want it to be an image.
For example I have an image of a house and I want to draw something on top of it.
@Override
public void run() {
Canvas canvas = null;
while (_run){
if(isDrawing == true){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(0xffffffff);
commandManager.executeAll(c,previewDoneHandler);
previewPath.draw(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
Thanks a lot for any help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要更改画布的背景,请在
mainActivity.java
的onCreate()
中键入此行。此外,您可以尝试在 onDraw() 中执行此操作,但性能会降低大大减少
To change the background of the canvas, type this line in
onCreate()
of yourmainActivity.java
Also, you can try doing this in your onDraw() however the performance will greatly reduce
我认为您正在寻找这个:
画布上的可绘制图像
I think your looking for this:
Drawable image on a canvas