【发布时间】:2021-05-11 13:31:00
【问题描述】:
我想在我的应用中实现这个搜索栏。我为此创建了一个自定义视图。我面临的唯一问题是当圆形视图越过文本时如何将文本的颜色从黑色更改为白色。 The image showing the custom view
我的 onDraw 方法()
override fun onDraw(canvas: Canvas?) {
canvas?.save()
canvas?.drawCircle(thumbX + 20F, thumbY, thumbRadius, mThumbPaint)
canvas?.drawBitmap(drawTextBitmap(), 0F, 0F, Paint().apply {
isAntiAlias = true
})
}
drawTextBitmap 方法绘制 1 2 3 4 5 .....
private fun drawTextBitmap(): Bitmap {
val h = height
val w = width
val conf = Bitmap.Config.ARGB_8888
val bmp = Bitmap.createBitmap(w, h, conf)
val newCanvas: Canvas? = Canvas(bmp)
//newCanvas?.drawColor(Color.GREEN)
val initialOffsetX = paddingLeftCustom + thumbRadius
//newCanvas.drawColor(Color.BLACK)
for (i in 1..10) {
val textPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
textPaint.color = Color.BLACK
textPaint.textSize = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
20f,
resources.displayMetrics
)
.toInt().toFloat()
textPaint.textAlign = Paint.Align.LEFT
val metric: Paint.FontMetrics = textPaint.fontMetrics
val textHeight = Math.ceil((metric.descent - metric.ascent).toDouble()).toInt()
textPaint.colorFilter = PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.DST)
val y = (textHeight - metric.descent) as Float
newCanvas?.drawText(i.toString(), initialOffsetX + (i - 1) * spaceBetweenTexts, y, textPaint)
}
return bmp
}
我正在使用波特的达夫概念。我面临的问题是我想混合文本颜色和拇指颜色(圆形视图),但在我应用颜色过滤器的任何视图上都会考虑画布颜色。
【问题讨论】:
-
你必须分享一些代码到这个自定义视图。
标签: android