Android examples for Graphics:Paint
Adjusts the Paint font size of the given paint for the given string, to match the given width AND height.
//package com.java2s; import android.graphics.Paint; import android.graphics.Rect; public class Main { /**/* w w w . j av a 2 s . com*/ * Adjusts the font size of the given paint for the given string, * to match the given width AND height. * * The original ratio of the font is NOT saved! The text is stretched horizontally to fill the whole width. * * @param text * @param textPaint * @param width * @param height */ public static void adjustTextSizeToWidthAndHeight(String text, Paint textPaint, int width, int height) { adjustTextSizeForHeight(text, textPaint, height); adjustTextScale(text, textPaint, width); } /** * Adjusts the text size for the given parameters. Called from adjustTextSize(). * * @param text * @param textPaint * @param height */ public static void adjustTextSizeForHeight(String text, Paint textPaint, int height) { textPaint.setTextSize(100); textPaint.setTextScaleX(1.0f); Rect bounds = new Rect(); // ask the paint for the bounding rect if it were to draw this text textPaint.getTextBounds(text, 0, text.length(), bounds); // get the height that would have been produced int h = bounds.bottom - bounds.top; // make the text text up 100% of the height float target = (float) height * 1.0f; // figure out what textSize setting would create that height of text float size = ((target / h) * 100f); // and set it into the paint textPaint.setTextSize(size); } /** * Adjusts the text scale X for the given parameters. Called from adjustTextSize(). * * @param text * @param textPaint * @param width */ public static void adjustTextScale(String text, Paint textPaint, int width) { // do calculation with scale of 1.0 (no scale) textPaint.setTextScaleX(1.0f); Rect bounds = new Rect(); // ask the paint for the bounding rect if it were to draw this text. textPaint.getTextBounds(text, 0, text.length(), bounds); // determine the width int w = bounds.right - bounds.left; // determine how much to scale the width to fit the view float xscale = ((float) width) / w; textPaint.setTextScaleX(xscale); } }