SpannableString简介

参考链接:

http://www.cnblogs.com/jisheng/archive/2013/01/10/2854088.html

http://www.jianshu.com/p/d4349a4ca216

package com.lml.activity.spanablestrtest;

import android.content.res.Resources;

import android.graphics.BlurMaskFilter;

import android.graphics.Color;

import android.graphics.EmbossMaskFilter;

import android.graphics.Typeface;

import android.graphics.drawable.Drawable;

import android.support.v4.content.res.ResourcesCompat;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.text.Spannable;

import android.text.SpannableString;

import android.text.method.LinkMovementMethod;

import android.text.style.AbsoluteSizeSpan;

import android.text.style.BackgroundColorSpan;

import android.text.style.ClickableSpan;

import android.text.style.DynamicDrawableSpan;

import android.text.style.ForegroundColorSpan;

import android.text.style.ImageSpan;

import android.text.style.MaskFilterSpan;

import android.text.style.RelativeSizeSpan;

import android.text.style.ScaleXSpan;

import android.text.style.StrikethroughSpan;

import android.text.style.StyleSpan;

import android.text.style.SubscriptSpan;

import android.text.style.SuperscriptSpan;

import android.text.style.TextAppearanceSpan;

import android.text.style.URLSpan;

import android.text.style.UnderlineSpan;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView tv_show = (TextView)findViewById(R.id.tv_strshow);

SpannableString spanText = getBackgroundColorSpanStr("BackgroundColor");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getForegroundColorSpanStr("ForegroundColor");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getBlurMaskSpanStr("BlurMask");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getEmbossMaskSpanStr("EmbossMask");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getStrikethroughSpanStr("Strikethrough");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getUnderlineSpanStr("Underline");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getAbsoluteSizeSpanStr("AbsoluteSize");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getDynamicDrawableSpanStr("DynamicDrawable");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getImageSpanStr("Image");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getScaleXSpanStr("ScaleX");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getStyleSpanStr("Style");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getClickableSpanStr("Clickable");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getSubscriptSpanStr("Subscript");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getSuperscriptSpanStr("Superscript");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getURLSpanStr("URLSpanStr");

tv_show.append(spanText);

tv_show.append("\n");

spanText = getTextAppearanceSpanStr("TextAppearance");

tv_show.append(spanText);

tv_show.setMovementMethod(LinkMovementMethod.getInstance());

}

/**

* 背景色

* @param str

* @return

*/

private SpannableString getBackgroundColorSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new BackgroundColorSpan(Color.BLUE), 0, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 前景色

* @param str

* @return

*/

private SpannableString getForegroundColorSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 模糊

* @param str

* @return

*/

private SpannableString getBlurMaskSpanStr(String str) {

SpannableString spanText = new SpannableString(str);

MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER));

spanText.setSpan(maskFilterSpan, 0, spanText.length(), Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText;

}

/**

* 浮雕

* @param str

* @return

*/

private SpannableString getEmbossMaskSpanStr(String str) {

SpannableString spanText = new SpannableString(str);

//浮雕(EmbossMaskFilter)

MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));

spanText.setSpan(maskFilterSpan, 0, spanText.length(), Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText;

}

/**

* 光栅

* @param str

* @return

*/

private SpannableString getStrikethroughSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new StrikethroughSpan(), 0, spanText.length(), Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 下划线

* @param str

* @return

*/

private SpannableString getUnderlineSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new UnderlineSpan(), 0, spanText.length(), Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 绝对大小

* @param str

* @return

*/

private SpannableString getAbsoluteSizeSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(), Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 动态图

* @param str

* @return

*/

private SpannableString getDynamicDrawableSpanStr(String str){

SpannableString spanText = new SpannableString(str);

DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(

DynamicDrawableSpan.ALIGN_BOTTOM) {

@Override

public Drawable getDrawable() {

Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);

d.setBounds(0, 0, 50, 50);

return d;

}

};

spanText.setSpan(drawableSpan, 1, 2, Spannable.

SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 图片

* @param str

* @return

*/

private SpannableString getImageSpanStr(String str){

SpannableString spanText = new SpannableString(str);

Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);

d.setBounds(0, 0, 50, 50);

spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 缩放

* @param str

* @return

*/

private SpannableString getScaleXSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new ScaleXSpan(3.8f), 2, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 样式

* @param str

* @return

*/

private SpannableString getStyleSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 2, 3,

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 点击

* @param str

* @return

*/

private SpannableString getClickableSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new ClickableSpan() {

@Override

public void onClick(View widget) {

Toast.makeText(MainActivity.this,"onclick",Toast.LENGTH_SHORT).show();

}

}, 3, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 下标

* @param str

* @return

*/

private SpannableString getSubscriptSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new SubscriptSpan(), 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 上标

* @param str

* @return

*/

private SpannableString getSuperscriptSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new SuperscriptSpan(), 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* url

* @param str

* @return

*/

private SpannableString getURLSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new URLSpan("http://www.baidu.com"), 3, spanText.length(),

Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

/**

* 文本外貌

* @param str

* @return

*/

private SpannableString getTextAppearanceSpanStr(String str){

SpannableString spanText = new SpannableString(str);

spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium),

2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

return spanText ;

}

}

SpannableString简介_第1张图片

你可能感兴趣的:(SpannableString简介)