How to draw text on canvas?

前端 未结 3 1616
南笙
南笙 2020-12-13 18:28

i\'m trying to develop a simple pie chart class for android. For now, it can take a map of labels and values and draw the pie chart. I\'m yet to add the legends for the pie,

相关标签:
3条回答
  • 2020-12-13 18:42

    Another (arguably better) way to draw text on a canvas is to use a StaticLayout. This handles multiline text when needed.

    String text = "This is some text.";
    
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
    textPaint.setColor(0xFF000000);
    
    int width = (int) textPaint.measureText(text);
    StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
    staticLayout.draw(canvas);
    

    The TextPaint and StaticLayout were instantiated right before being used here for the sake of illustration. Doing so in onDraw would hurt performance, though. Here is a better example showing them in the context of a custom view that draws it's own text.

    0 讨论(0)
  • 2020-12-13 18:44

    There used to be another answer here that got deleted because it was a link only. The original link is here. The code is basically the same, but I took out the non text drawing portions and also scaled up the sizes to work better on modern screen densities.

    This just shows a few things you can do with text drawing.

    Here is the updated code:

    public class MainActivity extends AppCompatActivity {
    
        DemoView demoview;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            demoview = new DemoView(this);
            setContentView(demoview);
        }
    
        private class DemoView extends View {
            public DemoView(Context context){
                super(context);
            }
    
            @Override protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
    
                // custom drawing code here
                // remember: y increases from top to bottom
                // x increases from left to right
                int x = 0;
                int y = 0;
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
    
                canvas.save();
                canvas.translate(100, 200);
    
                // make the entire canvas white
                canvas.drawColor(Color.WHITE);
    
                // draw some text using STROKE style
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(1);
                paint.setColor(Color.MAGENTA);
                paint.setTextSize(100);
                canvas.drawText("Style.STROKE", 0, 0, paint);
    
                canvas.translate(0, 200);
    
                // draw some text using FILL style
                paint.setStyle(Paint.Style.FILL);
                //turn antialiasing on
                paint.setAntiAlias(true);
                //paint.setTextSize(30);
                canvas.drawText("Style.FILL", 0, 0, paint);
    
                canvas.translate(0, 200);
    
                // draw some rotated text
                // get text width and height
                // set desired drawing location
                x = 75;
                y = 185;
                paint.setColor(Color.GRAY);
                //paint.setTextSize(25);
                String str2rotate = "Rotated!";
    
                // draw bounding rect before rotating text
                Rect rect = new Rect();
                paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
                canvas.translate(x, y);
                paint.setStyle(Paint.Style.FILL);
                // draw unrotated text
                canvas.drawText("!Rotated", 0, 0, paint);
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawRect(rect, paint);
                // undo the translate
                canvas.translate(-x, -y);
    
                // rotate the canvas on center of the text to draw
                canvas.rotate(-45, x + rect.exactCenterX(),
                        y + rect.exactCenterY());
                // draw the rotated text
                paint.setStyle(Paint.Style.FILL);
                canvas.drawText(str2rotate, x, y, paint);
    
                //undo the translation and rotation
                canvas.restore();
            }
        }
    }
    

    Something else that I want to try later is drawing text along a path.

    See also this fuller answer here that gives the following image.

    0 讨论(0)
  • 2020-12-13 18:45

    You will have to use the drawText method of the Canvas class.

    Paint paint = new Paint(); 
    canvas.drawPaint(paint); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(16); 
    canvas.drawText("My Text", x, y, paint); 
    

    Here's the relevant documentation about it:

    http://developer.android.com/reference/android/graphics/Canvas.html#drawText(java.lang.String, float, float, android.graphics.Paint)

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