android canvas drawText set font size from width?

前端 未结 3 1579
北恋
北恋 2020-12-12 13:36

I want to draw text on canvas of certain width using .drawtext

For example, the width of the text should always be 400px no ma

3条回答
  •  执笔经年
    2020-12-12 14:02

    Here's a much more efficient method:

    /**
     * Sets the text size for a Paint object so a given string of text will be a
     * given width.
     * 
     * @param paint
     *            the Paint to set the text size for
     * @param desiredWidth
     *            the desired width
     * @param text
     *            the text that should be that width
     */
    private static void setTextSizeForWidth(Paint paint, float desiredWidth,
            String text) {
    
        // Pick a reasonably large value for the test. Larger values produce
        // more accurate results, but may cause problems with hardware
        // acceleration. But there are workarounds for that, too; refer to
        // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
        final float testTextSize = 48f;
    
        // Get the bounds of the text, using our testTextSize.
        paint.setTextSize(testTextSize);
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
    
        // Calculate the desired size as a proportion of our testTextSize.
        float desiredTextSize = testTextSize * desiredWidth / bounds.width();
    
        // Set the paint for that size.
        paint.setTextSize(desiredTextSize);
    }
    

    Then, all you need to do is setTextSizeForWidth(paint, 400, str); (400 being the example width in the question).

    For even greater efficiency, you can make the Rect a static class member, saving it from being instantiated each time. However, this may introduce concurrency issues, and would arguably hinder code clarity.

提交回复
热议问题