简体   繁体   中英

Scale font size on android wear to fully utilize screen resolution

Super simple question: what is the relationship of font size to screen resolution on adroid? AVD? Eg if resolution width is 100px and a TextView has 10 monospaced chars in it, if text size is 10px, can i guarantee that the Text will occupy the full screen width?

My specific problem is that I need to scale 10 characters of text, using textSize, to use the full screen width (using textSize alone). If screen resolution is 100 x 100 and I want 10 characters to fully occupy the screen horizontally, why can't I just use:

 DisplayMetrics metrics = getResources().getDisplayMetrics();
 float pixelFontSize = metrics.widthPixels / 10f;
 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, pixelFontSize);

? In my testing, I get text that is way to small (uses about 1/3 the resolution) on a 280 x 280 wtih 280 dpi AVD, yet on another AVD 360 x 330 hdpi, the code above produce text way too big to fit on one line. ...I mean, sure I could see needing to subtract a small amount from the pixcelFontSize to account for text layout, but my results are all over the place. I must be missing something. First I was messing with density and the scaled measurement dp and sp, but logic would say that given my problem, density has nothing to do with it at all, right?

EDIT, to further show the problem, here is my solution to scale the text to fit the display, using font size, for 7 or 8 AVD's all wear, square round and chin

 switch (metrics.widthPixels) {
            case 280: //280 dpi

                    resolutionMultiplier = .4f;
                    ideoResolutionMultiplier = 0f;

                break;
            case 360: //hdpi
                resolutionMultiplier = .5f;
                ideoResolutionMultiplier = .0f;
                break;
            case 320: //works for round hdpi, chin tvdpi, but the res 320 square 280 dpi too tall....
                if(metrics.xdpi == 280) { 
                    resolutionMultiplier = .45f;
                    ideoResolutionMultiplier = 0f;
                }
                else {
                    resolutionMultiplier = .47f;
                    ideoResolutionMultiplier = 0f;
                }
                break;
            case 400: //280 dpi
                resolutionMultiplier = .55f;
                ideoResolutionMultiplier = 0f;
                break;
            case 480: //360 dpi
                resolutionMultiplier = .71f;
                ideoResolutionMultiplier = 0f;
                break;

        }

        if (mWzTheme.mWordArrays != null
                && mWzTheme.mWordArrays.isIdeographic()) {
            pixelFontSize = (metrics.widthPixels / new Integer(Constants.MAX_LINE_LENGTH_CHINESE_WATCH).floatValue())
                    + (new Integer(Constants.MAX_LINE_LENGTH_WATCH).floatValue() * ideoResolutionMultiplier);
        }
        else {
            pixelFontSize = (metrics.widthPixels / new Integer(Constants.MAX_LINE_LENGTH_WATCH).floatValue())
                    + (new Integer(Constants.MAX_LINE_LENGTH_WATCH).floatValue() * resolutionMultiplier);
        }

Try this library:

https://github.com/AndroidDeveloperLB/AutoFitTextView

This will fit the text to the screen exactly. (Not sure if there were any issues with Android Wear target.)

Here is a simple solution:

Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();

...but still need a guessing algorithm to go from screen resolution to the font size required to fill the screen with x characters...

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