最近公司的项目升级到了 9.x,随之而来的就是一大波的更新,其中有个比较明显的改变就是很多板块都出了一个带标签的设计图,如下:



怎么实现

看到这个,大多数小伙伴都能想到这就是一个简单的图文混排,不由得会想到鸿洋大佬的图文并排控件 MixtureTextView,或者自己写一个也不麻烦,只需要利用 shape 背景文件结合 SpannableString 即可。

确实如此,利用 SpannableString 确实是最方便快捷的方式,但稍不注意这里可能会踩坑。

private fun convertViewToBitmap(view: View): Bitmap {
view.isDrawingCacheEnabled = true
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
view.buildDrawingCache()
val bitmap = view.drawingCache
view.isDrawingCacheEnabled = false
view.destroyDrawingCache()
return bitmap
} fun setTagText(style: Int, content: String) {
val view = LayoutInflater.from(context).inflate(R.layout.layout_codoon_tag_textview, null)
val tagView = view.findViewById<CommonShapeButton>(R.id.tvName)
val tag = when (style) {
STYLE_NONE -> {
""
}
STYLE_CODOON -> {
tagView.setStrokeColor(R.color.tag_color_codoon.toColorRes())
tagView.setTextColor(R.color.tag_color_codoon.toColorRes())
"自营"
}
STYLE_JD -> {
tagView.setStrokeColor(R.color.tag_color_jd.toColorRes())
tagView.setTextColor(R.color.tag_color_jd.toColorRes())
"京东"
}
STYLE_TM -> {
tagView.setStrokeColor(R.color.tag_color_tm.toColorRes())
tagView.setTextColor(R.color.tag_color_tm.toColorRes())
"天猫"
}
STYLE_PDD -> {
tagView.setStrokeColor(R.color.tag_color_pdd.toColorRes())
tagView.setTextColor(R.color.tag_color_pdd.toColorRes())
"拼多多"
}
STYLE_TB -> {
tagView.setStrokeColor(R.color.tag_color_tb.toColorRes())
tagView.setTextColor(R.color.tag_color_tb.toColorRes())
"淘宝"
}
else -> {
""
}
}
val spannableString = SpannableString("$tag$content")
val bitmap = convertViewToBitmap(view)
val drawable = BitmapDrawable(resources, bitmap)
drawable.setBounds(0, 0, tagView.width, tagView.height)
spannableString.setSpan(CenterImageSpan(drawable), 0, tag.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
text = spannableString
gravity = Gravity.CENTER_VERTICAL
} companion object {
const val STYLE_NONE = 0
const val STYLE_JD = 1
const val STYLE_TB = 2
const val STYLE_CODOON = 3
const val STYLE_PDD = 4
const val STYLE_TM = 5
}

xml 文件的样式就不必在这里贴了,很简单,就是一个带 shape 背景的 TextView,不过由于 shape 文件的极难维护性,在我们的项目中统一采用的是自定义 View 来实现这些圆角等效果。

详细参考作者 blog:Android 项目中 shape 标签的整理和思考

圆角 shape 等效果不是我们在这里主要讨论的东西,我们来看这个代码,思路也是很清晰简洁:首先利用 LayoutInflater 返回一个 View,然后对这个 View 经过一系列判断逻辑确认里面的显示文案和描边颜色等处理。然后通过 ViewbuildDrawingCache() 的方法生成一个 Bitmap 供 SpannableString 使用,然后再把 spannableString 设置给 textView 即可。

一些注意点

其中有个细节需要注意的是,利用 LayoutInflater 生成的 View 并没有经过 measure()layout() 方法的洗礼,所以一定没对它的 widthheight 等属性赋值。

所以我们在 buildDrawingCache() 前做了至关重要的两步操作:

view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
view.layout(0, 0, view.measuredWidth, view.measuredHeight)

buildDrawingCache() 源码中我们可以看到,这个方法并不是一定会返回到正确的 Bitmap,在我们的 ViewCacheSize 大小超过了某写设备的默认值的时候,可能会返回 null。

系统给我了我们的默认最大的 DrawingCacheSize 为屏幕宽高乘积的 4 倍。

由于我们这里的 View 是极小的,所以暂时没有出现返回 null 的情况。

尽管上面的代码经过测试,基本上能在大部分机型上满足需求。但本着被标记 @Deprecated 的过时方法,我们坚决不用的思想,我们需要对生成 Bitmap 的方法进行小范围改造。

在最新的 SDK 中,我们发现 ViewbuildDrawingCache() 等一系列方法都已经被标记了 @Deprecated

/**
* <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
*
* @see #buildDrawingCache(boolean)
*
* @deprecated The view drawing cache was largely made obsolete with the introduction of
* hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
* layers are largely unnecessary and can easily result in a net loss in performance due to the
* cost of creating and updating the layer. In the rare cases where caching layers are useful,
* such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware
* rendering. For software-rendered snapshots of a small part of the View hierarchy or
* individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
* {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View. However these
* software-rendered usages are discouraged and have compatibility issues with hardware-only
* rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
* bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
* reports or unit testing the {@link PixelCopy} API is recommended.
*/
@Deprecated
public void buildDrawingCache() {
buildDrawingCache(false);
}

从官方注释中我们发现,使用视图渲染已经过时,硬件加速后中间缓存很多程度上都是不必要的,而且很容易导致性能的净损失。

所以我们采用 Canvas 进行简单改造一下:

private fun convertViewToBitmap(view: View): Bitmap? {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
val bitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_4444)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
view.draw(canvas)
return bitmap
}

