Android 自动适应字体大小的EditText
Android 中 EditText 单行显示时,如果字体过多,自动缩小字体,在字号小于指定字号时,显示"..." ,具体请参考如下代码:
package com.zepp.newsports.ui.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import com.zepp.newsports.R;
import com.zepp.z3a.common.util.FontUtil;
/**
* Created by QY on 15/7/20.
*/
public class AutoStretchTextView extends TextView {
private float DEFAULT_MIN_TEXTSIZE = 28;
private String TAG = AutoStretchTextView.class.getSimpleName();
private Rect viewRect;
private int textColor = Color.LTGRAY;
private boolean centerHorization;
private boolean alignLeft;
public AutoStretchTextView(Context context) {
super(context);
}
public AutoStretchTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public AutoStretchTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}
@Override protected void onDraw(Canvas canvas) {
int layoutWidth = getWidth();
double charWidth = 0;
int height = getHeight();
TextPaint paint = getPaint();
String text = getText().toString();
viewRect = new Rect();
float drawedWidth = paint.measureText(text);
char[] textCharArray = text.toCharArray();
if (textCharArray.length > 0) {
paint.getTextBounds(textCharArray, 0, textCharArray.length, viewRect);
charWidth = paint.measureText(textCharArray, textCharArray.length - 1, 1);
}
while (layoutWidth - drawedWidth < charWidth) {
if (getTextSize() <= DEFAULT_MIN_TEXTSIZE) {
StringBuilder builder = new StringBuilder();
for (char chars : textCharArray) {
builder.append(chars);
float visibleTextWidth = paint.measureText(builder.toString() + "...");
if (layoutWidth - visibleTextWidth < 1.5 * charWidth) {
text = builder.append("...").toString();
char[] chars1 = text.toCharArray();
paint.getTextBounds(chars1, 0, chars1.length, viewRect);
break;
}
} break;
}
paint.setTextSize(getTextSize() - 1);
drawedWidth = paint.measureText(text);
if(textCharArray.length > 0)
charWidth = paint.measureText(textCharArray, textCharArray.length - 1, 1);
}
float startY = height -(height- viewRect.height()) * 0.5f;
paint.setColor(textColor);
if (centerHorization) {
canvas.drawText(text, Math.abs(layoutWidth - viewRect.width()) / 2 , startY, paint);
} else if (alignLeft) {
canvas.drawText(text, 5, startY, paint);
} else {
int startX = layoutWidth - viewRect.width();
if (startX < 0)
startX = 0;
canvas.drawText(text, startX, startY, paint);
}
}
private void init(AttributeSet attrs) {
if (!isInEditMode()) {
String resource = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textColor");
if (!TextUtils.isEmpty(resource)) {
if (resource.contains("@")) {
textColor = getContext().getResources().getColor(Integer.valueOf(resource.substring(1, resource.length())));
} else {
textColor = Color.parseColor(resource);
}
}
TypedArray typedArray =
getContext().obtainStyledAttributes(attrs, R.styleable.AutoStretchTextView);
DEFAULT_MIN_TEXTSIZE =
typedArray.getDimension(R.styleable.AutoStretchTextView_default_min_textsize,
DEFAULT_MIN_TEXTSIZE);
typedArray.recycle();
int font = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "font",
0);
getPaint().setTypeface(FontUtil.getInstance().getFontTypeface(getContext(), font));
centerHorization =
attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto",
"centerHorization", false);
alignLeft =
attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto",
"alignLeft", false);
}
}
}
在 attr.xml中设置自定义的属性,用于设置最小字体,绘制字体是居中或巨左,默认是居右绘制的
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AutoStretchTextView">
<attr name="default_min_textsize" format="dimension"/>
<attr name="centerHorization" format="boolean"/>
<attr name="alignLeft" format="boolean"/>
</declare-styleable>
</resources>