自适应高度:
package com.chy.mytest.selfadaption;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
import androidx.annotation.Nullable;
/**
*
* @filename FitHeightTextView
* @describe 根据高度自适应字体文字大小
*
*/
@SuppressLint("AppCompatCustomView")
public class FitHeightTextView extends TextView {
private Paint mTextPaint;
private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小
private float mMinTextSize = 8; //最小的字体大小
public FitHeightTextView(Context context) {
super(context);
}
public FitHeightTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setGravity(getGravity() | Gravity.CENTER_VERTICAL); // 默认水平居中
setLines(1);
initialise();
}
private void initialise() {
mTextPaint = new TextPaint();
mTextPaint.set(this.getPaint());
//默认的大小是设置的大小,如果撑不下了 就改变
mMaxTextSize = this.getTextSize();
}
//文字改变的时候
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
refitText(text.toString(), this.getHeight()); //textview视图的高度
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
private void refitText(String textString, int height) {
if (height > 0) {
int availableHeight = height - this.getPaddingTop() - this.getPaddingBottom(); //减去边距为字体的实际高度
float trySize = mMaxTextSize;
mTextPaint.setTextSize(trySize);
while (mTextPaint.descent()-mTextPaint.ascent() > availableHeight) { //测量的字体高度过大,不断地缩放
trySize -= 1; //字体不断地减小来适应
if (trySize <= mMinTextSize) {
trySize = mMinTextSize; //最小为这个
break;
}
mTextPaint.setTextSize(trySize);
}
setTextSize(px2sp(getContext(), trySize));
}
}
//大小改变的时候
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (h != oldh) {
refitText(this.getText().toString(), h);
}
}
/**
* 将px值转换为sp值,保证文字大小不变
*/
public static float px2sp(Context context,float pxValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (pxValue / fontScale);
}
}
xml布局:
<com.chy.mytest.selfadaption.FitHeightTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="设置自适应高度设置自适应高度设置自适应高度设置自适应高度"
android:textSize="28sp"
android:textColor="#56BBe0"
android:background="@color/colorRed"/>
自适应宽度:
package com.chy.mytest.selfadaption;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
/**
*
* @filename FitWidthTextView
* @describe 根据宽度自适应字体文字大小
*
*/
@SuppressLint("AppCompatCustomView")
public class FitWidthTextView extends TextView {
private Paint mTextPaint;
private float mMaxTextSize; // 获取当前所设置文字大小作为最大文字大小
private float mMinTextSize = 3;
public FitWidthTextView(Context context) {
this(context, null);
}
public FitWidthTextView(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());
// 最大的大小默认为特定的文本大小,除非它太小了
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布局:
<com.chy.mytest.selfadaption.FitWidthTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="设置自适应宽度设置自适应宽度设置自适应宽度设置自适
应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自
适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度
适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度"
android:textColor="#56BBe0"
android:background="@color/colorGreen"/>