如何在画布内的文本周围绘制一个矩形

时间:2016-09-09 00:27:29

标签: android canvas

我正在尝试在文本周围创建一个矩形,这个文本是在画布中绘制的。画布内也有一个图像。 这是在图像中绘制文本的代码,但我无法获得Rectangle的位置。 这是我需要帮助的一行 canvas.drawRect(HELLLLLLLLLLLLPPPPPPPPPPPPPPPP);

> >  public Bitmap drawTextOnBitmap(Context context, int resId, String
> > text)  {
> >       //  void drawRect(float left, float top, float right, float bottom, Paint paint)
> >         // prepare canvas
> >         int offset=10;
> >         Resources resources = context.getResources();
> >         float scale = resources.getDisplayMetrics().density;
> >         Bitmap bitmap = BitmapFactory.decodeResource(resources, resId);
> > 
> >         android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
> >         // set default bitmap config if none
> >         if (bitmapConfig == null) {
> >             bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
> >         }
> >         // resource bitmaps are immutable, so we need to convert it to mutable one
> >         bitmap = bitmap.copy(bitmapConfig, true);
> >         Canvas canvas = new Canvas(bitmap);
> > 
> >         // new antialiased Paint
> >         TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
> >         // text color - #3D3D3D
> >         paint.setColor(Color.rgb(61, 61, 61));
> >         // text size in pixels
> >         paint.setTextSize((int) (bitmap.getHeight() / 10 * scale));
> >         // text shadow
> >         paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
> > 
> >         // set text width to canvas width minus 16dp padding
> >         int textWidth = canvas.getWidth() - (int) (16 * scale);
> > 
> >         // init StaticLayout for text
> >         StaticLayout textLayout = new StaticLayout(text, paint, textWidth,
> >                 Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
> > 
> >         // get height of multiline text
> >         int textHeight = textLayout.getHeight();
> > 
> >         // get position of text's top left corner
> >         float x = (bitmap.getWidth() - textWidth) / 2;
> >         float y = (bitmap.getHeight() - textHeight) / 2;
> >         Paint myPaint = new Paint();
> >         myPaint.setStyle(Paint.Style.STROKE);
> >         myPaint.setColor(Color.rgb(0, 0, 0));
> >         myPaint.setStrokeWidth(10);
> >         // draw text to the Canvas center
> >         canvas.save();
> > 
> > 
> >         canvas.translate(x, y);
> >         textLayout.draw(canvas);
> >         canvas.drawRect(HELLLLLLLLLLLLPPPPPPPPPPPPPPPP);
> >         canvas.restore();
> > 
> > 
> >         //  void drawRect(float left, float top, float right, float bottom, Paint paint)
> > 
> > 
> >         return bitmap;
> >     }

enter image description here

这就是我想要实现的目标,我坚持在正确的位置创建矩形

3 个答案:

答案 0 :(得分:2)

float w = myPaint.measureText(text,0,text.length();

将此作为宽度和高度使用,并为您的反角提供一些余量

答案 1 :(得分:0)

enter image description here

使用paint.setStyle(Paint.Style.STROKE);

一起
Canvas.drawrect();

它将绘制一个空心矩形。

    Paint paint=new Paint();
    paint.setColor(Color.parseColor("#000000"));
    paint.setStyle(Paint.Style.STROKE);

如果您不知道drawrect()的工作原理,请参阅此帖子: https://stackoverflow.com/a/20919124/6265154

答案 2 :(得分:0)

我已经在这里回答了另一个问题-> Draw text inside a filled rectangle using Canvas Android 希望对您有帮助