突如其来的崩溃

perfect,但很不幸,在上 4.x 某手机上测试的时候,发生了一个空指针崩溃。



一看日志,发现我们在执行 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) 这句代码的时候抛出了系统层源码的 bug。

进入源码发现在 RelativeLayoutonMeasure() 中有这样一段代码。

if (isWrapContentWidth) {
// Width already has left padding in it since it was calculated by looking at
// the right of each child view
width += mPaddingRight; if (mLayoutParams != null && mLayoutParams.width >= 0) {
width = Math.max(width, mLayoutParams.width);
} width = Math.max(width, getSuggestedMinimumWidth());
width = resolveSize(width, widthMeasureSpec);
// ...
}
}

看起来没有任何问题,但对比 4.3 的源码,发现了一点端倪。

if (mLayoutParams.width >= 0) {
width = Math.max(width, mLayoutParams.width);
}

原来空指针报的是这个 layoutParams

再看看我们 inflate() 的代码。

val view = LayoutInflater.from(context).inflate(R.layout.layout_codoon_tag_textview, null)

对任何一位 Android 开发来讲,都是最熟悉的代码了,意思很简单,从 xml 中实例化 View 视图,但是父视图为 null,所以从 xml 文件实例化的 View 视图没办法 attachView 层次树中,所以导致了 layoutParams 这个参数为 null。

既然找到了原因,那么解决方案也就非常简单了。

只需要在 inflate() 后,再设置一下 params 就可以了。

view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

至此,基本已经实现,主要逻辑代码为:

