简体   繁体   中英

Using breakText, what value should maxWidth have?

In my current code i'm getting the width of the screen and using it in my breakText as the maxWidth.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;

    Paint p = new Paint();
    p.setTextSize(60);
    p.setSubpixelText(true);

endChar = p.breakText(fullText, 0, endText, true, width, null);

This code isn't giving me the desired answers, it is giving more characters than needed, but i guess I'm using the maxWidth wrongly. Can someone explain what should be entered in maxWidth?

Remember about density of screen and scale factor for text on device. You should divide your screen width in px by scaledDensity to get screen width in sp.

It should look like that:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int width = (int)((float)size.x/metrics.scaledDensity);

    Paint p = new Paint();
    p.setTextSize(60);
    p.setSubpixelText(true);

    endChar = p.breakText(fullText, 0, endText, true, width, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM