1035

Paint.getTextBounds() returns to big height

Question:

<strong>EDIT: The problem came from the emulator, the error did not appear on a real device :(</strong>

I'm trying to draw some text in a custom view and must there for measure it but the value of the Paint.getTextBounds() returns a height which is about 30% higher then the actual text which gives everything a quirky look.

I found this: <a href="https://stackoverflow.com/questions/7549182/android-paint-measuretext-vs-gettextbounds" rel="nofollow">Android Paint: .measureText() vs .getTextBounds()</a> and tried to add the solution code to my own onDraw and saw that i the same measuring error as in my code. Here is a picture of the result: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/mliLV.png" data-original="https://i.stack.imgur.com/mliLV.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Compare with: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/CPXhA.png" data-original="https://i.stack.imgur.com/CPXhA.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /> The image is copied from <a href="https://stackoverflow.com/questions/7549182/android-paint-measuretext-vs-gettextbounds" rel="nofollow">Android Paint: .measureText() vs .getTextBounds()</a>

Note the spacing above the text in the first picture. Any Ideas what might be causing this? Or are there alternative ways to measure height of a drawn string?

Here is the onDraw method:

@Override public void onDraw(Canvas canvas){ // canvas.drawColor(color_Z1); // r.set(0, 0, (int)(width*progress), height); // paint.setColor(color_Z2); //// canvas.drawRect(r, paint); // textPaint.getTextBounds(text, 0, text.length(), r); // canvas.drawRect(r, paint); // canvas.drawText(text, 0, r.height(), textPaint); final String s = "Hello. I'm some text!"; Paint p = new Paint(); Rect bounds = new Rect(); p.setTextSize(60); p.getTextBounds(s, 0, s.length(), bounds); float mt = p.measureText(s); int bw = bounds.width(); Log.i("LCG", String.format( "measureText %f, getTextBounds %d (%s)", mt, bw, bounds.toShortString()) ); bounds.offset(0, -bounds.top); p.setStyle(Style.STROKE); canvas.drawColor(0xff000080); p.setColor(0xffff0000); canvas.drawRect(bounds, p); p.setColor(0xff00ff00); canvas.drawText(s, 0, bounds.bottom, p); }

Answer1:

i didnot test your code but i dont see any problems with Paint.getTextBounds():

public class TextBoundsTest extends View { private Paint paint; private Rect bounds; public TextBoundsTest(Context context) { super(context); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(32); bounds = new Rect(); } @Override protected void onDraw(Canvas canvas) { String text = "this is my text"; paint.getTextBounds(text, 0, text.length(), bounds); Log.d(TAG, "onDraw " + bounds); int x = (getWidth() - bounds.width()) / 2; int y = 70; paint.setColor(0xff008800); bounds.offset(x, y); canvas.drawRect(bounds, paint); paint.setColor(0xffeeeeee); canvas.drawText(text, x, y, paint); } }

add this in Activity.onCreate:

TextBoundsTest view = new TextBoundsTest(this); setContentView(view);

the result is: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/1wbqK.png" data-original="https://i.stack.imgur.com/1wbqK.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Recommend

  • What happened to Microsoft.FSharp.Math.Matrix?
  • How to get image from drawable-xhdpi
  • OCaml: get value's type name
  • Does the jQuery $(window).load(); event not fire on pages without a declaration? (…in a chrome exte
  • Draw multiple rectangles android canvas
  • Not logged in after manual login in CakePHP if redirected, logged in if no redirect
  • Closing current window when opening another window
  • Excel: search if a specific text exists in a column
  • Cancel a long task that's managed by a web service
  • Sending EoS to filesink while removing branch from tee
  • Substatemachine
  • How to enrich a TraversableOnce with my own generic map?
  • WooCommerce Free Shipping - Remove raw or change the text name on checkout and email
  • cq5 accessing osgi servlet through Url
  • Selenium 2 : finding web element locators programatically
  • Unreadable characters displaying in ASP.NET MVC
  • Split an Array into 3 arrays [duplicate]
  • Integrating Yelp API v2 into iOS 7 app
  • Android how to share data with Facebook Open Graph in new sdk 3.0?
  • Getting CKEditor to work with Flask Admin
  • How can I access the Google account user_id?
  • Working with codeception and laravel
  • Trigger powershell based on event log
  • Python tk scrollbar becomes inactive once text is outside the screen
  • MayAVI install on Python 3.6 [duplicate]
  • Adding horizontal slider to QTableWidget
  • Arraylist of strings into one comma separated string
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • How to make Rss News Reader application in android …? [closed]
  • How to run chrome.tabs.insertCSS from the background page on each page?
  • How to merge objects within array based on attribute
  • Runtime complexity of getting the length of a string in different representations
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to handle div that is created dynamically in a table
  • Bind selectedDates Aggregation for Calendar
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • ssh remote server login script
  • Computing the discrete fourier transform of audio data with FFTW