android Edittext内容字体大小动态变化

[java]  view plain  copy
  1. package com.yitong.mbank.android.views;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Paint;  
  5. import android.graphics.Paint.FontMetrics;  
  6. import android.util.AttributeSet;  
  7. import android.widget.EditText;  
  8.   
  9. public class AutoAdjustSizeEditText extends EditText {  
  10.     private static float DEFAULT_MIN_TEXT_SIZE = 10// 最小的字体大小  
  11.     private static float DEFAULT_MAX_TEXT_SIZE = 20;// 验证大部分手机情况下无效值  
  12.   
  13.     // Attributes  
  14.     private Paint testPaint;  
  15.     private float minTextSize, maxTextSize;  
  16.   
  17.     public AutoAdjustSizeEditText(Context context, AttributeSet attrs) {  
  18.         super(context, attrs);  
  19.         initialise();  
  20.     }  
  21.   
  22.     private void initialise() {  
  23.         testPaint = new Paint();  
  24.         testPaint.set(this.getPaint()); // 获取模拟的paint  
  25.   
  26.         // max size defaults to the intially specified text size unless it is  
  27.         // too small  
  28.         maxTextSize = this.getTextSize();// 获取单个字体的像素  
  29.         if (maxTextSize <= DEFAULT_MIN_TEXT_SIZE) {  
  30.             maxTextSize = DEFAULT_MAX_TEXT_SIZE;  
  31.         }  
  32.         minTextSize = DEFAULT_MIN_TEXT_SIZE;  
  33.     };  
  34.   
  35.     /** 
  36.      * Re size the font so the specified text fits in the text box * assuming 
  37.      * the text box is the specified width. 
  38.      */  
  39.     private void refitText(String text, int textWidth) {  
  40.   
  41.         if (textWidth > 0) {  
  42.             int availableWidth = textWidth - this.getPaddingLeft()  
  43.                     - this.getPaddingRight();// 获取改TextView的画布可用大小          
  44.             float trySize = maxTextSize;  
  45.             float scaled = getContext().getResources().getDisplayMetrics().scaledDensity;  
  46.             testPaint.setTextSize(trySize * scaled);// 模拟注意乘以scaled  
  47.             while ((trySize > minTextSize)  
  48.                     && (testPaint.measureText(text) > availableWidth)) {  
  49.                 trySize -= 2;  
  50.                 FontMetrics fm = testPaint.getFontMetrics();  
  51.                 float scaled1 = (float) (this.getHeight() / (Math  
  52.                         .ceil(fm.descent - fm.top) + 2));  
  53.                 float scaled2 = (float) ((testPaint.measureText(text) / availableWidth));  
  54.                 if (scaled1 >= 1.75 & scaled1 >= scaled2) {// 注意1.75是三星s4 小米3  
  55.                                                             // 的适合数值(当然包括我的联想了)  
  56.                     break;  
  57.                 }  
  58.                 if (trySize <= minTextSize) {  
  59.                     trySize = minTextSize;  
  60.                     break;  
  61.                 }  
  62.                 testPaint.setTextSize(trySize * scaled);  
  63.             }  
  64.             this.setTextSize(trySize);// 等同于this.getPaint().set(trySize*scaled);  
  65.         }  
  66.     };  
  67.   
  68.     @Override  
  69.     protected void onTextChanged(CharSequence text, int start, int before,  
  70.             int after) {  
  71.         super.onTextChanged(text, start, before, after);  
  72.         refitText(text.toString(), this.getWidth());  
  73.     }  
  74.   
  75.     @Override  
  76.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  77.         if (w != oldw) {  
  78.             refitText(this.getText().toString(), w);  
  79.         }  
  80.     }  
  81. }  

然后直接在你的xml布局文件中这样使用:

[html]  view plain  copy
  1. <com.allone.CTPMobile.component.edittext.AutoAdjustSizeEditText  
  2.                                android:id="@+id/horiTakeOrderPrice"  
  3.                                android:layout_width="fill_parent"  
  4.                                android:layout_height="fill_parent"  
  5.                                android:background="@drawable/selector_edit_bg"  
  6.                                android:gravity="center|right"  
  7.                                android:paddingLeft="34.0dip"  
  8.                                android:paddingRight="4.0dip"  
  9.                                android:textColor="@color/color_white_f0f0f0"  
  10.                                android:textSize="10sp"  
  11.                                android:text="888"  
  12.                                android:focusable ="true"  
  13.                                android:focusableInTouchMode="true"  
  14.                                android:clickable="true"/

文章出自 CSDN:

https://blog.csdn.net/nihaoqiulinhe/article/details/80340226

猜你喜欢

转载自blog.csdn.net/unfinished_story/article/details/80359948
今日推荐