Code example for Paint

Methods: measureTextsetsetTextSize

0
 
            if (context != null) {
                r = context.getResources();
            } 
 
            mPaint.set(getPaint());
            mPaint.setTextSize(newTextSize);
 
            if (mPaint.measureText(text) > targetWidth) {
                newTextSize = getTextSize(r, text, targetWidth, low, high);
 
                if (newTextSize < mMinTextSize) {
                    newTextSize = mMinTextSize;
                } 
            } 
 
            setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize);
 
        } 
    } 
 
    // Recursive binary search to find the best size for the text 
    private float getTextSize(Resources resources, String text, float targetWidth, float low, float high) {
        float mid = (low + high) / 2.0f;
 
        mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, resources.getDisplayMetrics()));
        float textWidth = mPaint.measureText(text);
 
        if (SPEW) Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " width=" + textWidth);
 
        if ((high - low) < mPrecision) {
            return low;
Contextual code suggestions in your IDE  Get Codota for Java