Android textView字间距自定义LetterSpacingTextView

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


<resources>

    <declare-styleable name="LetterSpacingTextView">
        <attr name="textSpacing" format="float"/>
        <attr name="text" format="string"/>
    declare-styleable>
resources>

xml中使用示例:


<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配置字间距的属性

你可能感兴趣的:(Android,android,textview,字间距)