微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在位图顶部绘制文本失败

我想在图像上显示一个点和一个文本.我已经尝试了几个关于覆盖位图的教程,但它似乎不起作用.这是显示背景图像的代码.
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,paint);
canvas.drawPoint(30.0f,50.0f,paint);
canvas.drawBitmap(bmOverlay,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(),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,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(roomplan)是你的背景?然后将其添加到您的布局作为背景图像,并使用ImageView绘制您的叠加层…

如果您的叠加层也需要背景图片,请尝试:

// overlay background
canvas.drawBitmap(myBmp,paint);
// draw the text and the point
canvas.drawText("You are here",paint);

如果您的ImageView应该将roomplan作为背景,请尝试:

// overlay background
canvas.drawBitmap(mBitmap,paint);

原文地址:https://www.jb51.cc/android/315311.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