Java Android Canvas.drawText (...) is equally large on all screens

I drew text with canvas.drawText(...) in my project and tested it using different devices.

However, the size of the text varies greatly from screen to screen.

I tried multiplying it by getResources().getDisplayMetrics().density , but they are still a bit not the same size.

Is there a way to make text have the same size on different devices?

+1
source share
4 answers

hi, you can watch this post, http://catchthecows.com/?p=72 , I think it helps you. @Override protected void onSizeChanged (int w, int h, int oldw, int oldh) {super.onSizeChanged (w, h, oldw, oldh);

  // save view size mViewWidth = w; mViewHeight = h; // first determine font point size adjustTextSize(); // then determine width scaling // this is done in two steps in case the // point size change affects the width boundary adjustTextScale(); } void adjustTextSize() { mTextPaint.setTextSize(100); mTextPaint.setTextScaleX(1.0f); Rect bounds = new Rect(); // ask the paint for the bounding rect if it were to draw this // text mTextPaint.getTextBounds(mText, 0, mText.length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // make the text text up 70% of the height float target = (float)mViewHeight*.7f; // figure out what textSize setting would create that height // of text float size = ((target/h)*100f); // and set it into the paint mTextPaint.setTextSize(size); } void adjustTextScale() { // do calculation with scale of 1.0 (no scale) mTextPaint.setTextScaleX(1.0f); Rect bounds = new Rect(); // ask the paint for the bounding rect if it were to draw this // text. mTextPaint.getTextBounds(mText, 0, mText.length(), bounds); // determine the width int w = bounds.right - bounds.left; // calculate the baseline to use so that the // entire text is visible including the descenders int text_h = bounds.bottom-bounds.top; mTextBaseline=bounds.bottom+((mViewHeight-text_h)/2); // determine how much to scale the width to fit the view float xscale = ((float) (mViewWidth-getPaddingLeft()-getPaddingRight())) / w; // set the scale for the text paint mTextPaint.setTextScaleX(xscale); } @Override protected void onDraw(Canvas canvas) { // let the ImageButton paint background as normal super.onDraw(canvas); // draw the text // position is centered on width // and the baseline is calculated to be positioned from the // view bottom canvas.drawText(mText, mViewWidth/2, mViewHeight-mTextBaseline, mTextPaint); } 

https://github.com/catchthecows/BigTextButton

+1
source

What is this

GetResources (). GetDisplayMetrics (). Density

this is what it makes it look like the same screen sizes with different densities. Appreciate your move.

There are two solutions to your problem. 1. Try having large, large, and normal layouts. Customize them accordingly to the appropriate layouts to look even.

  • Get the percentage filling of the screen (height / width and right / left / top / bottom of the canvas as a percentage for uniform alignment on different screens) of the canvas. And apply it as layoutParams.
0
source

The Paint class takes the size of the text in pixels. If you are going to draw text directly through it, you need to handle unit conversions before calling paint.setTextSize (float size). Always use sp or dp blocks, and you can include at least separate dimensions.xml resources for small, normal, large, and xlarge screens.

0
source

When Canvas.drawText() called, the size of the text is first determined by the Canvas.drawText() passed in the Paint object, which can be set via Paint.setTextSize() . The text size is automatically scaled by Canvas based on the density of the canvas, which can be found using Canvas.getDensity() .

When adjusting the text size for a drawing object to be drawn on Canvas, work with a single dp or sp value and let Canvas handle the scaling for you.

And, of course, you may need to specify different values โ€‹โ€‹depending on the screen size: Multi -screen support

0
source

All Articles