ImageButton上的VectorDrawable很丑

4
我有以下的ImageButton
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fireButton"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ic_whatshot_48px"
    android:tint="@color/accent"
    style="?android:attr/borderlessButtonStyle"
    android:scaleType="fitCenter"
    android:onClick="fire" />
ic_whatshot_48px是一个VectorDrawable
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="48"
    android:viewportHeight="48">

    <path
        android:fillColor="#000000"
        android:pathData="M27 1.34s1.48 5.3 1.48 9.6c0 4.12-2.7 7.47-6.83
7.47s-7.25-3.34-7.25-7.47l.05-.72C10.43 15.03 8 21.23 8 28c0 8.84 7.16 16 16
16s16-7.16 16-16c0-10.79-5.19-20.41-13-26.66zM23.42 38c-3.56
0-6.45-2.81-6.45-6.28 0-3.25 2.09-5.53 5.63-6.24s7.2-2.41 9.23-5.15c.78 2.58
1.19 5.3 1.19 8.07 0 5.29-4.3 9.6-9.6 9.6z" />
</vector>

但是这个图片被缩放后变得模糊、丑陋。

丑陋的VectorDrawable截图

为什么呢?它是VECTORDrawable,应该是漂亮而且平滑的。


我们能看到VectorDrawable的代码吗? - Chris Handy
2
可能是因为你的SVG尺寸为48x48像素? - Zielony
1
这是一个框架问题,与矢量可绘制对象的缓存方式有关。这个问题已经在未来的版本中得到了修复。 - alanv
@alanv 当你说“这是一个框架问题”时,这是否也会影响第三方向后兼容的VectorDrawable库? - gtcompscientist
1
这个问题比较新VectorDrawable被放缩后会变得模糊,但是它有一个非常好的解释,说明了为什么会出现这种情况。 - Roman
显示剩余4条评论
1个回答

2
也许现在已经太晚回答了,但可能还有其他人遇到了同样的问题。
问题是由于矢量定义中的宽度和高度引起的。将其设置为低值时,将其设置为较高值即可解决问题。
如果您想将其用于多种屏幕大小,则需要这样做:
<vector ...
        android:width="@dimen/vector_size"
        android:height="@dimen/vector_size"
        .../>

然后在 res/values 中定义:

<resources>
    <dimen name="vector_size">24dp</dimen>
</resources>

关于为什么需要为矢量图指定宽度和高度的更多信息,请阅读:

了解Android的VectorDrawable属性


这不是真的。在 ImageView 中使用 srcCompat 应该可以工作。你说的是默认大小的问题。 - cesards

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接