实现效果:多行文字开头是一个标签图片

布局中,

android:ellipsize="end"

android:maxLines="2"

android:letterSpacing="0.04"

android:lineSpacingExtra="3.3dp"

android:gravity="center_vertical"

android:layout_gravity="center_vertical"

android:id="@+id/tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="25dp"

android:text=""

android:textColor="#000000"

android:textSize="15sp" />

标签图片:(这个切图上下白边大小不一样,所以效果上竖直方向有点没居中,建议使用四周无白边的切图)

Activity中使用SpannableString将开头位置替换为标签图片,用了一个CenterAlignImageSpan来解决图片与文字居中对齐

TextView mTv = (TextView) this.findViewById(R.id.tv);

String str1 = " ";

String str2 = "北京首条自行车高速本周开通 限速15公里/小时,这条路东起昌平回龙观," +

"西至海淀区的上地西路和后厂村路交叉口,全长6.5公里骑行最高时速不超过15公里,禁止行人";

String str = str1 + str2;

SpannableString spannableString = new SpannableString(str);

Drawable d = getResources().getDrawable(R.drawable.icon_new);

int iconwidth = DensityUtil.dip2px(this,25);//27

int iconHeight = DensityUtil.dip2px(this,16);//15

d.setBounds(0,0,iconwidth,iconHeight);

//居中对齐imageSpan

CenterAlignImageSpan imageSpan1 = new CenterAlignImageSpan(d);

spannableString.setSpan(imageSpan1, 0, 1, ImageSpan.ALIGN_BASELINE);

mTv.setLineSpacing(12f,1);

mTv.setText(spannableString);

CenterAlignImageSpan类

public class CenterAlignImageSpan extends ImageSpan {

public CenterAlignImageSpan(Drawable drawable) {

super(drawable);

}

public CenterAlignImageSpan(Bitmap b) {

super(b);

}

@Override

public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {

Drawable b = getDrawable();

Paint.FontMetricsInt fm = paint.getFontMetricsInt();

int transY = (y + fm.descent + y + fm.ascent) / 2 - b.getBounds().bottom / 2;//计算y方向的位移

canvas.save();

canvas.translate(x, transY);//绘制图片位移一段距离

b.draw(canvas);

canvas.restore();

}

}

android图片添加多行文字,Android使用SpannableString实现多行文字开头放置标签图案...相关推荐

  1. android水印控件,Android图片添加文字水印并保存水印文字图片到指定文件

    Android图片添加文字水印并保存水印文字图片到指定文件package zhangphil.test;import android.graphics.Bitmap;import android.gr ...

  2. android图片上加有汉字,Android 为图片添加文字水印

    先上图: 上代码 ------------------------MainActivity------------------------ package com.example.zhuoshi.wa ...

  3. android图片上水印字体颜色,Android给图片添加文字和水印

    话不多说 上图 gif5新文件.gif public class ImageUtil { /** * 设置水印图片在左上角 * * @param context 上下文 * @param src * ...

  4. Android 图片添加白边

    /*** 图片添加白边* @param srcBitmap 源位图* @return 返回带白边的位图*/private Bitmap createWhiteBorderBitmap(Bitmap s ...

  5. android button 添加事件_2019最新Android常用开源库总结

    前言 收集了一些比较常见的开源库,特此记录(已收录350+).另外,本文将持续更新,大家有关于Android 优秀的开源库,也可以在下面留言. 一 .基本控件 TextView HTextView 一 ...

  6. android标题栏添加按钮_2019最新Android常用开源库总结(持续更新,建议收藏)

    热文导读|   点击标题阅读 如何才能成为优秀的架构师? 23种设计模式及案例整理分享(建议收藏) 程序员的35个坏习惯,你有几条? 作者:欢子-3824 来源:https://blog.csdn.n ...

  7. android 图片在哪里设置时间,android 按时间显示图片

    实现效果: 首先写需要用到的图片数据:时间.文件名.路径等 package com.example.a550211.cd; import java.text.SimpleDateFormat; imp ...

  8. android 图片相似度比对,Android图片对比(图片相似度)代码

    声明 这俩天在做图片对比的工具,这里将对比的核心功能代码贴上来共同学习,有什么缺点和不足请大家指出,谢谢.Lee出品,转载请注明出处:http://blog.csdn.net/hnulwt/artic ...

  9. android 图片闪光动画_剖析Android动画(图片闪烁、左右摇摆、上下晃动等效果) | 学步园...

    图片闪烁,类似这样. 2011-11-22 16:18 上传 左右摇摆: 2011-11-22 17:07 上传 一.续播  (不知道取什么名字好,就是先播放动画A, 接着播放动画B) 有两种方式. ...

  10. android动态添加布局属性 layoutparmas,Android 代码动态布局 LayoutParams 使用

    先来看一个简单的布局,先用xml写 xmlns:tools="http://schemas.android.com/tools" android:layout_width=&quo ...

最新文章

  1. 建立可扩展的silverlight应用框架 step-4
  2. python字符串用法详解(str、下标、切片、查找、修改、判断)
  3. 004 人物数据查找和代码编写
  4. 关于VECTOR和DEQUE
  5. DP专辑之最长公共子序列及其变形
  6. Unity下个月将推出开源VR编辑器,让VR内容开发so easy
  7. Struts2中访问HttpServletRequest和HttpSession
  8. manjaro linux 教程,Manjaro 使用基础
  9. 十分钟搞定特征值和特征向量
  10. dockerfile详解-dockerfile构建过程解析,dockerfile体系结构保留字解析,案例解析
  11. Linux Bash Shell中的特殊参数含义
  12. 乘法口诀练习(python-for循环)
  13. JavaWeb开发中的乱码问题
  14. redis+tomcat实现session共享
  15. 20200712每日一句
  16. 用于旅行商问题的离散布谷鸟算法
  17. 【Unity5】实现24小时日出日落的效果 24小时天幕系统
  18. C++内部链接与外部链接
  19. 在线电子书阅读微信小程序 毕业设计(4)图书详细页-图书目录
  20. Unity实现跨场景的传送门

热门文章

  1. 单元格中指定内容标红_你一直都不知道,键盘上“F4”在office中的强大功能
  2. 学习webpack系列之二 ---- (管理静态资源)
  3. 存储设备在linux名称,Linux下的存储设备的管理
  4. jQuery简单好用的JavaScript代码库略解使用
  5. linux有线程的概念,Linux线程相关概念
  6. java底层 文件操作,java底层是怎样对文件操作的
  7. comon lisp标准_Common Lisp 的一些函数
  8. 求栈中元素个数算法_嵌入式必知基础算法(一)
  9. 转 基于.NET平台的分层架构实战
  10. ASP.NET读取自定义的config文件