SpannableStringBuilder实现图文混排
1.我的后面添加图片
ssb = new SpannableStringBuilder("我的后面添加图片: ");
ssb.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), , , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

2.我的中间添加图片
ssb = new SpannableStringBuilder("我的中 间添加图片 ");
ssb.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), , , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

3.图片点击事件的处理
ssb = new SpannableStringBuilder("图片点击事件的处理 ");
ssb.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), , , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "图片点击事件的处理 ", Toast.LENGTH_SHORT).show();
}
}, , , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

4.背景带圆角,可设置颜色,角度
if (position % == ) {
strShow = "300英雄" + homeDataBean.User + ":" + homeDataBean.Content;
spannable = new SpannableStringBuilder(strShow);
spannable.setSpan(new RadiusBackgroundSpan(Color.parseColor("#fdc14f"), ),
, , Spannable.SPAN_INCLUSIVE_INCLUSIVE);
} else {
strShow = "枪界" + homeDataBean.User + ":" + homeDataBean.Content;
spannable = new SpannableStringBuilder(strShow);
spannable.setSpan(new RadiusBackgroundSpan(Color.parseColor("#9885fc"), ),
, , Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
/**
* 背景带圆角,可设置颜色,角度
* Created by g on 2018/1/23.
*/
public class RadiusBackgroundSpan extends ReplacementSpan { private int mSize;
private int mColor;
private int mRadius; /**
* @param color 背景颜色
* @param radius 圆角半径
*/
public RadiusBackgroundSpan(int color, int radius) {
mColor = color;
mRadius = radius;
} @Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
mSize = (int) (paint.measureText(text, start, end) + * mRadius);
//mSize就是span的宽度,span有多宽,开发者可以在这里随便定义规则
//我的规则:这里text传入的是SpannableString,start,end对应setSpan方法相关参数
//可以根据传入起始截至位置获得截取文字的宽度,最后加上左右两个圆角的半径得到span宽度
return mSize;
} @Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
int color = paint.getColor();//保存文字颜色
paint.setColor(mColor);//设置背景颜色
paint.setAntiAlias(true);// 设置画笔的锯齿效果
RectF oval = new RectF(x, y + paint.ascent(), x + mSize, y + paint.descent());
//设置文字背景矩形,x为span其实左上角相对整个TextView的x值,y为span左上角相对整个View的y值。paint.ascent()获得文字上边缘,paint.descent()获得文字下边缘
canvas.drawRoundRect(oval, mRadius, mRadius, paint);//绘制圆角矩形,第二个参数是x半径,第三个参数是y半径
paint.setColor(color);//恢复画笔的文字颜色
canvas.drawText(text, start, end, x + mRadius, y, paint);//绘制文字
}
}
6.设置图标:
if (!TextUtils.isEmpty(tagHorn)) {
int sixe = ScreenUtils.sp2px(mContext, );
EmojiconSpan imageSpan = new EmojiconSpan(mContext, R.mipmap.laba_icon,
(int) (sixe), DynamicDrawableSpan.ALIGN_BASELINE, (int) (sixe));
spannable.setSpan(imageSpan, strShow.indexOf(tagHorn), strShow.indexOf(tagHorn) + tagHorn.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
public class EmojiconSpan extends DynamicDrawableSpan {
private final Context mContext;
private final int mResourceId;
private final int mSize;
private final int mTextSize;
private int mHeight;
private int mWidth;
private int mTop;
private Drawable mDrawable;
private WeakReference<Drawable> mDrawableRef;
int nment = ;
public EmojiconSpan(Context context, int resourceId, int size, int alignment, int textSize) {
super(alignment);
mContext = context;
mResourceId = resourceId;
mWidth = mHeight = mSize = size;
mTextSize = textSize;
}
public Drawable getDrawable() {
if (mDrawable == null) {
try {
mDrawable = mContext.getResources().getDrawable(mResourceId);
mHeight = mSize;
mWidth = mHeight * mDrawable.getIntrinsicWidth() / mDrawable.getIntrinsicHeight();
mTop = (mTextSize - mHeight) / ;
mDrawable.setBounds(, mTop - nment, mWidth, mTop + mHeight);
} catch (Exception e) {
LogUtils.d(e.toString());
}
}
return mDrawable;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
//super.draw(canvas, text, start, end, x, top, y, bottom, paint);
Drawable b = getCachedDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY = top + ((bottom - top) / ) - ((b.getBounds().bottom - b.getBounds().top) / ) - mTop;
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
private Drawable getCachedDrawable() {
if (mDrawableRef == null || mDrawableRef.get() == null) {
mDrawableRef = new WeakReference<Drawable>(getDrawable());
}
return mDrawableRef.get();
}
}
https://blog.csdn.net/qq_33220645/article/details/53332834
SpannableStringBuilder实现图文混排的更多相关文章
- 使用android SpannableStringBuilder实现图文混排
项目开发中需要实现这种效果 多余两行,两行最后是省略号,省略号后面是下拉更多 之前用过的是Html.fromHtml去处理图文混排的,仅仅是文字后图片或者文字颜色字体什么的, 但是这里需要在最后文字的 ...
- 使用android SpannableStringBuilder实现图文混排,看到许多其他
项目开发需要达到这种效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmFuY3lsb3ZlamF2YQ==/font/5a6L5L2T/fontsiz ...
- 自定义图文混排视图MyImageTextView
http://blog.csdn.net/xujunfeng000/article/details/36399339?utm_source=tuicool&utm_medium=referra ...
- TextView + Spanned实现图文混排以及图片点击交互
最近要实现图文混排的需求,webview过大,所以想到了用SpannableStringBuilder来实现. 不过参考了大量国内文章,大多数是教你如何实现图文混排,并没有提及图片点击交互的.有翻阅了 ...
- 用NSAttributedString实现简单的图文混排
iOS7以后,因为TextKit的强大,可以用NSAttributedString很方便的实现图文混排(主要是利用了NSTextAttachment). 关于Textkit的牛逼之处,可以参考objc ...
- 图文混排--CoreText的简单运用
常见的在一些微博微信中可以看见一段文字中有不同的字体,字体有不同的颜色,并且可能会有一些笑脸之类的表情,这些可以通过图文混排做到. 图文混排可以通过WebView和CoreText做到,其他还有别的方 ...
- 【转】关于FLASH中图文混排聊天框的小结
原文链接 图文混排也是FLASH里一个很古老的话题了,我们不像美国佬那样游戏里面聊天框就是聊天框,全是文字干干净净,也不像日本人发明了并且频繁地使用颜文字.不管是做论坛.做游戏,必定要实现的一点就是带 ...
- DIV+CSS 图文混排的图片居中办法
不少人为了让 Div 图文混排的图片可以居中,给 IMG 套各式各样的 SPAN.DIV.LI 等等,以便于使用 text-align来进行居中. <div>图文混排 <br> ...
- ios图文混排
图文混排的形式 1. 富文本形式 2. core Text(文字排版) 3. TextKit 4. UIWebView 一.富文本 我们可以采用attributeString来进行图文混排.例如一个文 ...
随机推荐
- linux ftp配置及实操
一.基础知识: 1.ftp:file transfer protocal 及文件传输协,工作与应用层. 2.ftp协议的实现: 服务器端实现软件:vsftpd,pureftpd,filezilla s ...
- vCenter Server Appliance(VCSA )6.7部署指南
目录 简介 环境准备 开始安装 第一阶段安装 第二阶段安装 使用 简介 早期的VCSA支持 SUSE 和 Windows,不太懂SUSE,也不想用Windows 而在2018年4月17日VCSA 6. ...
- 学习spring和spring mvc过程中遇到的一些问题
以下仅为个人通过网上查找资料总结的内容,如有不对的地方欢迎提出: 注解: @Controller//表明本类是一个Controller @RequestMapping(value="/dem ...
- map.entrySet().iterator()
1.首先创建一个HashMap, Map map= new HashMap(); 2.Iterator iter= map.entrySet().iterator(); 首先是map.entrySet ...
- python字符串的特性及相关应用
一.字符串定义 字符串是 Python 中最常用的数据类型.用单引号(' '),双引号(" ")或者三引号(''' ''')括起来的数据称为字符串(其中,使用三引号的字符串可以横跨 ...
- LVS+Keepalived-DR模式(Apache)
Environment:4台CentOS机器 两台LVS 两台web服务器 LVS主备的操作,都需要安装ipvsadm和keepalived LVS主机操作 : 1.更改Keepalived的配置文件 ...
- #华为云·寻找黑马程序员# 如何实现一个优雅的Python的Json序列化库
在Python的世界里,将一个对象以json格式进行序列化或反序列化一直是一个问题.Python标准库里面提供了json序列化的工具,我们可以简单的用json.dumps来将一个对象序列化.但是这种序 ...
- react文本溢出hover气泡显示全部文本——JS判断文本溢出
需求: 在文本溢出的时候,显示气泡 JS相关知识 // target js元素 const containerLength = target.width; //当前容器的宽度 const textLe ...
- [TimLinux] Python 类型与运算
1. 内建(built-in)数据类型种类 数字类型:int(), float() 顺序(sequence): 字符串:str() 元祖:tuple() 列表:list() 字典:dict() 集合: ...
- ceph 网络配置
ceph 网络配置 9. 分离 public network 和 cluster network 9.1 分离的好处 (1)提高性能:消除副本创建.数据恢复和再平衡对 public network 的 ...