利用SpannableString实现标签TAG效果(可加圆角)

利用SpannableString实现标签TAG效果(可加圆角)_第1张图片
TagImageSpan.java
package com.example.administrator.myapplication;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.text.style.ImageSpan;

/**
 * Created by Administrator on 2015/10/30.
 */
public class TagImageSpan extends ImageSpan {
    public int expandWidth = 0; //设置之后可能会出现显示不全的问题,可通过TextView的 padding解决
    public int expandHeight = 0;//设置之后可能会出现显示不全的问题,可通过TextView的 padding解决

    public TagImageSpan() {
        this( 0, 0);
    }

    public TagImageSpan( int expandWidth, int expandHeight) {
        super(getShape());
        this.expandWidth = expandWidth;
        this.expandHeight = expandHeight;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        int width = getWidth(text, start, end, paint);
        int hight = getHeight(paint);
        getDrawable().setBounds(0, 0, width, hight);
        float bgX = x - (expandWidth * 0.5F); //使得在水平方向居中
        int bgBottom = bottom + expandHeight / 2;//使得在垂直方向居中
        super.draw(canvas, text, start, end, bgX, top, y, bgBottom, paint);
        canvas.drawText(text.subSequence(start, end).toString(), x, y, paint);
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return getWidth(text, start, end, paint);
    }

    /**
     * 计算span的宽度
     *
     * @param text
     * @param start
     * @param end
     * @param paint
     * @return
     */
    private int getWidth(CharSequence text, int start, int end, Paint paint) {
        return Math.round(paint.measureText(text, start, end)) + expandWidth;
    }

    /**
     * 计算span的高度
     *
     * @param paint
     * @return
     */
    private int getHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent) + expandHeight;
    }

    /**
     * 生成shape 可以通过xml实现
     * @return
     */
    private static GradientDrawable getShape() {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(10);
        drawable.setColor(Color.parseColor("#d8d8d8"));
        drawable.setStroke(1, Color.parseColor("#b2b2b2"));
        return drawable;
    }
}
Main2Activity.java
package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    private TextView text;
    private TextView text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        text = (TextView) findViewById(R.id.et);
        text2 = (TextView) findViewById(R.id.et2);

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        String[] texts = {"琅琊榜", "毙了狗了", "CP"};
        for (String s : texts) {
            SpannableString sp = new SpannableString(s);
            Object span = new TagImageSpan(0, 10);
            sp.setSpan(span, 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            stringBuilder.append(sp).append(",");
        }
        text.setText(stringBuilder);

        text2.setText("琅琊榜,毙了狗了,CP");

    }

}



你可能感兴趣的:(利用SpannableString实现标签TAG效果(可加圆角))