自定义进度可绘制不适用于 Android Lollipop(API 21)设备

Custom progress drawable not working on Android Lollipop (API 21) devices

我有一个进度可绘制对象无法在设备 运行ning Android Lollipop 上正常工作。

M上的截图

棒棒糖上的屏幕截图

circle_percentage_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="oval">
      <solid android:color="@color/colorTranslucentBlack"/>
    </shape>
  </item>
  <item android:id="@android:id/progress">
    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>
  </item>
  <item android:id="@android:id/secondaryProgress">
    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>
  </item>

</layer-list>

此可绘制对象用作 ProgressView 的背景,如下所示:

<ProgressBar
      android:id="@+id/circle_progress"
      style="?android:attr/progressBarStyleHorizontal"
      android:layout_width="70dp"
      android:layout_height="70dp"
      android:gravity="center"
      android:progress="65"
      android:indeterminate="false"
      android:progressDrawable="@drawable/circle_percentage_drawable"
      />

绘制为 65% 的圆圈呈现在设备上 运行ning Android M、KitKat、Jellybean。但是,如果 运行 在 Android Lollipop (API 21) 上的相同代码,则圆圈显示为 100%。

此处提供完整的源代码:https://github.com/slashrootv200/CircleProgressPercentage

在你的循环进度条中添加android:useLevel=true"xml

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

    <shape android:shape="oval"
           android:useLevel="true">
      <solid android:color="@color/colorTranslucentBlack"/>
    </shape>

  </item>

  <item android:id="@android:id/progress">

    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0"
          android:useLevel="true">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>

  </item>

  <item android:id="@android:id/secondaryProgress">

    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
      <shape
          android:innerRadiusRatio="2.5"
          android:shape="ring"
          android:thicknessRatio="25.0"
          android:useLevel="true">
        <gradient
            android:centerColor="@android:color/holo_red_dark"
            android:endColor="@android:color/holo_red_dark"
            android:startColor="@android:color/holo_red_dark"
            android:type="sweep"/>
      </shape>
    </rotate>

  </item>

</layer-list>