在Android/Java中使用画布在图像上绘制 [英] Draw with a Canvas over an Image in Android/Java

查看:215
本文介绍了在Android/Java中使用画布在图像上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Canvas上绘制,背景是Android中加载的图片. 没有backgroundimage,效果很好:

I try to draw on a Canvas, where the background is an loaded Image in Android. It works just fine without the backgroundimage:

background = (ImageView)view.findViewById(R.id.Background);
Bitmap bitmap = Bitmap.createBitmap(canvasSize,canvasSize,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
background.setImageBitmap(bitmap);

我将画布传递给另一个类并在其上进行绘制.很好.

Im passing that canvas to another class and draw on it. That works fine.

但是当我这样做时:

background = (ImageView)view.findViewById(R.id.Background);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); //Just to test

canvas = new Canvas(bitmap); 
//or this
canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));

background.setImageBitmap(bitmap);

它不会覆盖图像,所以您看不到重点. 这是我使用此画布绘制的另一个类的代码:

It doesn't draw over the image, so you can't see the point. Here is the code from the other class where I use this canvas to draw:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawCircle( (float)sObject.getSideGforce()*mult+add, 
(float)sObject.getFrontRearGforce()*mult+add, 15*V.LOGICAL_DENSITY, dot);                                                                                                                          

我的目标是创建一个具有自定义尺寸的画布,该画布具有可作为背景绘制的图像. 它是Gforce显示器,因此图像将是带有数字的一些圆圈,并且画布将在其上绘制一个点,这样您就可以看到要拉动的G数.就像我已经说过的那样,在没有背景的情况下它可以完美运行.而且,我不想每次重新定位点时都重新绘制背景.因此背景应该是静态的,画布上的点应该是动态的(在100hz处). 预先感谢.

My goal is to create a Canvas with a custom size which has an image as a background where I can draw on. Its a Gforce Display, so the Image will be some circles with numbers and the canvas will draw a point over it so you can see how many Gs you are pulling. Like I already said it works perfectly without the background. And I dont want to redraw the background every single time I reposition the point. So the background should be static and the point should on the canvas is dynamic ( at 100hz ). Thanks in advance.

解决方案: 可以使用此代码.

SOLUTION: It works with this code.

view.setBackgroundResource(R.drawable.ic_launcher);

我只需要设置视图的背景.它将图像拉伸到我片段的大小,但这没关系.

I just had to set the Background of my view. It stretches the image to the size of my fragment, but thats OK.

推荐答案

BitmapFactory.decodeResource()创建且不可变的位图,因此有不能更改它的原因.

BitmapFactory.decodeResource() creates and immutable Bitmap, so there is why you can't change it.

您需要创建一个位图作为您的工作代码,然后使用canvas.drawBitmap()在后台绘制所需的位图.然后画出你想要的东西.

You need to create a Bitmap as your working code, then use canvas.drawBitmap() to draw the Bitmap you want in the background. And then draw the things you want.

或使用setBackground()作为背景,仅在画布上绘制点.

Or use setBackground() with the background and only draw the points on the canvas.

这篇关于在Android/Java中使用画布在图像上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