VectorDrawable在API 21中无法正确呈现

时间:2018-09-25 21:45:53

标签: android svg drawable android-vectordrawable

此向量在API 24甚至API 16中都可以很好地呈现,但是由于某种原因,它在API 21中无法正确呈现。我已经尝试将属性fillType从evenOdd更改为nonZero,但这没有用。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24">
      <path
          android:pathData="M5.129,11.4073C5.129,8.5179 7.4493,6.1767 10.3111,6.1767C13.1728,6.1767 15.493,8.5179 15.493,11.4073C15.493,14.2957 13.1728,16.6377 10.3111,16.6377C7.4493,16.6377 5.129,14.2957 5.129,11.4073M12.6329,18.6069C12.3232,19.609 12.3427,20.635 12.67,21.5405C11.9123,21.7193 11.1226,21.8141 10.3111,21.8141C4.6165,21.8141 0,17.1551 0,11.4073C0,5.6597 4.6165,1 10.3111,1C13.1386,1 15.7,2.1494 17.5627,4.0101L17.5627,14.4885C16.6515,14.6136 15.7446,14.969 14.9499,15.5195C13.8321,16.2937 13.0093,17.3902 12.6329,18.6069ZM10.2122,13.0279C11.1499,13.0279 11.9096,12.2608 11.9096,11.3143C11.9096,10.3682 11.1499,9.6015 10.2122,9.6015C9.2752,9.6015 8.5146,10.3682 8.5146,11.3143C8.5146,12.2608 9.2752,13.0279 10.2122,13.0279ZM10.2122,7.8328C12.113,7.8328 13.6549,9.389 13.6549,11.3079C13.6549,13.2264 12.113,14.7828 10.2122,14.7828C8.3108,14.7828 6.7693,13.2264 6.7693,11.3079C6.7693,9.389 8.3108,7.8328 10.2122,7.8328ZM20.1297,7.1933L20.1297,18.7806L20.1033,18.7806C20.106,19.8388 19.4816,20.9914 18.3807,21.7536C16.786,22.8578 14.7952,22.7251 13.9339,21.4572C13.0722,20.1902 13.6664,18.2669 15.2608,17.1631C16.3862,16.3835 17.7074,16.2215 18.6912,16.6372L18.6912,7.6493L18.6912,3.9714C18.6912,3.9714 26.0377,4.4141 23.0833,11.5972C23.0833,11.5972 23.6886,7.4605 20.1297,7.1933Z"
          android:strokeWidth="1"
          android:fillColor="#FFFFFF"
          android:fillType="evenOdd"
          android:strokeColor="#00000000"/>
    </vector>

这是Android Studio显示预览的方式

enter image description here

这就是我将可绘制对象设置为扩展Button的自定义类的方法

private void initDrawables(Context context, AttributeSet attrs) {
    if (attrs != null) {
        TypedArray attributeArray = context.obtainStyledAttributes(
                attrs,
                R.styleable.ApaTextView);

        Drawable drawableStart = null;
        Drawable drawableEnd = null;
        Drawable drawableBottom = null;
        Drawable drawableTop = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawableStart = attributeArray.getDrawable(R.styleable.ApaTextView_drawableStartCompat);
            drawableEnd = attributeArray.getDrawable(R.styleable.ApaTextView_drawableEndCompat);
            drawableBottom = attributeArray.getDrawable(R.styleable.ApaTextView_drawableBottomCompat);
            drawableTop = attributeArray.getDrawable(R.styleable.ApaTextView_drawableTopCompat);
        } else {
            final int drawableStartId = attributeArray.getResourceId(R.styleable.ApaTextView_drawableStartCompat, -1);
            final int drawableEndId = attributeArray.getResourceId(R.styleable.ApaTextView_drawableEndCompat, -1);
            final int drawableBottomId = attributeArray.getResourceId(R.styleable.ApaTextView_drawableBottomCompat, -1);
            final int drawableTopId = attributeArray.getResourceId(R.styleable.ApaTextView_drawableTopCompat, -1);

            if (drawableStartId != -1)
                drawableStart = AppCompatResources.getDrawable(context, drawableStartId);
            if (drawableEndId != -1)
                drawableEnd = AppCompatResources.getDrawable(context, drawableEndId);
            if (drawableBottomId != -1)
                drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
            if (drawableTopId != -1)
                drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
        }


        if (Build.VERSION.SDK_INT >= 17) {
            setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
        }

        attributeArray.recycle();
    }
}

XML:

<com.amco.components.ApaButton
        android:id="@+id/button_new_inicio_generos"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/medium_item_menu"
        app:drawableStartCompat="@drawable/icon_generos"
        android:drawablePadding="2dp"
        android:paddingEnd="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="20dp"
        android:paddingStart="10dp"
        android:textAllCaps="false"
        android:textColor="@drawable/text_item_menu"
        android:textSize="12sp" />

API 24确定 enter image description here

API 21无法呈现 enter image description here

0 个答案:

没有答案