4

I am measuring a string text to see if I should add a \n for wrapping.

I am using the Paint.measureText function, and it works most of the time but it occurs to me that this isn't accurate since a px doesn't equal a dp - I've seen online how to convert pixels to dp, but instead what I would rather do is convert px to a percentage of the screen size for example

If a is 8 pixels wide, how would I say:

float LetterWidthPercent = _______ //get width of character in percent of screen width
float LetterHeightPercent = _______ //get height of character in percent of screen height

This way I could just see:

 if (LineOfTextWidth >= ScreenWidth * 0.9f) //90%

This would be an extremely useful function to have handy.

double-beep
  • 5,031
  • 17
  • 33
  • 41
GideonKain
  • 744
  • 1
  • 12
  • 27
  • @CrandellWS help me to understand, what exactly do you want from this question? Isn't it just count width of the screen by `getWindowManager().getDefaultDisplay().getMetrics(metrics)` and dividing measured text-width by it? (regarding your own question you mentioned, just set fontSize based on the `dp`'s width of the screen, etc.) – Konstantin Loginov Feb 01 '16 at 18:28
  • I was trying to draw attention to My own question honestly -> http://stackoverflow.com/questions/35117278/canvas-drawtext-with-pixel-perfect-t‌​ext-on-a-canvas which I solved and you can use that solution to create the answer for this question. The reputation is not super important to me so feel free to collect easy 50... – CrandellWS Feb 01 '16 at 18:38
  • 1
    @GideonKain you can use layout percentage support library and can give percentage to widgets. you can check out sample here. http://www.androidauthority.com/using-the-android-percent-support-library-630715/ – Wasim K. Memon Feb 04 '16 at 08:37

3 Answers3

1

You will need to get the screen width through DisplayMetrics or how ever...

For getting the character size use paint with bounds to calculate.

characterWidth/screenWidth = characterScreenPercentage

I made it a double but you can easily change it to a float.

For Characters:

public Double characterToScreenWidthPercentage(Character c) {
    Paint paint = new Paint();
    Rect boundA = new Rect();
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getMetrics(metrics);
    paint.getTextBounds(Character.toString(c), 0, 1, boundA);
    Log.v("fn", Integer.toString(boundA.width()));
    Log.v("fn", Integer.toString(metrics.widthPixels));
    Log.v("fn", Double.toString((float) boundA.width() / (float) metrics.widthPixels));
    return ((double) boundA.width() / (double) metrics.widthPixels);
}

For strings:

public Double stringToScreenWidthPercentage(String str) {
    Paint paint = new Paint();
    Rect boundA = new Rect();
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getMetrics(metrics);
    paint.getTextBounds(str, 0, 1, boundA);
    Log.v("fn", Integer.toString(boundA.width()));
    Log.v("fn", Integer.toString(metrics.widthPixels));
    Log.v("fn", Double.toString((float) boundA.width() / (float) metrics.widthPixels));
    return ((double) boundA.width() / (double) metrics.widthPixels);
}
CrandellWS
  • 2,708
  • 5
  • 49
  • 111
1
public static double measureWeightPercent(Context context, String textToMeasure) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay().getMetrics(metrics);

    TextView view = new TextView(context);
    view.setText(textToMeasure);
    view.measure(metrics.widthPixels, metrics.heightPixels);

    double textWidth = view.getMeasuredWidth();

    return textWidth / (metrics.widthPixels / 100);
}

You can measure a TextView with text inside before(or instead) drawing it on a screen. At this way you can set any font or textSize for TextView and always know how many percents the text will have on the screen.

0

You can use following methods to convert do to pixel and pixel to dp.

Convert dp to pixel:

public int dpToPixel(int dp) {
    DisplayMetrics dm= getContext().getResources().getDisplayMetrics();
    int pixel = Math.round(dp * (dm.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return pixel
}

Convert pixel to dp:

public int pixelToDp(int pixel) {
    DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(pixel / (dm.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}
thiagolr
  • 6,909
  • 6
  • 44
  • 64
Rakesh
  • 756
  • 1
  • 9
  • 19