序言
在最近的項(xiàng)目中需要實(shí)現(xiàn)行內(nèi)標(biāo)簽,我原來覺得挺簡(jiǎn)單的后來,后來試了很多方式發(fā)現(xiàn)并沒有那么簡(jiǎn)單。比如想到的幾種實(shí)現(xiàn)方式和最終的效果如下圖,本著辛苦我一個(gè)方便千萬家的思想,我把我的TagUtil抽取出來了,并上傳到GitHub上了。
這里寫圖片描述
實(shí)現(xiàn)思路
1.首先需要一個(gè)tag的布局文件
這里寫圖片描述
2.手動(dòng)的填充布局文件,生成一個(gè)TextView
3.根據(jù)這個(gè)TextView獲取它的截屏,生成一個(gè)Bitmap
4.將bitmap設(shè)置到需要顯示tag的TextView上,這和TextView中顯示表情的原理是一樣的。
代碼如下
package com.trs.nxnews.news.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.trs.nxnews.R;
import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;
/**
* Created by zhuguohui on 2016/12/12.
*/
public class TagUtil {
public static void setTag(String tag, TextView textView) {
if (TextUtils.isEmpty(tag) || "無".equals(tag)) {
return;
}
//填充tag布局
TextView tv_tag = (TextView) LayoutInflater.from(textView.getContext()).inflate(R.layout.view_tag, null, true);
tv_tag.setText(tag);
//獲取View的截圖,用來當(dāng)Tag圖標(biāo)
Bitmap drawingCache = convertViewToBitmap(tv_tag);
//將圖片設(shè)置到TextView中
BitmapDrawable bitmapDrawable = new BitmapDrawable(drawingCache);
bitmapDrawable.setBounds(0, 0, drawingCache.getWidth(), drawingCache.getHeight());
SpannableString spannableString = new SpannableString(tag + textView.getText());
MyIm imageSpan = new MyIm(bitmapDrawable);
spannableString.setSpan(imageSpan, 0, tag.length(), SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
/**
* 獲取tag圖標(biāo)
*
* @param view
* @return
*/
public static Bitmap convertViewToBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
/**
* 可以居中對(duì)齊的ImageSPan
*/
public static class MyIm extends ImageSpan {
public MyIm(Context arg0, int arg1) {
super(arg0, arg1);
}
public MyIm(Drawable drawable) {
super(drawable);
}
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
Drawable d = getDrawable();
Rect rect = d.getBounds();
if (fm != null) {
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.bottom - fmPaint.top;
int drHeight = rect.bottom - rect.top;
int top = drHeight / 2 - fontHeight / 4;
int bottom = drHeight / 2 + fontHeight / 4;
fm.ascent = -bottom;
fm.top = -bottom;
fm.bottom = top;
fm.descent = top;
}
//15為padding
return rect.right + 15;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
Drawable b = getDrawable();
canvas.save();
int transY = 0;
transY = ((bottom - top) - b.getBounds().bottom) / 2 + top;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
}
源碼
總結(jié)
有一些事情并沒有想象中那么簡(jiǎn)單,多積累一點(diǎn)一滴的技術(shù),學(xué)會(huì)靈活運(yùn)用技術(shù),這樣開發(fā)才會(huì)越來越簡(jiǎn)單,與君共勉。