700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 自定义TextView 文字自适应高度和宽度

自定义TextView 文字自适应高度和宽度

时间:2023-10-04 22:26:02

相关推荐

自定义TextView 文字自适应高度和宽度

自适应高度:

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();}//文字改变的时候@Overrideprotected 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));}}//大小改变的时候@Overrideprotected 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.FitHeightTextViewandroid: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();}@Overrideprotected 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;}@Overrideprotected 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.FitWidthTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:text="设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度适应宽度设置自适应宽度设置自适应宽度设置自适应宽度设置自适应宽度"android:textColor="#56BBe0"android:background="@color/colorGreen"/>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。