自动调整TextView字体大小以适应文…

效果是 当字数少的时候 字体最大,随着字数的增多字体逐渐减小。 
Java代码  收藏代码
  1. import android.content.Context;  
  2. import android.graphics.Paint;  
  3. import android.util.AttributeSet;  
  4. import android.widget.TextView;  
  5.   
  6. public class CustomTextView extends TextView  
  7.   
  8.     private static float DEFAULT_MIN_TEXT_SIZE 10 
  9.     private static float DEFAULT_MAX_TEXT_SIZE 20 
  10.   
  11.     // Attributes  
  12.     private Paint testPaint;  
  13.     private float minTextSize, maxTextSize;  
  14.   
  15.     public CustomTextView(Context context, AttributeSet attrs)  
  16.         super(context, attrs);  
  17.         initialise();  
  18.      
  19.   
  20.     private void initialise()  
  21.         testPaint new Paint();  
  22.         testPaint.set(this.getPaint());  
  23.   
  24.         // max size defaults to the intially specified text size unless it is  
  25.         // too small  
  26.         maxTextSize this.getTextSize();  
  27.   
  28.         if (maxTextSize <= DEFAULT_MIN_TEXT_SIZE)  
  29.             maxTextSize DEFAULT_MAX_TEXT_SIZE;  
  30.          
  31.   
  32.         minTextSize DEFAULT_MIN_TEXT_SIZE;  
  33.     };  
  34.   
  35.       
  36.     private void refitText(String text, int textWidth)  
  37.         if (textWidth 0 
  38.             int availableWidth textWidth this.getPaddingLeft()  
  39.                     this.getPaddingRight();  
  40.             float trySize maxTextSize;  
  41.             testPaint.setTextSize(trySize);  
  42.             while ((trySize minTextSize)  
  43.                     && (testPaint.measureText(text) availableWidth))  
  44.                 trySize -= 1 
  45.                 if (trySize <= minTextSize)  
  46.                     trySize minTextSize;  
  47.                     break 
  48.                  
  49.                 testPaint.setTextSize(trySize);  
  50.              
  51.             this.setTextSize(trySize);  
  52.          
  53.     };  
  54.   
  55.     @Override  
  56.     protected void onTextChanged(CharSequence text, int start, int before,  
  57.             int after)  
  58.         super.onTextChanged(text, start, before, after);  
  59.         refitText(text.toString(), this.getWidth());  
  60.      
  61.   
  62.     @Override  
  63.     protected void onSizeChanged(int w, int h, int oldw, int oldh)  
  64.         if (w != oldw)  
  65.             refitText(this.getText().toString(), w);  
  66.          
  67.      
  68.  



拖动一个角增大textView面积 
Java代码    收藏代码
  1. public class EditTextClass extends Activity implements OnTouchListener  
  2.   
  3.     private EditText txt;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         txt (EditText) findViewById(YourEditTextId);  
  10.         txt.setOnTouchListener(this);  
  11.      
  12.   
  13.   
  14.     @Override  
  15.     public boolean onTouch(View v, MotionEvent event)  
  16.        if(event.getX() >= txt.getWidth() 5 && event.getX() <= txt.getWidth()  
  17.             event.getY() >= txt.getHeight() 5 && event.getY() <= txt.getHeight())  
  18.            txt.setWidth(event.getX());   
  19.            txt.setHeight(event.getY());  
  20.         
  21.        return true 
  22.      
  23.  


ImageView伸缩动画效果 
http://edison-cool911.iteye.com/blog/870339
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值