/**
* 电商专用的 TagTextView
* 后面可以拓展直接设置颜色和样式的其他风格
*
* Author: nanchen
* Email: liusl@codoon.com
* Date: 2019/5/7 10:43
*/
class CodoonTagTextView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) { private var tagTvSize: Float = 0f init {
val array = context.obtainStyledAttributes(attrs, R.styleable.CodoonTagTextView)
val style = array.getInt(R.styleable.CodoonTagTextView_codoon_tag_style, 0)
val content = array.getString(R.styleable.CodoonTagTextView_codoon_tag_content)
tagTvSize = array.getDimension(R.styleable.CodoonTagTextView_codoon_tag_tv_size, 0f)
content?.apply {
setTagText(style, this)
}
array.recycle()
} private fun convertViewToBitmap(view: View): Bitmap? {
// view.isDrawingCacheEnabled = true
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
// view.buildDrawingCache()
// val bitmap = view.drawingCache
// view.isDrawingCacheEnabled = false
// view.destroyDrawingCache()
val bitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_4444)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
view.draw(canvas)
return bitmap
} fun setTagText(style: Int, content: String) {
val view = LayoutInflater.from(context).inflate(R.layout.layout_codoon_tag_textview, null)
view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
val tagView = view.findViewById<CommonShapeButton>(R.id.tvName)
val tag = when (style) {
STYLE_NONE -> {
""
}
STYLE_CODOON -> {
tagView.setStrokeColor(R.color.tag_color_codoon.toColorRes())
tagView.setTextColor(R.color.tag_color_codoon.toColorRes())
"自营"
}
STYLE_JD -> {
tagView.setStrokeColor(R.color.tag_color_jd.toColorRes())
tagView.setTextColor(R.color.tag_color_jd.toColorRes())
"京东"
}
STYLE_TM -> {
tagView.setStrokeColor(R.color.tag_color_tm.toColorRes())
tagView.setTextColor(R.color.tag_color_tm.toColorRes())
"天猫"
}
STYLE_PDD -> {
tagView.setStrokeColor(R.color.tag_color_pdd.toColorRes())
tagView.setTextColor(R.color.tag_color_pdd.toColorRes())
"拼多多"
}
STYLE_TB -> {
tagView.setStrokeColor(R.color.tag_color_tb.toColorRes())
tagView.setTextColor(R.color.tag_color_tb.toColorRes())
"淘宝"
}
else -> {
""
}
}
if (tag.isNotEmpty()) {
tagView.text = tag
if (tagTvSize != 0f) {
tagView.textSize = tagTvSize.toDpF()
}
// if (tagHeight != 0f) {
// val params = tagView.layoutParams
// params.height = tagHeight.toInt()
// tagView.layoutParams = params
// }
}
val spannableString = SpannableString("$tag$content")
val bitmap = convertViewToBitmap(view)
bitmap?.apply {
val drawable = BitmapDrawable(resources, bitmap)
drawable.setBounds(0, 0, tagView.width, tagView.height)
spannableString.setSpan(CenterImageSpan(drawable), 0, tag.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
}
text = spannableString
gravity = Gravity.CENTER_VERTICAL
} companion object {
const val STYLE_NONE = 0 // 不加
const val STYLE_JD = 1 // 京东
const val STYLE_TB = 2 // 淘宝
const val STYLE_CODOON = 3 // 自营
const val STYLE_PDD = 4 // 拼多多
const val STYLE_TM = 5 // 天猫
}
}

