android - 包含自定义形状的 ShapeDrawable 的 setTint?

标签 android android-drawable

在包含自定义 ShapeShapeDrawable 上调用 setTint() 似乎对基础形状的颜色没有影响。

CustomShape.kt

class CustomShape : Shape() {

    private val paint = Paint()
    private val path = Path()

    init {
        paint.isAntiAlias = true
    }

    override fun onResize(width: Float, height: Float) {
        // update path
    }

    override fun draw(canvas: Canvas, paint: Paint) {
        canvas.drawPath(path, this.paint)
    }
}

用法

val shape = CustomShape()
val drawable = ShapeDrawable(shape)
drawable.setTint(Color.RED) // not working
someView.background = drawable

最佳答案

解决方案

使用 draw() 提供的 Paint 对象,它已经应用了反别名标志,并且会尊重您在 ShapeDrawable< 上调用的任何方法.


问题是我正在创建和使用一个新的 Paint 对象,而不是使用 draw() 中提供的对象。这样做的原因是我需要打开抗锯齿,我想避免在 draw 方法中这样做。

此外,我最初是直接在 CustomShape 中给 Paint 对象一个颜色,然后才意识到让 ShapeDrawable 可能更好/必要处理那个。

关于android - 包含自定义形状的 ShapeDrawable 的 setTint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684972/

相关文章:

android - 如何将 LayerDrawable 转换为位图?

android - WebView 流量监控器

android - 我的应用程序的 Google 商店版本崩溃了

java - 如何获取 Google 返回的 JSON 对象中的持续时间和距离?

android - 如何在 Android 中绘制多行文本并在 Canvas 上旋转

android:drawableLeft margin 和/或 padding

android - 我什么时候可以首先测量 View ?

android - 如何知道电子邮件已发送或保存或丢弃...?

android - 如何在圆内画圆弧?

android - TabHost TabSpec setIndicator 中的标签和图标是否相互排斥?