当尺寸变小时,矢量可绘制像素化

Vector drawable is pixelated when size changes to smaller

我正在使用这张图片:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="32dp"
    android:height="39dp"
    android:viewportWidth="32"
    android:viewportHeight="39">
 <path
    android:pathData="M15.8575,6.5831L15.8575,1.0031C15.8575,0.1031 14.7775,-0.3369 14.1575,0.3031L6.5575,7.8831C6.1575,8.2831 6.1575,8.9031 6.5575,9.3031L14.1375,16.8831C14.7775,17.5031 15.8575,17.0631 15.8575,16.1631L15.8575,10.5831C23.3175,10.5831 29.2175,17.4231 27.5775,25.1631C26.6375,29.7031 22.9575,33.3631 18.4375,34.3031C11.2975,35.8031 4.9375,30.9031 3.9775,24.2831C3.8375,23.3231 2.9975,22.5831 2.0175,22.5831C0.8175,22.5831 -0.1425,23.6431 0.0175,24.8431C1.2575,33.6231 9.6175,40.1231 19.0775,38.2831C25.3175,37.0631 30.3375,32.0431 31.5575,25.8031C33.5375,15.5431 25.7375,6.5831 15.8575,6.5831Z"
    android:strokeWidth="1"
    android:fillColor="#fff"
    android:fillType="evenOdd"
    android:strokeColor="#00000000"/>
  <path
    android:pathData="M-34,-30h100v100h-100z"
    android:strokeWidth="1"
    android:fillType="evenOdd"
    android:strokeColor="#00000000"/>
</vector>

在此 ImageView 中:

<ImageView
   android:id="@+id/icon"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"
   android:scaleType="fitXY"/>

我正在使用焦点导航,因此我需要放大和缩小图像的尺寸。我在 onFocusChanged 方法中尝试了这些方法

override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
  if (gainFocus) {
    imageView.layoutParams.height = 60
    imageView.layoutParams.width = 60
  } else {
    imageView.layoutParams.height = 50
    imageView.layoutParams.width = 50
  }
}  

override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
 if (gainFocus) {
   imageView.drawable.setBounds(0,0,60,60)
 } else {
   imageView.drawable.setBounds(0,0,50,50)
 }
}

None 这两种方法都有效。缩小到较小状态时,图标会像素化。为什么会发生这种情况,我该如何解决?

注意:

minSdkVersion 26
targetSdkVersion 28

编辑: 我刚刚发现实际行为是这样的:

  1. 应用首次启动 - 图标处于缩小状态 - 未像素化
  2. 图标聚焦 - 放大状态 - 未像素化
  3. 图标未聚焦 - 缩小状态 - 像素化
  4. 图标聚焦 - 放大状态 - 未像素化 ...

编辑2: 根据评论,在这些组合中尝试了 mutate() 方法:

    if (gainFocus) {
        imageView.layoutParams.height = 60
        imageView.layoutParams.width = 60
        imageView.drawable.mutate().setBounds(0,0,60,60)
    } else {
        imageView.layoutParams.height = 50
        imageView.layoutParams.width = 50
        imageView.drawable.mutate().setBounds(0,0,50,50)
    }

然后一样,但是没有layoutParams 还有一个:

override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
 if (gainFocus) {
   imageView.drawable.setBounds(0,0,60,60)
 } else {
   imageView.drawable.setBounds(0,0,50,50)
 }
 imageView.invalidateDrawable(imageView.drawable.mutate())
}

没有任何帮助。 也尝试通过动画来做,但结果是一样的。我开始运行没主意了。

改用 png 解决了这个问题。向量在此范围内不可用。