33.Android 自動適配字體大小的AutoAdjustSizeEditText
AutoAdjustSizeEditText介紹
由於一個需求,就是一個定寬高的EditText裏,輸入後,要根據字的內容改變字體大小。
這時候就需要這麼一個自定適配字體大小的EditView。
AutoAdjustSizeEditText原理
實現原理: 重寫EditText,覆寫其setText
、onTextChanged
和onSizeChanged
方法。分別在這裏重新寫適配字體大小的邏輯處理,所謂的邏輯處理,就是計算單行可見文字的寬度,然後從自定義的最大字體開始嘗試,先用最大字體去寫字( Paint.setTextSize(float textSize)
),然後判斷:最大字體畫出字的寬度>可見文字寬度。如果成立,那麼減小一號字體再去畫,不斷去減小字體去判斷,直到不成立時,就找到合適的字體大小了。
AutoAdjustSizeEditText
public class AutoAdjustSizeEditText extends EditText {
// 最小字體
private static final float DEFAULT_MIN_TEXT_SIZE = 8.0f;
// 最大字體
private static final float DEFAULT_MAX_TEXT_SIZE = 16.0f;
private Paint textPaint;
private float minTextSize = DEFAULT_MIN_TEXT_SIZE;
private float maxTextSize = DEFAULT_MAX_TEXT_SIZE;
public AutoAdjustSizeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void initialise() {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
if (this.textPaint == null) {
this.textPaint = new Paint();
this.textPaint.set(this.getPaint());
}
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
if (DEFAULT_MIN_TEXT_SIZE >= maxTextSize) {
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
}
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
this.minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.minTextSize, displayMetrics);
}
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
*/
private void fitText(String text, int textWidth) {
if (textWidth > 0) {
// 單行可見文字寬度
int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
float trySize = maxTextSize;
// 先用最大字體寫字
textPaint.setTextSize(trySize);
// 如果最大字體>最小字體 && 最大字體畫出字的寬度>單行可見文字寬度
while ((trySize > minTextSize) && (textPaint.measureText(text) > availableWidth)) {
// 最大字體小一號
trySize -= 1;
// 保證大於最小字體
if (trySize <= minTextSize) {
trySize = minTextSize;
break;
}
// 再次用新字體寫字
textPaint.setTextSize(trySize);
}
this.setTextSize(trySize);
}
}
/**
* 重寫setText
* 每次setText的時候
*
* @param text
* @param type
*/
@Override
public void setText(CharSequence text, BufferType type) {
this.initialise();
String textString = text.toString();
float trySize = maxTextSize;
if (this.textPaint == null) {
this.textPaint = new Paint();
this.textPaint.set(this.getPaint());
}
this.textPaint.setTextSize(trySize);
// 計算設置內容前 內容佔據的寬度
int textWidth = (int) this.textPaint.measureText(textString);
// 拿到寬度和內容,進行調整
this.fitText(textString, textWidth);
super.setText(text, type);
}
@Override
protected void onTextChanged(CharSequence text, int start, int before, int after) {
super.onTextChanged(text, start, before, after);
this.fitText(text.toString(), this.getWidth());
}
/**
* This is called during layout when the size of this view has changed. If
* you were just added to the view hierarchy, you're called with the old
* values of 0.
*
* @param w Current width of this view.
* @param h Current height of this view.
* @param oldw Old width of this view.
* @param oldh Old height of this view.
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 如果當前view的寬度 != 原來view的寬度
if (w != oldw) this.fitText(this.getText().toString(), w);
}
}
AutoAdjustSizeEditTextActivity
AutoAdjustSizeEditTextActivity
public class AutoAdjustSizeEditTextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_auto_adjust_size_edit_text);
}
}
activity_auto_adjust_size_edit_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<camnter.newlife.widget.AutoAdjustSizeEditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything"
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeEditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything"
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeEditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeEditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeEditText
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
</LinearLayout>
AutoAdjustSizeEditTextActivity效果圖
AutoAdjustSizeTextView
由於,EditText也是TextView的子類,所以用上面的AutoAdjustSizeEditText代碼一樣可以實現自動適配字體大小的TextView。
public class AutoAdjustSizeTextView extends TextView {
// 最小字體
private static final float DEFAULT_MIN_TEXT_SIZE = 8.0f;
// 最大字體
private static final float DEFAULT_MAX_TEXT_SIZE = 16.0f;
private Paint textPaint;
private float minTextSize = DEFAULT_MIN_TEXT_SIZE;
private float maxTextSize = DEFAULT_MAX_TEXT_SIZE;
public AutoAdjustSizeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void initialise() {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
if (this.textPaint == null) {
this.textPaint = new Paint();
this.textPaint.set(this.getPaint());
}
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
if (DEFAULT_MIN_TEXT_SIZE >= maxTextSize) {
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
}
this.maxTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.maxTextSize, displayMetrics);
this.minTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this.minTextSize, displayMetrics);
}
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
*/
private void fitText(String text, int textWidth) {
if (textWidth > 0) {
// 單行可見文字寬度
int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight();
float trySize = maxTextSize;
// 先用最大字體寫字
textPaint.setTextSize(trySize);
// 如果最大字體>最小字體 && 最大字體畫出字的寬度>單行可見文字寬度
while ((trySize > minTextSize) && (textPaint.measureText(text) > availableWidth)) {
// 最大字體小一號
trySize -= 1;
// 保證大於最小字體
if (trySize <= minTextSize) {
trySize = minTextSize;
break;
}
// 再次用新字體寫字
textPaint.setTextSize(trySize);
}
this.setTextSize(trySize);
}
}
/**
* 重寫setText
* 每次setText的時候
*
* @param text
* @param type
*/
@Override
public void setText(CharSequence text, BufferType type) {
this.initialise();
String textString = text.toString();
float trySize = maxTextSize;
if (this.textPaint == null) {
this.textPaint = new Paint();
this.textPaint.set(this.getPaint());
}
this.textPaint.setTextSize(trySize);
// 計算設置內容前 內容佔據的寬度
int textWidth = (int) this.textPaint.measureText(textString);
// 拿到寬度和內容,進行調整
this.fitText(textString, textWidth);
super.setText(text, type);
}
@Override
protected void onTextChanged(CharSequence text, int start, int before, int after) {
super.onTextChanged(text, start, before, after);
this.fitText(text.toString(), this.getWidth());
}
/**
* This is called during layout when the size of this view has changed. If
* you were just added to the view hierarchy, you're called with the old
* values of 0.
*
* @param w Current width of this view.
* @param h Current height of this view.
* @param oldw Old width of this view.
* @param oldh Old height of this view.
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 如果當前view的寬度 != 原來view的寬度
if (w != oldw) this.fitText(this.getText().toString(), w);
}
}
AutoAdjustSizeTextViewActivity
AutoAdjustSizeTextViewActivity
public class AutoAdjustSizeTextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_auto_adjust_size_text_view);
}
}
activity_auto_adjust_size_text_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<camnter.newlife.widget.AutoAdjustSizeTextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything"
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeTextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything"
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeTextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeTextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
<camnter.newlife.widget.AutoAdjustSizeTextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:text="Save you from anything Save you from anything Save you from anything Save you from anything Save you from anything "
android:textColor="#ffFF4081" />
</LinearLayout>