【问题标题】:How to convert text width in pixels to percent of screen width/height?如何将以像素为单位的文本宽度转换为屏幕宽度/高度的百分比?
【发布时间】:2011-11-17 17:54:36
【问题描述】:

我正在测量一个字符串文本,看看是否应该添加一个\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的屏幕宽度等设置fontSize)
  • 我试图诚实地提醒我自己的问题 -> stackoverflow.com/questions/35117278/…‌​ext-on-a-canvas 我已解决,您可以使用该解决方案来创建此问题的答案。声誉对我来说不是很重要,所以随意收集轻松的 50...
  • @GideonKain 您可以使用布局百分比支持库并可以为小部件提供百分比。你可以在这里查看样本。 androidauthority.com/…

标签: android text text-size


【解决方案1】:

您可以使用以下方法将do转换为像素,将像素转换为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;
}

【讨论】:

【解决方案2】:

您将需要通过 DisplayMetrics 或如何获取屏幕宽度...

要获取字符大小,请使用带边界的绘制来计算。

characterWidth/screenWidth = characterScreenPercentage

我将其设为双精度,但您可以轻松将其更改为浮点数。

对于字符:

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

【讨论】:

    【解决方案3】:
    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。通过这种方式,您可以为 TextView 设置任何字体或 textSize,并且始终知道文本在屏幕上的百分比。

    【讨论】:

    • 好答案,这将比我的建议有用并且在资源上更好。
    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 2014-06-07
    • 2015-06-12
    • 2011-12-05
    • 2014-12-06
    • 2013-06-22
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多