所以我们采用 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。
进入源码发现在 RelativeLayout 的 onMeasure() 中有这样一段代码。
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() 的代码。
对任何一位 Android 开发来讲,都是最熟悉的代码了,意思很简单,从 xml 中实例化 View 视图,但是父视图为 null,所以从 xml 文件实例化的 View 视图没办法 attach 到 View 层次树中,所以导致了 layoutParams 这个参数为 null。
既然找到了原因,那么解决方案也就非常简单了。
只需要在 inflate() 后,再设置一下 params 就可以了。
至此,基本已经实现,主要逻辑代码为:
/** * 电商专用的 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 // 天猫 } }