0

我有一个layout_image这样命名的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/grumpy_cat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我在几个活动/片段中包含 XML

<include
    android:id="@+id/llytImage"
    layout="@layout/layout_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

imageView我通过使用 kotlinx 合成访问它在 Activity A 中设置了一个颜色过滤器:

import kotlinx.android.synthetic.main.layout_image.view.*

llytImage.imageView.setColorFilter(Color.RED)

它变成红色,我导航到活动 B。活动 B 还包含具有相同 ID 的相同 XML 布局。但是,imageView在活动 B 中仍然是红色的。

似乎 Kotlin 缓存了状态imageView并使用相同的视图来实现更好的性能。

但是,我不希望它缓存imageView。我需要默认状态。我尝试了两种方法来阻止 Kotlin 缓存它:

  1. @ContainerOptions(CacheImplementation.NO_CACHE)在包含 XML 的活动中使用注释
  2. imageView.clearFindViewByIdCache()导航到 Activity B 时调用

他们都没有为我工作。

如果您知道真正的解决方案,我将不胜感激。谢谢

4

1 回答 1

0

mutate()通过调用为我操作可绘制而不是图像视图:

import kotlinx.android.synthetic.main.layout_image.view.*

val drawable = llytImage.imageView.drawable.mutate()
val colorFilter = PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN)
drawable.colorFilter = colorFilter
于 2019-05-30T14:23:12.120 回答