android - ImageButton 上的 VectorDrawable 很难看

标签 android android-5.0-lollipop material-design android-vectordrawable

我有以下 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>

但是图像被缩放并且模糊且丑陋。

Ugly VectorDrawable screenshot

这是为什么呢?是VECTORDrawable,应该很流畅。

最佳答案

也许现在回答这个问题为时已晚,但可能还有其他人遇到同样的问题。

问题出在Vector定义中的width和height。您将其设置为较低的值,将其设置为较高的值,您就完成了。

如果你想将它用于多屏幕尺寸,那么你需要做这样的事情:

<vector ...
        android:width="@dimen/vector_size"
        android:height="@dimen/vector_size"
        .../>

然后在 res/values 中定义:

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

有关为什么需要为向量提供宽度和高度的更多信息,请阅读:

Understanding Android's VectorDrawable properties

关于android - ImageButton 上的 VectorDrawable 很难看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28460806/

相关文章:

android - 如何在 ICS 中以编程方式打开和关闭(启用和禁用)位置服务?

android - 如何以适当的方式缩放 View/ViewGroup - 动 Canvas 局?

java - 请求中的改造和常量参数

android-actionbar - 使用 Theme.Material.Light 时缺少 Logo /图标

javascript - 仅当用户单击某个字段后,AngularJS Material 才会将必填字段变为红色

android - RecyclerView 在项目之间创建边距

java - Android Studio 中的 IllegalAccessException 目标 android 为 4.4

colors - Android L - SwipeRefreshLayout 配色方案的 Material 设计

android - 如何在 Lollipop 之前的 Android 版本中更改工具栏的标题颜色

android - 工具栏仅在平板电脑尺寸上具有左右填充