【发布时间】:2011-02-15 10:51:31
【问题描述】:
我想在图像上显示一个点和一个文本。我已经尝试了几个关于覆盖位图的教程,但它似乎不起作用。这是显示背景图像的代码。
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);
【问题讨论】:
-
这很正常,您在绘制位图之前绘制文本并指向。当然你的位图会覆盖一切!
标签: android graphics bitmap overlay