如何实现FontFitTextView

时间:2015-12-16 13:05:35

标签: java android android-layout

我正在尝试从另一篇文章实现FontFitTextView.java类。但是,我试过,字体不会扩展到其容器。在测试中,它只有一个非常小的字体大小,另外,当文本大小改变时它也不会增加。我看到该类有其他函数接受像textwidth这样的参数,但我不知道从哪里得到它(容器可能?)。

How to adjust text font size to fit textview

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

public class FontFitTextView extends TextView {

public FontFitTextView(Context context) {
    super(context);
    initialise();
}

public FontFitTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialise();
}

private void initialise() {
    mTestPaint = new Paint();
    mTestPaint.set(this.getPaint());
    //max size defaults to the initially specified text size unless it is too small
}

/* Re size the font so the specified text fits in the text box
 * assuming the text box is the specified width.
 */
private void refitText(String text, int textWidth) 
{ 
    if (textWidth <= 0)
        return;
    int targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
    float hi = 100;
    float lo = 2;
    final float threshold = 0.5f; // How close we have to be

    mTestPaint.set(this.getPaint());

    while((hi - lo) > threshold) {
        float size = (hi+lo)/2;
        mTestPaint.setTextSize(size);
        if(mTestPaint.measureText(text) >= targetWidth) 
            hi = size; // too big
        else
            lo = size; // too small
    }
    // Use lo so that we undershoot rather than overshoot
    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int height = getMeasuredHeight();
    refitText(this.getText().toString(), parentWidth);
    this.setMeasuredDimension(parentWidth, height);
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
    refitText(text.toString(), this.getWidth());
}

@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    if (w != oldw) {
        refitText(this.getText().toString(), w);
    }
}

//Attributes
private Paint mTestPaint;
}

我在OnCreate中使用的内容:

FontFit = (FontFitTextView) findViewById(R.id.textViewFit); 
FontFit.setText("MEME");

我确保在Layout.xml中使用自定义视图

<com.example.Rotation.FontFitTextView

1 个答案:

答案 0 :(得分:0)

这是工作代码..

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontFitTextView extends TextView {

public FontFitTextView(Context context) {
    super(context);
    initialise();
}

public FontFitTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialise();
}

private void initialise() {
    testPaint = new Paint();
    testPaint.set(this.getPaint());
    //max size defaults to the intially specified text size unless it      is    too small
    maxTextSize = this.getTextSize();
    if (maxTextSize < 11) {
        maxTextSize = 20;
    }
    minTextSize = 10;
}

/* Re size the font so the specified text fits in the text box
 * assuming the text box is the specified width.
 */
private void refitText(String text, int textWidth) { 
    if (textWidth > 0) {
        int availableWidth = textWidth - this.getPaddingLeft() -           this.getPaddingRight();
        float trySize = maxTextSize;

        testPaint.setTextSize(trySize);
        while ((trySize > minTextSize) && (testPaint.measureText(text) > availableWidth)) {
            trySize -= 1;
            if (trySize <= minTextSize) {
                trySize = minTextSize;
                break;
            }
            testPaint.setTextSize(trySize);
        }

        this.setTextSize(trySize);
    }
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
    refitText(text.toString(), this.getWidth());
}

@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    if (w != oldw) {
        refitText(this.getText().toString(), w);
    }
}

//Getters and Setters
public float getMinTextSize() {
    return minTextSize;
}

public void setMinTextSize(int minTextSize) {
    this.minTextSize = minTextSize;
}

public float getMaxTextSize() {
    return maxTextSize;
}

public void setMaxTextSize(int minTextSize) {
    this.maxTextSize = minTextSize;
}

//Attributes
private Paint testPaint;
private float minTextSize;
private float maxTextSize;

}