Code example for TextPaint

Methods: getDesiredWidth, getTextScaleX, setTextScaleX

0
    /** 
     * Set the horizontal stretch factor of the text 
     * @param size Text scale factor 
     */ 
    public void setTextScaleX(float size) {
        if (size != mTextPaint.getTextScaleX()) {
            mTextPaint.setTextScaleX(size);
            measureContent(); 
        } 
    } 
 
    /** 
     * Return the current text alignment setting 
     */ 
    public Layout.Alignment getTextAlign() {
        return mTextAlignment;
    } 
 
    /** 
     * Set the text alignment.  The alignment itself is based on the text layout direction. 
     * For LTR text NORMAL is left aligned and OPPOSITE is right aligned. 
     * For RTL text, those alignments are reversed. 
     * @param align Text alignment value.  Should be set to one of: 
     * 
     *   {@link Layout.Alignment#ALIGN_NORMAL}, 
     *   {@link Layout.Alignment#ALIGN_NORMAL}, 
     *   {@link Layout.Alignment#ALIGN_OPPOSITE}. 
     */ 
    public void setTextAlign(Layout.Alignment align) {
        if (mTextAlignment != align) {
            mTextAlignment = align;
            measureContent(); 
        } 
    } 
 
    /** 
     * Sets the typeface and style in which the text should be displayed. 
     * Note that not all Typeface families actually have bold and italic 
     * variants, so you may need to use 
     * {@link #setTypeface(Typeface, int)} to get the appearance 
     * that you actually want. 
     */ 
    public void setTypeface(Typeface tf) {
        if (mTextPaint.getTypeface() != tf) {
            mTextPaint.setTypeface(tf);
 
            measureContent(); 
        } 
    } 
 
    /** 
     * Sets the typeface and style in which the text should be displayed, 
     * and turns on the fake bold and italic bits in the Paint if the 
     * Typeface that you provided does not have all the bits in the 
     * style that you specified. 
     * 
     */ 
    public void setTypeface(Typeface tf, int style) {
        if (style > 0) {
            if (tf == null) {
                tf = Typeface.defaultFromStyle(style);
            } else { 
                tf = Typeface.create(tf, style);
            } 
 
            setTypeface(tf);
            // now compute what (if any) algorithmic styling is needed 
            int typefaceStyle = tf != null ? tf.getStyle() : 0;
            int need = style & ~typefaceStyle;
            mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
            mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
        } else { 
            mTextPaint.setFakeBoldText(false);
            mTextPaint.setTextSkewX(0);
            setTypeface(tf);
        } 
    } 
 
    /** 
     * Return the current typeface and style that the Paint 
     * using for display. 
     */ 
    public Typeface getTypeface() {
        return mTextPaint.getTypeface();
    } 
 
    /** 
     * Set a single text color for all states 
     * @param color Color value such as {@link Color#WHITE} or {@link Color#argb(int, int, int, int)} 
     */ 
    public void setTextColor(int color) {
        setTextColor(ColorStateList.valueOf(color));
    } 
 
    /** 
     * Set the text color as a state list 
     * @param colorStateList ColorStateList of text colors, such as inflated from an R.color resource 
     */ 
    public void setTextColor(ColorStateList colorStateList) {
        mTextColors = colorStateList;
        updateTextColors(getState());
    } 
 
    /** 
     * Optional Path object on which to draw the text.  If this is set, 
     * TextDrawable cannot properly measure the bounds this drawable will need. 
     * You must call {@link #setBounds(int, int, int, int) setBounds()} before 
     * applying this TextDrawable to any View. 
     * 
     * Calling this method with <code>null</code> will remove any Path currently attached. 
     */ 
    public void setTextPath(Path path) {
        if (mTextPath != path) {
            mTextPath = path;
            measureContent(); 
        } 
    } 
 
    /** 
     * Internal method to take measurements of the current contents and apply 
     * the correct bounds when possible. 
     */ 
    private void measureContent() { 
        //If drawing to a path, we cannot measure intrinsic bounds 
        //We must resly on setBounds being called externally 
        if (mTextPath != null) {
            //Clear any previous measurement 
            mTextLayout = null;
            mTextBounds.setEmpty();
        } else { 
            //Measure text bounds 
            float desired = Layout.getDesiredWidth(mText, mTextPaint);
            mTextLayout = new DynamicLayout(mText, mTextPaint, (int)desired,
                    mTextAlignment, 1.3f, 1.0f, false);
            //Log.i("tttt", "Ancho: " + desired + " textSize: " + getTextSize() 
            //        +" textLayoutWidth: " +mTextLayout.getWidth()); 
            mTextBounds.set(0, 0, mTextLayout.getWidth(), mTextLayout.getHeight());