LetterSpacingTextView.java

package com.akm.akmviewpager;/** * Created by toge on 16/1/20. * 字间距 */import android.content.Context;import android.content.res.TypedArray;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 spacing = Spacing.NORMAL;    private CharSequence originalText = "";    public LetterSpacingTextView(Context context) {        super(context);    }    public LetterSpacingTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs);    }    public LetterSpacingTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context, attrs);    }    public float getSpacing() {        return this.spacing;    }    private void init(Context context, AttributeSet attrs) {        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSpacingTextView);        originalText = array.getString(R.styleable.LetterSpacingTextView_text);        setSpacing(array.getFloat(R.styleable.LetterSpacingTextView_textSpacing, 0));        array.recycle();    }    public void setSpacing(float spacing) {        this.spacing = spacing;        applySpacing();    }    @Override    public void setText(CharSequence text, BufferType type) {        originalText = text;        applySpacing();    }    @Override    public CharSequence getText() {        return originalText;    }    private void applySpacing() {        if (this == null || this.originalText == null) return;        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((spacing + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);            }        }        super.setText(finalText, BufferType.SPANNABLE);    }    public class Spacing {        public final static float NORMAL = 0;    }}

attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="LetterSpacingTextView">        <attr name="textSpacing" format="float"/>        <attr name="text" format="string"/>    declare-styleable>resources>

xml中使用示例:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <com.akm.akmviewpager.LetterSpacingTextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="match_parent"        app:text="暗淡轻黄体性柔,情疏迹远只香留。 何须浅碧轻红色,自是花中第一流。梅定妒,菊应羞,画栏开处冠中秋。 骚人可煞无情思,何事当年不见收。"        app:textSpacing="5" />LinearLayout>

Java代码中使用示例:

 LetterSpacingTextView textView = (LetterSpacingTextView) findViewById(R.id.textView);        textView.setText("出自宋代诗人李清照的《鹧鸪天·桂花》");        textView.setSpacing(5);

改编自:How to adjust text kerning in Android TextView?
增加了xml配置字间距的属性

更多相关文章

  1. Android 自定义View及其在布局文件中的使用示例(三):结合Android
  2. Android团队提供的示例项目
  3. Android中ProgressDialog的简单示例
  4. android从网上加载图片简单示例
  5. android opengl开发示例代码
  6. Android 经典示例,初学者的绝好源码资料
  7. Android基础系列-----------Android进程/线程管理应用示例(Androi

随机推荐

  1. Android与服务器传递数据
  2. android 界面 滑入 效果
  3. android-数据库操作实例,留着以后用
  4. android 的popwindow弹窗
  5. android计算器简单版【安卓进化一】
  6. Android:ImageView 设置图片
  7. Android(安卓)添加桌面快捷方式操作
  8. android 获得监听某一广播的所有程序
  9. android 应用异常可以引起android系统崩
  10. Android: How to play Tone sound