Adjusts the Paint text scale X for the given parameters. - Android Graphics

Android examples for Graphics:Paint

Description

Adjusts the Paint text scale X for the given parameters.

Demo Code


//package com.java2s;

import android.graphics.Paint;
import android.graphics.Rect;

public class Main {
    /**// ww  w .  j  a v a2 s .c  o m
     * 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);
    }
}

Related Tutorials