行内标签的实现
来源:互联网 发布:p2psearcher同类软件 编辑:程序博客网 时间:2024/06/05 02:29
序言
在最近的项目中需要实现行内标签,我原来觉得挺简单的后来,后来试了很多方式发现并没有那么简单。比如想到的几种实现方式和最终的效果如下图,本着辛苦我一个方便千万家的思想,我把我的TagUtil抽取出来了,并上传到GitHub上了。
实现思路
1.首先需要一个tag的布局文件
2.手动的填充布局文件,生成一个TextView
3.根据这个TextView获取它的截屏,生成一个Bitmap
4.将bitmap设置到需要显示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的截图,用来当Tag图标 Bitmap drawingCache = convertViewToBitmap(tv_tag); //将图片设置到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图标 * * @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; } /** * 可以居中对齐的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
总结
有一些事情并没有想象中那么简单,多积累一点一滴的技术,学会灵活运用技术,这样开发才会越来越简单,与君共勉。
1 0
- 行内标签的实现
- jquery ajax 实现标签容器内的局部刷新
- html中块级标签,行内标签,空元素的总结
- html的标签显示模式(块级标签,行内标签)
- css块级标签,行内标签,行内块标签的转换(2)
- HTML 块标签,行内标签,行内块标签以及之间的相互转换
- html head标签内的标签用途
- Head标签内的常用标签
- head标签内的设置
- iframe标签 行内框架
- 我的html-body内的标签
- 自定义标签的开发----内嵌标签的开发
- 前台读取数据库提取html标签内的内…
- 获取<span>标签内的值
- 正则获取HTML标签内的内容
- 寻找标签内的物体播放动画
- CRC32的C行内汇编实现
- 自定义标签内嵌Struts2标签的解决方法
- Python ProcessPoolExecutor logging Not pickle able
- Sass的编译环境
- MySQL 获得当前日期时间(以及时间的转换)
- 1220.Look for homework
- linux基本操作命令
- 行内标签的实现
- 大端小端格式详解
- Google webp
- JavaScript闭包理解
- python matplotlib 图表组织
- centos6.x yum安装最新mongodb数据库服务。
- 笑cry!在镜子面前,沉稳暖心的金毛同学也会瞬间化身逗比!
- Java 中Timer和TimerTask 定时器使用
- 使用display:table-cell元素部分出现空白缝隙的结解决