Android 各种Span示例

看到一篇说Span的文章,留做备用,来源:http://orgcent.com/android-textview-spannablestring-span/

import android.app.Activity;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Rasterizer;
import android.graphics.Typeface;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.drawable.Drawable;
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.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.MaskFilterSpan;
import android.text.style.RasterizerSpan;
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.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class SpanTestActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView displayText = (TextView) findViewById(R.id.display);
        
        // BackgroundColorSpan
        SpannableString spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new BackgroundColorSpan(Color.GREEN), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);;
        displayText.append(spanText);
        
        // ForegroundColorSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // MaskFilterSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        int length = spanText.length();
        //模糊(BlurMaskFilter)
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, Blur.OUTER));
        spanText.setSpan(maskFilterSpan, 0, length - 10, Spannable.
        SPAN_INCLUSIVE_EXCLUSIVE);
        //浮雕(EmbossMaskFilter)
        maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{1,1,3}, 1.5f, 8, 3));
        spanText.setSpan(maskFilterSpan, length - 10, length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // RasterizerSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new RasterizerSpan(new Rasterizer()), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // StrikethroughSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new StrikethroughSpan(), 0, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // UnderlineSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new UnderlineSpan(), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // AbsoluteSizeSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        spanText.setSpan(new AbsoluteSizeSpan(20, true), 0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // DynamicDrawableSpan
        DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE) {
            @Override
            public Drawable getDrawable() {
                Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
                d.setBounds(0, 0, 50, 50);
                return d;
            }
        };
        DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) {
            @Override
            public Drawable getDrawable() {
                  Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
                  d.setBounds(0, 0, 50, 50);
                        return d;
                    }
                };
        spanText.setSpan(drawableSpan, 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spanText.setSpan(drawableSpan2, 7, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // ImageSpan
        spanText = new SpannableString("吾将上下而求索 -- http://google.com");
        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, 50, 50);
        spanText.setSpan(new ImageSpan(d), 3, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        displayText.append("\n");
        displayText.append(spanText);
        
        // RelativeSizeSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		// 参数proportion:比例大小
		spanText.setSpan(new RelativeSizeSpan(2.5f), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// ScaleXSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		//参数proportion:比例大小
		spanText.setSpan(new ScaleXSpan(3.8f), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// StyleSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		//Typeface.BOLD_ITALIC:粗体+斜体
		spanText.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// SubscriptSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		spanText.setSpan(new SubscriptSpan(), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// SuperscriptSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		spanText.setSpan(new SuperscriptSpan(), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// TextAppearanceSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		//若需自定义TextAppearance,可以在系统样式上进行修改
		spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// TypefaceSpan
		spanText = new SpannableString("吾将上下而求索 -- http://google.com");
		//若需使用自定义字体,可能要重写类TypefaceSpan
		spanText.setSpan(new TypefaceSpan("monospace"), 5, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		
		// URLSpan
		spanText = new SpannableString("吾将上下而求索 -- GOOGLE");
		spanText.setSpan(new URLSpan("http://google.com"), 10, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		displayText.append("\n");
		displayText.append(spanText);
		//让URLSpan可以点击
		displayText.setMovementMethod(new LinkMovementMethod());
        
    }
    
}

 

效果图片:


Android 各种Span示例
 

 多说一句:当开发中需要做一些特殊操作时,可以考虑继承以上合适的Span,然后做相关处理!

 

 

 

你可能感兴趣的:(android,ImageSpan,URLSpan)