如何将文本宽度从像素转换为屏幕宽度/高度的百分比?

4

我正在测量一个字符串文本,以确定是否应该添加\n进行换行。

我正在使用Paint.measureText函数,它大多数时候都有效,但是我意识到这不准确,因为px并不等于dp - 我已经在网上看到如何将像素转换为dp,但我更想将px转换为屏幕大小的百分比。

如果a宽度为8像素,我应该怎么说:

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

这样我就可以看到:
 if (LineOfTextWidth >= ScreenWidth * 0.9f) //90%

这将是一个非常有用的函数,方便使用。

@CrandellWS,请帮我理解,您从这个问题中究竟想要什么?难道不只是通过getWindowManager().getDefaultDisplay().getMetrics(metrics)计算屏幕宽度并将测量的文本宽度除以它吗?(关于您自己提到的问题,只需根据屏幕上“dp”的宽度设置字体大小等即可。) - Konstantin Loginov
我想诚实地引起对我的问题的关注 -> http://stackoverflow.com/questions/35117278/canvas-drawtext-with-pixel-perfect-t‌​ext-on-a-canvas,我已经解决了这个问题,你可以使用那个解决方案来回答这个问题。声望对我来说并不是非常重要,所以请随意获得简单的50... - CrandellWS
1
@GideonKain 你可以使用布局百分比支持库,并且可以为小部件设置百分比。你可以在这里查看示例。 http://www.androidauthority.com/using-the-android-percent-support-library-630715/ - Wasim K. Memon
3个回答

1
您需要通过DisplayMetrics或其他方式获取屏幕宽度...
要获取字符大小,请使用带有边界的paint进行计算。 characterWidth/screenWidth = characterScreenPercentage 我将其设为double,但您可以轻松更改为float。
对于字符:
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);
}

对于字符串:

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);
}

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);
}

在屏幕上绘制TextView之前(或代替),您可以测量其中包含文本的大小。通过这种方式,您可以为TextView设置任何字体或文本大小,并始终知道文本在屏幕上占多少百分比。


好的回答,比我的建议更有用且资源消耗更少。 - CrandellWS

0
你可以使用以下方法将dp转换为像素和像素转换为dp:
将dp转换为像素:
public int dpToPixel(int dp) {
    DisplayMetrics dm= getContext().getResources().getDisplayMetrics();
    int pixel = Math.round(dp * (dm.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return pixel
}

将像素转换为 dp:

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

你需要使用边界来获取像素高度和宽度。 - CrandellWS
尝试重新修改你的答案,也许这会有所帮助。https://dev59.com/LVvUa4cB1Zd3GeqPsEyK#35193695 - CrandellWS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接