【问题标题】:Android custom vector scalingAndroid 自定义矢量缩放
【发布时间】:2019-02-28 00:45:43
【问题描述】:

我正在尝试创建一个自定义形状以用作我的菜单抽屉的背景。我遇到的问题是我无法得到我想要的形状。目前,我在 Adob​​e Illustrator 中将形状创建为 svg,然后将 converting 转换为 XML 可绘制对象,但它没有达到预期的效果。如何根据其父级 (match_parent) 和内容 (wrap_content) 来缩放可绘制的矢量?

menu_shape.xml
我发现的一个问题是,这种形状的响应并不准确,因为它本质上是被挤压以适应视口,因此根据屏幕尺寸的不同表现得非常不同。

<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>

当前结果:

期望的结果:
想要的结果本质上是一个稍微向右偏移的大圆圈

【问题讨论】:

  • @Sha-1 你有 SVG 的形状吗?

标签: android android-drawable android-shape


【解决方案1】:

试试这个

<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>

【讨论】:

  • 这给了我想要的形状,但是我将如何根据布局内的内容来增加形状的高度?
  • 对于矢量可绘制,android:width 和 android:height 仅适用于 API
  • 我用layout_width: match_parentlayout_height: wrap_content创建了一个以drawable为背景的相对布局。然而,形状并没有根据它的内容进行调整,几乎就像形状的宽度和高度是永久设置的(它们似乎在您提供的 xml 中)。 RelativeLayout 的高度增加,但可绘制的背景没有
【解决方案2】:

试试shape xml代码

<?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>

【讨论】:

    猜你喜欢
    • 2020-05-12
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多