//设置minsize 自动缩放大小
public class AutoScaleTextView extends AppCompatTextView {
private int size_max;
private int size_min;
private Context context;
public AutoScaleTextView(Context context) {
super(context, null);
}
public AutoScaleTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.autoScaleTextViewStyle);
this.context = context;
}
public AutoScaleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoScaleTextView, defStyleAttr, 0);
this.size_min = (int) a.getDimension(R.styleable.AutoScaleTextView_minTextSize, 10f);
a.recycle();
this.size_max = (int) this.getTextSize();
}
/**
* 设置最小的size
*
* @param size_min
*/
public void setSize_min(int size_min) {
this.size_min = size_min;
}
/**
* 根据填充内容调整textview
*
* @param text
* @param textWidth
*/
private void refitText(String text, int textWidth, int textHeight) {
if (textWidth <= 0 || textHeight <= 0 || text == null || text.length() == 0) {
return;
}
float targetWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
float targetHeight = textHeight - this.getPaddingTop() - this.getPaddingBottom();
final int threshold = 1;
if (getFontWidth(size_max, text) <= targetWidth && getFontHeight(size_max) <= targetHeight) {
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.size_max);
return;
}
int targetSize = this.size_max;
a:
while ((targetSize - size_min) > threshold) {
targetSize--;
if (getFontWidth(targetSize, text) < targetWidth
&& getFontHeight(targetSize) < targetHeight) {
break a;
}
}
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetSize);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
this.refitText(text.toString(), this.getWidth(), this.getHeight());
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw) {
this.refitText(this.getText().toString(), dip2px(w), dip2px(h));
}
}
//获取字体的高度 包含leading
private float getFontHeight(int fontSize) {
TextPaint tp = new TextPaint();
tp.setTextSize(fontSize);
Paint.FontMetrics fm = tp.getFontMetrics();
float fontHeight = (float) Math.ceil(fm.bottom - fm.top);
return fontHeight;
}
//获取对应字体的宽度
private float getFontWidth(int fontSize, String content) {
TextPaint tp = new TextPaint();
tp.setTextSize(fontSize);
return tp.measureText(content);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dip2px(float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
在attrs.xml 中加入如下配置
<attr name="autoScaleTextViewStyle"/>
<declare-styleable name="AutoScaleTextView">
<attr name="minTextSize" format="dimension"/>
</declare-styleable>
如下使用
<com.xxx.order.ui.view.AutoScaleTextView
android:id="@+id/text_name"
android:text="sdfadfasdfasdfsad"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="end"
android:gravity="center|left"
android:textColor="#000"
melove:minTextSize="10sp"
android:textSize="@dimen/textsize_3"
android:maxLines="2" />