行內(nèi)標(biāo)簽的實(shí)現(xiàn)

序言

在最近的項(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();
        }
    }
}

源碼

TAGDemo

總結(jié)

有一些事情并沒有想象中那么簡(jiǎn)單,多積累一點(diǎn)一滴的技術(shù),學(xué)會(huì)靈活運(yùn)用技術(shù),這樣開發(fā)才會(huì)越來越簡(jiǎn)單,與君共勉。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評(píng)論 25 709
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,330評(píng)論 0 17
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,161評(píng)論 22 665
  • 一、雞湯 appwidget是android中小組件,我們經(jīng)常說的widget其實(shí)是指的那些button、text...
    歡樂斗佛閱讀 2,393評(píng)論 1 8
  • 胖小葵今天看了簡(jiǎn)書,一篇文章“你為什么不能堅(jiān)持練字”,她寫的字很有風(fēng)格,正式我喜歡的那種,飄逸而不失剛韌,給她發(fā)了...
    蔻兮閱讀 280評(píng)論 0 0

友情鏈接更多精彩內(nèi)容