Draw text inside a filled rectangle using Canvas Android

前端 未结 3 1988
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 02:26

How to draw a filled rectangle with specified bounds and inside that rectangle text to be drawn using Canvas Android ?? I tried

mPaint.setColor(Color.GREEN)         


        
相关标签:
3条回答
  • 2021-01-03 02:34

    This might be very late for this particular query but I think many will find this answer useful. So, the problem with the Canvas for any CustomView is that, you can get the width for a particular text, but it's not that easy to get the height of the text. Also if you are using canvas.drawText(....) with simple Paint object, you can not draw multi line text. So, use the below code within your onDraw() method.

    String displayText = "Hello World";
    int mainTextPositionX = getWidth() / 2 ;
    int mainTextPositionY = getHeight() / 2;
    
    StaticLayout textStaticLayout;
    TextPaint textPaint;
    textPaint = new TextPaint();
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setColor(Color.BLUE);
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(convertDpToPixel(30, context));
    textPaint.setTextAlign(Paint.Align.CENTER);
    highlightedRectPaint = new Paint();
    
    highlightedRectPaint.setStrokeWidth(12);
    highlightedRectPaint.setStyle(Paint.Style.STROKE);
    highlightedRectPaint.setColor(Color.RED);
    highlightedRectPaint.setAntiAlias(true);
    
    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            textStaticLayout = StaticLayout
                    .Builder
                    .obtain(displayText, 0, displayText.length(), textPaint, (int) textPaint.measureText(displayText))
                    .build();
        }else{
            textStaticLayout = new StaticLayout(
                    displayText, textPaint, (int)textPaint.measureText(displayText), Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        }
    
        Rect highlightedTextBorderRect = new Rect();
        highlightedTextBorderRect.top = mainTextPositionY-20;
        highlightedTextBorderRect.left = mainTextPositionX- 
        ((int)textPaint.measureText(displayText)/2)-20;
        highlightedTextBorderRect.right = mainTextPositionX+ 
        ((int)textPaint.measureText(displayText)/2) + 20;
        highlightedTextBorderRect.bottom = mainTextPositionY+ 
        (int)textStaticLayout.getHeight()+20;
    
        canvas.save();
        canvas.translate(mainTextPositionX, mainTextPositionY);
        textStaticLayout.draw(canvas);
        canvas.restore();
    
        canvas.drawRect(highlightedTextBorderRect,highlightedRectPaint);
    

    just make sure that, you declare all the objects and variable outside of the draw() method. And this will draw a rectangle outline around the text with multi line support. If you want the rectangle to have a fill, then just use the highlightedRectPaint and change the setStyle(Paint.Style.FILL). Hope that helps.

    0 讨论(0)
  • Here i have hardcoded x and y values. You can change them

            mpaint= new Paint();
            mpaint.setColor(Color.RED);
            mpaint.setStyle(Style.FILL);
            paint2= new Paint();
            paint2.setColor(Color.GREEN);
            paint2.setTextSize(50);  //set text size
            float w = paint2.measureText(s)/2;
            float textSize = paint2.getTextSize();
    
    
            @Override
            protected void onDraw(Canvas canvas) {
                paint2.setTextAlign(Paint.Align.CENTER);
                canvas.drawRect(300-w, 300 - textsize, 300 + w, 300, mpaint);
                canvas.drawText(s, 300, 300 ,paint2); //x=300,y=300    
            }
    

    Edit :

    Its bad a idea to call measureText in onDraw. You can do that outside of onDraw.

    There is a video on also about performance and why you should avoid allocations in onDraw. https://www.youtube.com/watch?v=HAK5acHQ53E

    Resulting snap shot

    enter image description here

    0 讨论(0)
  • 2021-01-03 02:42

    If you have to center the text inside de rect you have use this code

        mpaint= new Paint();
        mpaint.setColor(Color.RED);
        mpaint.setStyle(Style.FILL);
        paint2= new Paint();
        paint2.setColor(Color.GREEN);
        paint2.setTextSize(50);  //set text size
        float w = paint2.measureText(s)/2;
        float textSize = paint2.getTextSize();
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            paint2.setTextAlign(Paint.Align.CENTER);
            Rect rect = new Rect(300-w, 300 - textsize, 300 + w, 300);
            canvas.drawRect(rect, mpaint);
            canvas.drawText(s, rect.centerX(), rect.centerY() ,paint2); // center text inside rect
        }
    

    0 讨论(0)
提交回复
热议问题