Android 在位图顶部绘制文本失败

Android 在位图顶部绘制文本失败,android,graphics,bitmap,overlay,Android,Graphics,Bitmap,Overlay,我想在图像顶部显示一个点和一个文本。我已经尝试了几个关于叠加位图的教程,但它似乎不起作用。下面是显示背景图像的代码 mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roomplan); mIV = (ImageView)findViewById(R.id.ImageView01); mIV.setImageBitmap(mBitmap); mIV.invalidate(); btnDraw = (Button

我想在图像顶部显示一个点和一个文本。我已经尝试了几个关于叠加位图的教程,但它似乎不起作用。下面是显示背景图像的代码

mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roomplan);
mIV = (ImageView)findViewById(R.id.ImageView01);
mIV.setImageBitmap(mBitmap); 
mIV.invalidate();

btnDraw = (Button)findViewById(R.id.Button01);
btnDraw.setOnClickListener(this);
然后在OnClickListener上,我定义另一个位图并绘制点和文本

Bitmap bmOverlay = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), 
    Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);
canvas.drawBitmap(bmOverlay, 0, 0, null);
即使我删除了背景图像,也不会在背景图像顶部显示任何内容。有什么提示吗

更新:工作代码

// get a reference to the background image
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.raumplan_isst);

mIV = (ImageView)findViewById(R.id.ImageView01);

// create a mutable bitmap with the same size as the background image
Bitmap bmOverlay = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), 
    Bitmap.Config.ARGB_4444);
// create a canvas on which to draw
canvas = new Canvas(bmOverlay);

Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);

// if the background image is defined in main.xml, omit this line
canvas.drawBitmap(mBitmap, 0, 0, null);
// draw the text and the point
canvas.drawPoint(fKoordX, fKoordY, paint);
canvas.drawText("You are here", fKoordX+3, fKoordY+3, paint);

// set the bitmap into the ImageView
mIV.setImageBitmap(bmOverlay);

你用帆布做什么?在哪里使用?(发布更多代码…)

除了绘图顺序错误之外,您正在过度绘制文本并使用位图指向

编辑:

我有点迷茫,因为我不知道你的哪些图片应该是背景,你已经看到了什么图片。。。所以我猜mBitmap(房间平面图)是你的背景?然后将其作为背景图像添加到布局中,然后使用ImageView绘制覆盖图

如果覆盖图也需要背景图像,请尝试:

// overlay background
canvas.drawBitmap(myBmp, 0, 0, paint);
// draw the text and the point
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);
// overlay background
canvas.drawBitmap(mBitmap, 0, 0, paint);
// draw the text and the point
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);
如果您的ImageView应以roomplan为背景,请尝试:

// overlay background
canvas.drawBitmap(myBmp, 0, 0, paint);
// draw the text and the point
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);
// overlay background
canvas.drawBitmap(mBitmap, 0, 0, paint);
// draw the text and the point
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);

这很正常,在绘制位图之前先绘制文本和点。当然,您的位图将重叠所有内容!看看我的答案,就是这样。第一个代码在
onCreate()
方法中,第二个代码在
OnClickListener
中。好的,因为我得到了关于绘图顺序的相同答案,所以我将尝试玩一玩,然后返回给您;)你需要将画布应用到某个地方。画布是你绘制的地方,但它必须在UI的某个地方使用,因为当你不使用它时,它将不会显示…@WarrenFaith是的,明白了。现在我可以看到要点和文本,但仍然没有在叠加运气。你能给我一个提示吗?当你看到点和文本时,你应该不难画位图,就像画点和文本一样,但是在它之前。如果没有,用你所掌握的更新问题,please@WarrenFaith我把修改过的代码放进去了。我看不出应该在哪里设置背景图像。