Android custom vector scaling

不打扰是莪最后的温柔 提交于 2021-01-29 19:12:24

问题


I am attempting to create a custom shape to use as the background for my menu drawer. The issue i'm experiencing is that i can't get the shape i want. At the moment i am creating the shape as an svg in Adobe Illustrator and then converting that into an XML drawable but it isn't having the desired effect. How can i get the vector drawable to scale depending on its parent (match_parent) and content (wrap_content)?

menu_shape.xml
An issue i've picked up on this is that this shape isn't exactly responsive as it is essentially being squeezed to fit the viewport and thus behaves extremely different depending on the screen size.

<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
    android:viewportWidth="597.28"
    android:viewportHeight="542.16"
    android:width="597.28dp"
    android:height="542.16dp">
    <path
        android:pathData="M0.5 303.39V0.5H596.78V370.39S310.5 797.13 0.5 303.39Z"
        android:fillColor="#A92324"
        android:strokeColor="#231F20"
        android:strokeWidth="1"
        android:strokeMiterLimit="10" />
</vector>

Current result:

Desired result:
The desired result is essentially a massive circle that is offset to the right slightly


回答1:


Try this

<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt"
        android:viewportWidth="597.28"
        android:viewportHeight="542.16"
        android:width="597.28dp"
        android:height="542.16dp">
  <path
          android:pathData="M0.5 302.39V0.5H596.7V374.39S310.5 501.13 0.5 303.39Z"
          android:fillColor="#A92324"
          android:strokeColor="#231F20"
          android:strokeWidth="1"
          android:strokeMiterLimit="10" />
</vector>



回答2:


Try shape xml code

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">

                <padding android:left="-100dp"
                    android:top="-100dp"
                    android:right="-100dp"/>
            </shape>
        </item>
        <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="oval">
                <solid
                    android:color="#712EF1" />
            </shape>
        </item>
    </layer-list>


来源:https://stackoverflow.com/questions/54916710/android-custom-vector-scaling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!