Android 从零编写一个带标签 TagTextView的更多相关文章

  1. CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL

    CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...

  2. 如何编写一个带命令行参数的Python文件

    看到别人执行一个带命令行参数的python文件,瞬间觉得高大上起来.牛逼起来,那么如何编写一个带命令行参数的python脚本呢?不用紧张,下面将简单易懂地让你学会如何让自己的python脚本,支持带命 ...

  3. Android简单的编写一个txt阅读器(没有处理字符编码),适用于新手学习

    本程序只是使用了一些基本的知识点编写了一个比较简单粗陋的txt文本阅读器,效率不高,只适合新手练习.所以大神勿喷. 其实想到编写这种程序源自本人之前喜欢看小说,而很多小说更新太慢,所以本人就只能找一个 ...

  4. 【Android自动化】编写一个log模块,输出至控制台,供程序运行查看

    # -*- coding:utf-8 -*- import logging def get_log(name): log = logging.getLogger(name) log.setLevel( ...

  5. django “如何”系列4:如何编写自定义模板标签和过滤器

    django的模板系统自带了一系列的内建标签和过滤器,一般情况下可以满足你的要求,如果觉得需更精准的模板标签或者过滤器,你可以自己编写模板标签和过滤器,然后使用{% load %}标签使用他们. 代码 ...

  6. 2.4 自己编写一个vivi驱动程序

    学习目标:从零编写一个vivi驱动程序,并测试: 一. vivi驱动应用程序调用过程 上节对xawtv对vivi程序调用欧城进行了详细分析,可总结为以下流程: 二.仿照vivi.c编写myvivi.c ...

  7. 创建带标签页的MDI WinForms应用程序

    http://www.cnblogs.com/island/archive/2008/12/02/mditab.html 创建MDI应用程序 先创建”Windows窗体应用程序”解决方案Tabable ...

  8. Android的NDK开发(2)————利用Android NDK编写一个简单的HelloWorld

    1.Android NDK简介 NDK全称为native development kit本地语言(C&C++)开发包.而对应的是经常接触的Android-SDK,(software devel ...

  9. Android逆向 编写一个Android程序

    本节使用的Android Studio版本是3.0.1 首先,我们先编写一个apk,后面用这个apk来进行逆向.用Android Studio创建一个新的Android项目,命名为Jhm,一路Next ...

随机推荐

  1. Scala中sortBy和Spark中sortBy区别

    Scala中sortBy是以方法的形式存在的,并且是作用在Array或List集合排序上,并且这个sortBy默认只能升序,除非实现隐式转换或调用reverse方法才能实现降序,Spark中sortB ...

  2. 更改yum 源

    刚建好的linux服务器,有很多依赖包没有安装,手动安装会非常麻烦,可以通过更改yum源,然后通过yum进行安装会非常方便 1)根据服务器版本找到对应得镜像文件,并将 .iso 结尾的镜像文件上传到l ...

  3. 基于 HTML5 换热站可视化应用

    换热站是整个热网系统中最核心的环节,它将一侧蒸汽或高温水通过热交换器换成可以直接进入用户末端的采暖热水.换热站控制系统是集中供热监控系统的核心部分,换热站控制系统既可独立工作,也可以接受调度中心的监督 ...

  4. virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护

    virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护 两个方面:对应用端 & 对DOM端(渲染准备的计算) 1.将DOM状态的维护工作由系统维护转交给vi ...

  5. 【java】javac命令在win10不可用,提示javac不是内部或外部命令,也不是可运行的程序【解决方法】

    JDK安装成功,并且配置了环境变量,java命令正常可以使用,但是javac命令提示 不是内部或外部命令,也不是可运行的程序 解决方法: 产生这个问题的原因,是因为环境变量的配置中,Path中配置使用 ...

  6. RookeyFrame2.0发布,UI重构

    RookeyFrame2.0在原来1.0的基础上进行了UI的重构,设计了扁平化的样式风格,看起来更清爽,更赏心阅目,由于之前工作比较忙升级比较慢,后续会投入比较多时间来更新维护,同时针对二次开发项目做 ...

  7. .NetCore打包docker镜像

    1..NetCore 项目打包成Docker 镜像 1.1创建一个.NetCore web项目 项目名为   testmvc  此处用的是.NetCore2.1版本 1.2并且在program里面设置 ...

  8. Go 笔记之如何防止 goroutine 泄露

    今天来简单谈谈,Go 如何防止 goroutine 泄露. 概述 Go 的并发模型与其他语言不同,虽说它简化了并发程序的开发难度,但如果不了解使用方法,常常会遇到 goroutine 泄露的问题.虽然 ...

  9. topshelf注册服务

    1.需要从nutget上获取toshelf配置 2.代码 using Common.Logging; using Quartz; using Quartz.Impl; using System; us ...

  10. android studio学习---实时布局(Live Layout)

    Android Studio中的实时布局功能允许大家在无需将应用程序运行在设备或者模拟器中的前提下,直接预览应用的用户界面.实时布局是一款极为强大的工具,能够帮助开发者节约大量时间.在实时布局的帮助下 ...