簡體   English   中英

以編程方式在可繪制的矢量上清除色調?

[英]Clear tint on a vector drawable programmatically?

我正在嘗試切換ImageView的色調,其來源是矢量可繪制對象:

    <ImageView
    android:id="@+id/iv_lightbulb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:srcCompat="@drawable/ic_lightbulb" />

看看這個網站上的其他解決方案,我在我的 recyclerview 適配器中的 imageview 上嘗試了這個:

lightbulbIV.setOnClickListener {
                // read tint of vector drawable, unfilled by default or yellow
                val currentColor =
                    ImageViewCompat.getImageTintList(lightbulbIV)?.defaultColor
                if (currentColor == null) {
                    // make vector drawable yellow
                    ImageViewCompat.setImageTintList(
                        lightbulbIV,
                        ColorStateList.valueOf(
                            ContextCompat.getColor(parent.context, R.color.yellow)
                        )
                    )
                } else
                    // unfill the vector drawable 
                    ImageViewCompat.setImageTintList(lightBulbIV, null)

帶有 null 的setImageTintList只是完全刪除圖像,這不是我想要lightBulbIV.clearColorFilter()也不會產生預期的效果。 我需要做什么來清除色調,以便下次單擊圖像時將其設置為色調?

這個對我有用。 你能試試這個代碼嗎?

    logo.setOnClickListener {
        // read tint of vector drawable, unfilled by default or yellow
        val currentColor =
            ImageViewCompat.getImageTintList(logo)?.defaultColor
        if (currentColor == null) {
            // make vector drawable yellow
            ImageViewCompat.setImageTintList(
                logo,
                ColorStateList.valueOf(
                    ContextCompat.getColor(applicationContext, R.color.purple_200)
                )
            )
        } else
        // unfill the vector drawable
            ImageViewCompat.setImageTintList(logo, null)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM