textView的字符间距的调整

/** * Text view that allows changing the letter spacing of the text.

 *  * @author Pedro Barros (pedrobarros.dev at gmail.com) * @since May 7, 2013 *

/import android.content.Context;

import android.text.Spannable;

import android.text.SpannableString;

import android.text.style.ScaleXSpan;

import android.util.AttributeSet;

import android.widget.TextView;

public class LetterSpacingTextView extends TextView { 

   private float letterSpacing = LetterSpacing.NORMAL; 

   private CharSequence originalText = "";  

  public LetterSpacingTextView(Context context) {  

      super(context);    }   

 public LetterSpacingTextView(Context context, AttributeSet attrs){ 

       super(context, attrs);   

}  

  public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle){ 

       super(context, attrs, defStyle);   

 }  

  public float getLetterSpacing() {    

    return letterSpacing; 

   }  

  public void setLetterSpacing(float letterSpacing) { 

       this.letterSpacing = letterSpacing;  

      applyLetterSpacing();   

 }  

  @Override  

  public void setText(CharSequence text, BufferType type) {  

      originalText = text;     

   applyLetterSpacing();  

  }   

 @Override   

 public CharSequence getText() {  

      return originalText;  

  }   

 private void applyLetterSpacing() { 

       StringBuilder builder = new StringBuilder();    

    for(int i = 0; i < originalText.length(); i++) {   

         builder.append(originalText.charAt(i));  

          if(i+1 < originalText.length()) {       

         builder.append("\u00A0");       

     }   

     }       

 SpannableString finalText = new SpannableString(builder.toString());

        if(builder.toString().length() > 1) {  

          for(int i = 1; i < builder.toString().length(); i+=2) {    

            finalText.setSpan(new ScaleXSpan((letterSpacing+1)/10), i, i+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);     

       }        }   

     super.setText(finalText, BufferType.SPANNABLE);    }  

  public class LetterSpacing { 

       public final static float NORMAL = 0;    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值