android - 当尺寸变小时,矢量可绘制对象像素化
问题描述
我正在使用这张图片:
<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)
}
}
这两种方法都不起作用。图标缩小到较小状态时像素化。为什么会发生这种情况,我该如何解决?
注意:
minSdkVersion 26
targetSdkVersion 28
编辑:我刚刚发现实际行为是这样的:
- 应用程序首次启动 - 图标处于缩小状态 - 未像素化
- 图标聚焦 - 放大状态 - 未像素化
- 图标未聚焦 - 缩小状态 - 像素化
- 图标聚焦 - 放大状态 - 未像素化......
EDIT2:根据评论,在这些组合中尝试了 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 解决了这个问题。向量在此范围内不可用。
推荐阅读
- javascript - 查找最后一次说出一个词
- android - 无法解决 response.body().string() 的问题
- jupyter-notebook - Jupyter 中单元格之间的乳胶方程数重置为 1
- node.js - “npm install”安装不熟悉的文件
- c - 如何使用 Bash shell 脚本存储 C 程序的输出
- python-3.x - 通过优先考虑另一列中的文本来分组熊猫
- javascript - 通过索引交换js数组中的元素
- javascript - 如何使用 Jquery 获取具有特定 id 的 div 值?
- python - Python Telegram Bot - run_daily 不起作用
- android - 我可以删除软键盘中的点(。)吗?