很多时候我们需要文字大小随控件的宽变小,以保证文字能完整显示,现自定义控件如下:
package com.v.vutils.views;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
public class FitTextView extends TextView {
private Paint mTextPaint;
private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小
private float mMinTextSize = 8;
public FitTextView(Context context) {
this(context, null);
}
public FitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setGravity(getGravity() | Gravity.CENTER_VERTICAL); // 默认水平居中
setLines(1);
initialise();
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
refitText(text.toString(), this.getWidth());
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
private void initialise() {
mTextPaint = new TextPaint();
mTextPaint.set(this.getPaint());
// max size defaults to the intially specified text size unless it is too small
mMaxTextSize = this.getTextSize();
// mMinTextSize = 8;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw) {
refitText(this.getText().toString(), w);
}
}
/**
* Resize 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 = mMaxTextSize;
mTextPaint.setTextSize(trySize);
while (mTextPaint.measureText(text) > availableWidth) {
trySize -= 1;
if (trySize <= mMinTextSize) {
trySize = mMinTextSize;
break;
}
mTextPaint.setTextSize(trySize);
}
// setTextSize参数值为sp值
setTextSize(px2sp(getContext(), trySize));
}
}
/**
* 将px值转换为sp值,保证文字大小不变
*/
public static float px2sp(Context context, float pxValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (pxValue / fontScale);
}
}
xml中使用,同TextView使用完全一样:
<com.v.vutils.views.FitTextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="80000000"
android:textSize="36sp" />