【问题标题】:VectorDrawable is not center alignedVectorDrawable 未居中对齐
【发布时间】:2017-01-14 02:37:54
【问题描述】:

我用我拥有的路径数据创建了一个 VectorDrawable 文件。但问题是图像没有在总区域的中心对齐,而是创建为左上对齐。看看:

文件:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">

    <path
        android:fillColor="@android:color/white"
        android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />

</vector>

现在检查它作为 app:srcCompat 用于 imageView 时的实际外观。

由于我对 VectorDrawables 没有太多经验,有什么办法可以解决这个问题?

我们将不胜感激。

编辑:这就是我使用矢量可绘制对象的方式。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/close_button"/>

【问题讨论】:

  • 发布您的图像查看代码
  • I've created one SVG file 否。这称为VectorDrawable,这是else。灵感来自但不等同于 SVG 文件。
  • 好的@Rotwang 所以最终我创建了VectorDrawable。如何使其居中对齐?
  • @DrilonBlakqori 请查看更新后的问题
  • 嗯.... 在你的 ImageView 中设置 android:gravity 和/或 android:layout_gravity 怎么样?

标签: android svg android-vectordrawable


【解决方案1】:

您指定矢量是 24x24,但路径不是那么大。如果我们真的检查它的尺寸,它的边界框坐标是:

     x: -0.034
     y: -0.033
 width: 13.369
height: 13.032

所以它只占据左上角大约 13x13 的区域。

您有多种选择来解决此问题,具体取决于您想要的结果。

解决方案 1

如果您希望将图标放大以占据整个 24x24 图标区域,那么将 viewportWidth 和 viewportHeight 更改为更合适的应该会起作用。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="13.4"
    android:viewportWidth="13.1">

    <path
        android:fillColor="@android:color/white"
        android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />

</vector>

我在这里通过改变视口所做的是告诉 Android VectorDrawable 的实际内容在 (0,0) 到 (13.4,13.1) 的区域内。这并不准确,但可能已经足够接近了。 Android 应该将该区域中的所有内容放大以填充 24x24 图标区域。

解决方案 2

另一种解决方案是将路径移动到 24x24 视口的中心。你可以使用 VectorDrawable &lt;group&gt; 标签来做到这一点。

我们需要对移动该路径的路径应用平移,使其居中。

现在路径的中心在:

x = -0.034 + 13.369/2
  = 6.651
y = -0.033 + 13.032/2
  = 6.483

我们希望将其移至 12,12。因此,我们将路径包装在一个组中,该组具有适当数量的 translateX 和 translateY 值。

我们需要沿 X 轴右移 (12 - 6.651) = 5.349,并且 我们需要向下移动 (12 - 6.483) = 5.517。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24"
    android:viewportWidth="24">

    <group translateX="5.349" translateY="5.517"> 
        <path
            android:fillColor="@android:color/white"
            android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />
    </group>
</vector>

当然,您也可以选择结合这两种方法。或者,如果您不仅需要移动十字,还需要放大一点,也可以在组中添加比例。

【讨论】:

  • 如何像你一样检查坐标,你能解释一下吗?
  • 只需创建一个带有路径的小 SVG。然后在路径元素上调用getBBox()。 jsfiddle.net/ckak2zr7/1
  • @PaulLeBeau 你从哪里得到 13.369 和 13.032?
  • @Red-M 我已经在你上面的 cmets 中回答了这个问题。
  • @the_prole VectorDrawable 使用的通用单位。如果 VD 以 24 x 24dp 的大小绘制,那么它实际上是在dp 中。那是因为它以与视口宽度/高度相同的大小绘制。如果以双倍尺寸 (48 x 48dp) 绘制,那么 5.349 实际上最终会变成 10.698dp。
【解决方案2】:

<group
    android:pivotX="12"
    android:pivotY="12"
    android:scaleX="0.75"
    android:scaleY="0.75">

<path
    android:fillColor="@android:color/white"
    android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />
</group>

关注 Group 标签,pivotX 和 pivotY 将定位图像,如果 viewportHeight 和 viewportWidth 为 24,则表示 pivotX 和 pivotY 12 会将其放置在中心,因为 24/2 是 12 :)

另一个重要的标签 scaleX 和 scaleY 将缩放图像的大小。

【讨论】:

    【解决方案3】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0">
    
        <group
              android:name="randomname"
              android:pivotX="12.0"
              android:pivotY="12.0">
    
        <path
            android:fillColor="@android:color/white"
            android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />
        </group>
    </vector>
    

    或将layout_gravity="center" 添加到您的 ImageView

    【讨论】:

    • 不,真的,您不想编辑 VectorDrawable 本身。只需将其容器视图与容器布局对齐即可。
    • 不,它不起作用,我认为它与 pathData 有关。
    【解决方案4】:

    我有同样的问题。我只是将宽度、高度、viewportWidth 和 viewportHeight 更改为:

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportHeight="24"
        android:viewportWidth="24">
    
    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
    
    </vector>
    

    【讨论】:

      【解决方案5】:

      您可以通过调整路径数据开头的 M 的 x 和 y 值来完成此操作。

      我并不是说这比 &lt;group&gt; 更好,但它是我发现在某些情况下有用的替代解决方案。

      原始路径:

      <path
              android:fillColor="@android:color/white"
              android:pathData="M7.144375,6.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />
      

      同一路径向右调整 100 向下调整 200:

      <path
              android:fillColor="@android:color/white"
              android:pathData="M107.144375,206.49965789 L13.196575,0.583973684 C13.333075,0.450552632 13.333075,0.233657895 13.196575,0.0995526316 C13.060075,-0.0331842105 12.838175,-0.0331842105 12.701675,0.0995526316 L6.649475,6.01592105 L0.596575,0.0995526316 C0.460775,-0.0331842105 0.238875,-0.0331842105 0.102375,0.0995526316 C-0.034125,0.233657895 -0.034125,0.450552632 0.102375,0.583973684 L6.154575,6.49965789 L0.102375,12.4153421 C-0.034125,12.5487632 -0.034125,12.7656579 0.102375,12.8997632 C0.170975,12.9661316 0.260575,12.9989737 0.350175,12.9989737 C0.439775,12.9989737 0.529375,12.9654474 0.597975,12.8997632 L6.650175,6.98339474 L12.702375,12.8997632 C12.770975,12.9661316 12.860575,12.9989737 12.950175,12.9989737 C13.039775,12.9989737 13.129375,12.9654474 13.197975,12.8997632 C13.334475,12.7656579 13.334475,12.5487632 13.197975,12.4153421 L7.145775,6.49965789 L7.144375,6.49965789 Z" />
      

      注意:其他答案已经指出了计算要调整的正确 X 和 Y 量的方法,此答案仅表示实施调整的另一种方法。

      【讨论】:

        猜你喜欢
        • 2015-07-25
        • 2019-01-08
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        相关资源
        最近更新 更多