package com.hdl.photovoltaic.utils; import android.graphics.Paint; import android.graphics.Typeface; import android.text.Spannable; import android.text.SpannableString; import android.text.TextPaint; import android.text.style.BackgroundColorSpan; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.MetricAffectingSpan; import android.text.style.RelativeSizeSpan; import android.view.View; import androidx.annotation.NonNull; /** * String字符串通过区间来改变颜色,大小,字体,下划线等 */ public class SpanUtils { private long mLastClickTime = 0; public static final int TIME_INTERVAL = 1000; private static volatile SpanUtils sSpanUtils; /** * 获取当前对象 * * @return HdlCommonLogic */ public static synchronized SpanUtils getInstance() { if (sSpanUtils == null) { synchronized (SpanUtils.class) { if (sSpanUtils == null) { sSpanUtils = new SpanUtils(); } } } return sSpanUtils; } private SpanUtils() { } /** * 变大变小 */ public CharSequence toSizeSpan(CharSequence charSequence, int start, int end, float scale) { SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan( new RelativeSizeSpan(scale), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } /** * 变色 */ public CharSequence toColorSpan(CharSequence charSequence, int start, int end, int color) { SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan( new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } /** * 变背景色 */ public CharSequence toBackgroundColorSpan(CharSequence charSequence, int start, int end, int color) { SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan( new BackgroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } /** * 可点击-带下划线 */ public CharSequence toClickSpan(CharSequence charSequence, int start, int end, int color, boolean needUnderLine, OnSpanClickListener listener) { SpannableString spannableString = new SpannableString(charSequence); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(@NonNull View widget) { if (listener != null) { //防止重复点击 if (System.currentTimeMillis() - mLastClickTime >= TIME_INTERVAL) { //to do listener.onClick(charSequence.subSequence(start, end)); mLastClickTime = System.currentTimeMillis(); } } } @Override public void updateDrawState(@NonNull TextPaint ds) { ds.setColor(color); ds.setUnderlineText(needUnderLine); } }; spannableString.setSpan( clickableSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } public interface OnSpanClickListener { void onClick(CharSequence charSequence); } /** * 变成自定义的字体 */ public CharSequence toCustomTypeFaceSpan(CharSequence charSequence, int start, int end, Typeface typeface) { SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan( new MyTypefaceSpan(typeface), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } /** * 系统原生的TypefaceSpan只能使用原生的默认字体 * 如果使用自定义的字体,通过这个来实现 */ public class MyTypefaceSpan extends MetricAffectingSpan { private final Typeface typeface; public MyTypefaceSpan(final Typeface typeface) { this.typeface = typeface; } @Override public void updateDrawState(final TextPaint drawState) { apply(drawState); } @Override public void updateMeasureState(final TextPaint paint) { apply(paint); } private void apply(final Paint paint) { final Typeface oldTypeface = paint.getTypeface(); final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0; int fakeStyle = oldStyle & ~typeface.getStyle(); if ((fakeStyle & Typeface.BOLD) != 0) { paint.setFakeBoldText(true); } if ((fakeStyle & Typeface.ITALIC) != 0) { paint.setTextSkewX(-0.25f); } paint.setTypeface(typeface); } } }