👉 About the author
As we all know, life is a long process of overcoming difficulties, reflecting and moving forward. In this process, there will be a lot of questions and thinking about life, so I decided to share my own thinking, experience and story, in order to find resonance!! Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
👉 Soon to learn
Use the onTextChanged method of TextView to achieve width and height adaptive control, currently only applicable to a single line.
👉 background
🙈 Small space: The reason for this is that the project requires a wide range of devices (involving phones/tablets/large TVS/numerous custom devices). That’s fine if it’s a well-known brand, but the key customers are still a bunch of messy brands, with countless sizes causing screen density.
🙎 Xiaozhi: you said it was irritating but not irritating. I blew up.
🙈 empty (sigh) : The key is not to give more adaptation time to start. This is very hurtful. If you use the resource file values to match, you will not be able to burn incense enough, and even cause a series of accidents.
🙎 Small zhi (doubt) : so? Is there a solution!
🙈 small empty: that of course, although be to take advantage of the form, but somehow solved the problem. At the beginning, I had no idea. After searching on GitHub for half a day, I finally found a similar open source: AutoFitTextView, which has more than 800 stars as of 2021-8-2. Many others have been searched, but they do not meet the needs of solution.
🙎 Xiaozhi (very happy) : Wow, thanks for open source.
👉 Practice Process
Current scope: TextView without width and height limit; No other restrictions; However, the weight layout used in the project can be adjusted according to the situation! Results: in the lower figure of the control back automatically filled, control large, large font, small control, small font!
Procedure: annotation explanation is very complete
import android.content.Context; import android.graphics.Paint; import android.support.annotation.Nullable; import android.text.TextPaint; import android.util.AttributeSet; import android.view.Gravity; import android.widget.TextView; /** * Created by akitaka on 2018-07-05. * https://zhima.blog.csdn.net/ * https://juejin.cn/user/4265760844943479/columns * https://www.zhihu.com/people/zhimalier * @ filename FitHeightTextView * @ the describe according to highly adaptive text font size * @ email [email protected] */public class FitHeightTextView extends TextView { private Paint mTextPaint; private float mMaxTextSize; Private float mMinTextSize = 8; 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); // Default horizontal center setLines(1); initialise(); } private void initialise() { mTextPaint = new TextPaint(); mTextPaint.set(this.getPaint()); MMaxTextSize = this.gettextSize (); // The default size is the set size. } @override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { refitText(text.toString(), this.getHeight()); Super. onTextChanged(text, start, lengthBefore, lengthAfter); } private void refitText(String textString, Int availableHeight = height-this.getPaddingTop () -. Int availableHeight = height.getPaddingTop () - this.getPaddingBottom(); float trySize = mMaxTextSize; mTextPaint.setTextSize(trySize); While (mtextpaint.descent () -mtextPaint.ascent () > availableHeight) {trySize -= 1; If (trySize <= mMinTextSize) {trySize = mMinTextSize; // at least this 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); } /** * convert px value to sp value, */ public static float px2sp(Context Context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (pxValue / fontScale); }}Copy the code
When using it in your layout, be careful to set the font size to your largest device so that it will automatically scale back on smaller devices
Android: textSize remain the font size of the largest support equipment < cn. Jucheng. www.cpr.util.FitHeightTextView android: layout_width = "wrap_content" Android :layout_height="wrap_content" Android :gravity="center_vertical" Android :text=" set "Android :textSize="28sp" android:textColor="#56BBe0" />Copy the code
And when you do it, it’s amazing;
There’s auto-adaptation based on height, and there’s auto-adaptation based on width. This is online, and it’s mostly on Github; Simple code from last time
/** * Created by akitaka on 2018-07-05. * https://zhima.blog.csdn.net/ * https://juejin.cn/user/4265760844943479/columns * https://www.zhihu.com/people/zhimalier * @ filename FitHeightTextView * @ the describe according to highly adaptive text font size * @ email [email protected] */public class FitTextView extends TextView{ private Paint mTextPaint; private float mMaxTextSize; Private float mMinTextSize = 3; private float mMinTextSize = 3; public FitTextView(Context context) { this(context, null); } public FitTextView(Context context, AttributeSet attrs) { super(context, attrs); setGravity(getGravity() | Gravity.CENTER_VERTICAL); // Default horizontal center 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()); // The maximum size defaults to a specific text size unless it is too small. 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); } // setextSize (px2sp(getContext(), trySize); } /** * convert px value to sp value, */ public static float px2sp(Context Context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (pxValue / fontScale); }}Copy the code
The main differences are: TextPaint method: measureText(text) Descent Ascent!
📢 by xiao Kong and xiao Kong in Xiao Zhi 📢. 📢 Welcome to like 👍 save 🌟 leave 📝