位图的顶部没有绘制文本 [英] draw text on top of bitmap failed

查看:134
本文介绍了位图的顶部没有绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个点,并在图像的顶部上的文本。我已经试了几个教程上叠加的位图,但它似乎并没有工作。这里是code,其显示的背景图像。

  mBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.roomplan);
MIV =(ImageView的)findViewById(R.id.ImageView01);
mIV.setImageBitmap(mBitmap);
mIV.invalidate();

btnDraw =(按钮)findViewById(R.id.Button01);
btnDraw.setOnClickListener(本);
 

然后在OnClickListener,我定义另一个位图,绘制点和文本。

 位图bmOverlay = Bitmap.createBitmap(mBitmap.getWidth(),mBitmap.getHeight()
    Bitmap.Config.ARGB_4444);
帆布油画=新的Canvas(bmOverlay);
涂料粉刷=新的油漆();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawText(你在这里,100,100,漆);
canvas.drawPoint(30.0f,50.0f,油漆);
canvas.drawBitmap(bmOverlay,0,0,NULL);
 

没有被显示在背景图象的上面,即使当我去除图像。任何提示吗?

更新:工作code

  //得到一个参考的背景图像
mBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.raumplan_isst);

MIV =(ImageView的)findViewById(R.id.ImageView01);

//创建一个可变位图具有相同的大小作为背景图象
位图bmOverlay = Bitmap.createBitmap(mBitmap.getWidth(),mBitmap.getHeight(),
    Bitmap.Config.ARGB_4444);
//创建要在其上绘制的帆布
帆布=新的Canvas(bmOverlay);

涂料粉刷=新的油漆();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);

//如果背景图像main.xml中定义,省略这一行
canvas.drawBitmap(mBitmap,0,0,NULL);
//绘制文本和点
canvas.drawPoint(fKoordX,fKoordY,油漆);
canvas.drawText(你在这里,fKoordX + 3,fKoordY + 3,油漆);

//设置位图到ImageView的
mIV.setImageBitmap(bmOverlay);
 

解决方案

你用画布是什么?它在哪里使用? (发布一些更code ...)

在那旁边的绘制顺序是错的,你是透支你的文字和点与位图。

编辑:

我是有点失落,因为我不知道您的图片应该是背景和什么样的形象,你已经看到...所以我猜测mBitmap(该roomplan)是你的背景?比起添加到您的布局作为背景图像,只需使用ImageView的绘制您的覆盖......

如果您的覆盖需要一个背景图片也尝试:

  //叠加背景
canvas.drawBitmap(myBmp,0,0,油漆);
//绘制文本和点
canvas.drawText(你在这里,100,100,漆);
canvas.drawPoint(30.0f,50.0f,油漆);
 

如果您的ImageView应该有roomplan为背景,尝试:

  //叠加背景
canvas.drawBitmap(mBitmap,0,0,油漆);
//绘制文本和点
canvas.drawText(你在这里,100,100,漆);
canvas.drawPoint(30.0f,50.0f,油漆);
 

I want to display a point and a text on top of an image. I have tried several tutorial on overlaying bitmap, but it doesn't seem to work. Here is the code which displays the background image.

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);

Then on the OnClickListener, I define another bitmap and draw the point and the text.

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);

Nothing is displayed on top of the background image, even when I remove the image. Any hint please?

Update: The working code

// 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);

解决方案

What do you do with the canvas? Where is it used? (post some more code...)

Beside that the drawing order is wrong, you are overdrawing your text and point with the bitmap.

Edit:

I am a bit lost as I don't know which of your images should be the background and what image you already see... so i am guessing that mBitmap (the roomplan) is your background? Than add that to your layout as a background image and just use the ImageView to draw your overlay...

if your Overlay need a background image too, try that:

// 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);

If your ImageView should have the roomplan as a background, try that:

// 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);

这篇关于位图的顶部没有绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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