工具类之SpannableStringUtils(相信你会爱上它)
字数412阅读709评论13喜欢33
这个工具类真是构思了良久才设计出来,采用了建造者模式,然后你们就可以用链式调用了,talk is cheap, let me show the demo.
demo
code
有没有心动一下哈,下面就让老司机为你们开路。
站点
SpannableString相关工具类→SpannableStringUtils.java→Demo
getBuilder : 获取建造者setFlag : 设置标识setForegroundColor: 设置前景色setBackgroundColor: 设置背景色setQuoteColor : 设置引用线的颜色setLeadingMargin : 设置缩进setBullet : 设置列表标记setProportion : 设置字体比例setXProportion : 设置字体横向比例setStrikethrough : 设置删除线setUnderline : 设置下划线setSuperscript : 设置上标setSubscript : 设置下标setBold : 设置粗体setItalic : 设置斜体setBoldItalic : 设置粗斜体setFontFamily : 设置字体setAlign : 设置对齐setBitmap : 设置图片setDrawable : 设置图片setUri : 设置图片setResourceId : 设置图片setClickSpan : 设置点击事件setUrl : 设置超链接setBlur : 设置模糊append : 追加样式字符串create : 创建样式字符串
具体路线
packagecom.blankj.utilcode.utils;importandroid.graphics.Bitmap;importandroid.graphics.BlurMaskFilter;importandroid.graphics.Typeface;importandroid.graphics.drawable.Drawable;importandroid.net.Uri;importandroid.support.annotation.ColorInt;importandroid.support.annotation.DrawableRes;importandroid.support.annotation.NonNull;importandroid.support.annotation.Nullable;importandroid.text.Layout.Alignment;importandroid.text.SpannableStringBuilder;importandroid.text.Spanned;importandroid.text.style.AlignmentSpan;importandroid.text.style.BackgroundColorSpan;importandroid.text.style.BulletSpan;importandroid.text.style.ClickableSpan;importandroid.text.style.ForegroundColorSpan;importandroid.text.style.ImageSpan;importandroid.text.style.LeadingMarginSpan;importandroid.text.style.MaskFilterSpan;importandroid.text.style.QuoteSpan;importandroid.text.style.RelativeSizeSpan;importandroid.text.style.ScaleXSpan;importandroid.text.style.StrikethroughSpan;importandroid.text.style.StyleSpan;importandroid.text.style.SubscriptSpan;importandroid.text.style.SuperscriptSpan;importandroid.text.style.TypefaceSpan;importandroid.text.style.URLSpan;importandroid.text.style.UnderlineSpan;importstaticandroid.graphics.BlurMaskFilter.Blur;/**
*
* author: Blankj
* blog : http://blankj.com
* time : 16/12/13
* desc : SpannableString相关工具类
*
*/publicclassSpannableStringUtils{privateSpannableStringUtils(){thrownewUnsupportedOperationException("u can't instantiate me..."); }/** * 获取建造者 * *@return{@linkBuilder} */publicstaticBuildergetBuilder(@NonNull CharSequence text){returnnewBuilder(text); }publicstaticclassBuilder{privateintdefaultValue =0x12000000;privateCharSequence text;privateintflag;@ColorIntprivateintforegroundColor;@ColorIntprivateintbackgroundColor;@ColorIntprivateintquoteColor;privatebooleanisLeadingMargin;privateintfirst;privateintrest;privatebooleanisBullet;privateintgapWidth;privateintbulletColor;privatefloatproportion;privatefloatxProportion;privatebooleanisStrikethrough;privatebooleanisUnderline;privatebooleanisSuperscript;privatebooleanisSubscript;privatebooleanisBold;privatebooleanisItalic;privatebooleanisBoldItalic;privateString fontFamily;privateAlignment align;privatebooleanimageIsBitmap;privateBitmap bitmap;privatebooleanimageIsDrawable;privateDrawable drawable;privatebooleanimageIsUri;privateUri uri;privatebooleanimageIsResourceId;@DrawableResprivateintresourceId;privateClickableSpan clickSpan;privateString url;privatebooleanisBlur;privatefloatradius;privateBlur style;privateSpannableStringBuilder mBuilder;privateBuilder(@NonNull CharSequence text){this.text = text; flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; foregroundColor = defaultValue; backgroundColor = defaultValue; quoteColor = defaultValue; proportion = -1; xProportion = -1; mBuilder =newSpannableStringBuilder(); }/** * 设置标识 * *@paramflag
- *
- {@linkSpanned#SPAN_INCLUSIVE_EXCLUSIVE} *
- {@linkSpanned#SPAN_INCLUSIVE_INCLUSIVE} *
- {@linkSpanned#SPAN_EXCLUSIVE_EXCLUSIVE} *
- {@linkSpanned#SPAN_EXCLUSIVE_INCLUSIVE} *
- *
- monospace *
- serif *
- sans-serif *
- *
- {@linkAlignment#ALIGN_NORMAL}正常 *
- {@linkAlignment#ALIGN_OPPOSITE}相反 *
- {@linkAlignment#ALIGN_CENTER}居中 *
需添加view.setMovementMethod(LinkMovementMethod.getInstance())
*@paramclickSpan 点击事件 *@return{@linkBuilder} */publicBuildersetClickSpan(@NonNull ClickableSpan clickSpan){this.clickSpan = clickSpan;returnthis; }/** * 设置超链接 *需添加view.setMovementMethod(LinkMovementMethod.getInstance())
* *@paramurl 超链接 *@return{@linkBuilder} */publicBuildersetUrl(@NonNull String url){this.url = url;returnthis; }/** * 设置模糊 *尚存bug,其他地方存在相同的字体的话,相同字体出现在之前的话那么就不会模糊,出现在之后的话那会一起模糊
*推荐还是把所有字体都模糊这样使用
* *@paramradius 模糊半径(需大于0) *@paramstyle 模糊样式- *
- {@linkBlur#NORMAL} *
- {@linkBlur#SOLID} *
- {@linkBlur#OUTER} *
- {@linkBlur#INNER} *
* 设置样式
*/privatevoidsetSpan(){intstart = mBuilder.length(); mBuilder.append(this.text);intend = mBuilder.length();if(foregroundColor != defaultValue) { mBuilder.setSpan(newForegroundColorSpan(foregroundColor), start, end, flag); foregroundColor = defaultValue; }if(backgroundColor != defaultValue) { mBuilder.setSpan(newBackgroundColorSpan(backgroundColor), start, end, flag); backgroundColor = defaultValue; }if(isLeadingMargin) { mBuilder.setSpan(newLeadingMarginSpan.Standard(first, rest), start, end, flag); isLeadingMargin =false; }if(quoteColor != defaultValue) { mBuilder.setSpan(newQuoteSpan(quoteColor), start, end,0); quoteColor = defaultValue; }if(isBullet) { mBuilder.setSpan(newBulletSpan(gapWidth, bulletColor), start, end,0); isBullet =false; }if(proportion != -1) { mBuilder.setSpan(newRelativeSizeSpan(proportion), start, end, flag); proportion = -1; }if(xProportion != -1) { mBuilder.setSpan(newScaleXSpan(xProportion), start, end, flag); xProportion = -1; }if(isStrikethrough) { mBuilder.setSpan(newStrikethroughSpan(), start, end, flag); isStrikethrough =false; }if(isUnderline) { mBuilder.setSpan(newUnderlineSpan(), start, end, flag); isUnderline =false; }if(isSuperscript) { mBuilder.setSpan(newSuperscriptSpan(), start, end, flag); isSuperscript =false; }if(isSubscript) { mBuilder.setSpan(newSubscriptSpan(), start, end, flag); isSubscript =false; }if(isBold) { mBuilder.setSpan(newStyleSpan(Typeface.BOLD), start, end, flag); isBold =false; }if(isItalic) { mBuilder.setSpan(newStyleSpan(Typeface.ITALIC), start, end, flag); isItalic =false; }if(isBoldItalic) { mBuilder.setSpan(newStyleSpan(Typeface.BOLD_ITALIC), start, end, flag); isBoldItalic =false; }if(fontFamily !=null) { mBuilder.setSpan(newTypefaceSpan(fontFamily), start, end, flag); fontFamily =null; }if(align !=null) { mBuilder.setSpan(newAlignmentSpan.Standard(align), start, end, flag); align =null; }if(imageIsBitmap || imageIsDrawable || imageIsUri || imageIsResourceId) {if(imageIsBitmap) { mBuilder.setSpan(newImageSpan(Utils.context, bitmap), start, end, flag); bitmap =null; imageIsBitmap =false; }elseif(imageIsDrawable) { mBuilder.setSpan(newImageSpan(drawable), start, end, flag); drawable =null; imageIsDrawable =false; }elseif(imageIsUri) { mBuilder.setSpan(newImageSpan(Utils.context, uri), start, end, flag); uri =null; imageIsUri =false; }else{ mBuilder.setSpan(newImageSpan(Utils.context, resourceId), start, end, flag); resourceId =0; imageIsResourceId =false; } }if(clickSpan !=null) { mBuilder.setSpan(clickSpan, start, end, flag); clickSpan =null; }if(url !=null) { mBuilder.setSpan(newURLSpan(url), start, end, flag); url =null; }if(isBlur) { mBuilder.setSpan(newMaskFilterSpan(newBlurMaskFilter(radius, style)), start, end, flag); isBlur =false; } flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE; } }}
终点站
为了能让乘客们调用地爽快与满意,我真是把能囊括的span都收纳过来了,终点站到了,如果对本次旅途满意的话,请给五星好评哦,毕竟老司机这次真的牺牲了很多时间与精力才换来这么一份精良的工具类,如果该工具类依赖其他工具类,都可以在我的Android开发人员不得不收集的代码(持续更新中)中找到。
网友评论