首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在位图上绘制文本失败

在位图上绘制文本失败
EN

Stack Overflow用户
提问于 2011-02-15 10:51:31
回答 1查看 7.1K关注 0票数 7

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

代码语言:javascript
复制
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上定义另一个位图并绘制点和文本。

代码语言:javascript
复制
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);

即使当我删除图像时,也不会在背景图像的顶部显示任何内容。有什么提示吗?

更新:工作代码

代码语言:javascript
复制
// 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-15 10:54:25

你拿帆布做什么?它在哪用的?(发布更多代码.)

此外,绘图顺序是错误的,您正在透支您的文本和点与位图。

编辑:

我有点迷茫,因为我不知道你的哪些图像应该是背景,你已经看到了什么图像.所以我猜mBitmap (房间计划)是你的背景吗?而不是将它作为背景图像添加到您的布局中,只需使用ImageView绘制覆盖层.

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

代码语言:javascript
复制
// 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);

如果您的ImageView应该以房间计划为背景,请尝试:

代码语言:javascript
复制
// 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);
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5002722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档