【问题标题】:ring shapes for L preview not workingL 预览的环形不工作
【发布时间】:2014-07-06 06:19:34
【问题描述】:

刚刚测试了新的开发者预览版,发现我现有的环形可绘制对象无法正确渲染。它不是一个环,而是一个完整的圆圈。它是一个错误还是发生了某些变化,或者我一开始就做错了?任何可能的解决方案?

代码如下:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0">
            <solid android:color="#ff5698fb"/>
        </shape>
    </item>
</layer-list>

谢谢!

更新

我已将形状更新如下:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0">
            <solid android:color="#ff5698fb"/>
        </shape>
    </item>
    <item >
        <shape
            android:innerRadiusRatio="4"
            android:shape="ring"
            android:thicknessRatio="5.5">
            <solid android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

通过重叠两个圆圈产生一个环。但是我使用它的进度条不起作用。 这是进度条的代码:

<ProgressBar
                android:id="@+id/progressCircle"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:indeterminate="false"
                android:max="100"
                android:progress="65"
                android:rotation="270"
                android:progressDrawable="@drawable/progress_circle"
                android:visibility="visible"/>

【问题讨论】:

  • 这个你搞定了吗?我的代码和你的几乎一样——基本上是一个自定义的圆形进度条。现在当我调用 updateProgress() 时,它似乎没有任何效果,并且进度保持在 100%。它在以前的 Android 版本中运行良好,但 Android L 预览版不喜欢它。

标签: android android-5.0-lollipop


【解决方案1】:

您需要在进度项中添加android:useLevel="true"。这在以前的版本中似乎没有必要,但 L 想要它。

<item android:id="@android:id/progress">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="7.0"
        android:useLevel="true" >
        <solid android:color="#ff5698fb"/>
    </shape>
</item>

【讨论】:

  • 谢谢!我已经有几个星期没有机会参与我的项目了,但我很感激!
  • 奇怪...但现在我的背景不可见?!编辑:必须将 useLevel="false" 作为背景
  • 你能告诉我“uselevel”属性是什么意思吗?从android doc看没有意义
  • 说实话,我也没有完全理解。查看Drawable docs 级别“允许可绘制对象根据连续控制器改变其图像,例如显示进度或音量级别。”因此,级别可能会根据您拥有的可绘制对象的类型做不同的事情。在我们的例子中,它只是设置要绘制的特定数量的环。这确实引出了一个问题,即为什么这不是默认行为以及为什么我们需要明确设置它!
【解决方案2】:

TL;DR:为自定义进度条中使用的所有环形显式设置 useLevel 的值。


卡诺的答案是解决您问题的方法。我正在对其进行补充,以在自定义进度条中添加有关环形形状的一般信息。

似乎useLevel 的默认值在 Android 版本中发生了变化。这是一篇与之相关的调查。

以下是进度条的工作实现,使用一个环作为进度指示器,另一个环作为进度背景:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Progress background -->
    <item android:id="@android:id/background">
        <shape
            android:shape="ring"
            android:useLevel="false">
            <solid
                android:color="#dae1e6"/>
        </shape>
    </item>

    <!-- Progress -->
    <item android:id="@android:id/progress">
        <!-- Rotation of the progress to start at the top-->
        <rotate
            android:fromDegrees="-90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="-90">
            <shape
                android:shape="ring"
                android:useLevel="true">
                <solid
                    android:color="#61bcf9"/>
            </shape>
        </rotate>
    </item>

</layer-list>

ProgressBar 中将其用作progressDrawable

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:progressDrawable="@drawable/custom_horizontal"
    android:progress="50"
    android:max="100"
    android:id="@+id/progressBar"
    android:layout_gravity="center"/>

useLevel=true 设置为进度环形,useLevel=false 设置为背景环形,可为所有 Android 版本提供正确的所需行为:

从背景形状中删除useLevel

5.0 前(左)| 5.0(右)

从进度形状中删除 useLevel

5.0 前(左)| 5.0(右)


所以,据我了解,useLevel 用于表示形状是否受进度值的影响。如果不是 (false),它将被完全绘制;如果是 (true),它将被绘制为进度值。

总结起来,好像是:

  • useLevelAndroid 中默认为true,如果useLevel 未设置为环形,则会导致背景环形跟随进度值,从而使其隐藏在进度指示器后面响。
  • useLevelAndroid >= 5.0 中默认为 false,如果未将 useLevel 设置为环形,则会导致进度指示器环形完全绘制。

因此,由于这种不一致,最好在取决于进度的形状中将useLevel 的值显式设置为true,而在不依赖于进度的形状中,最好将false 设置为。

这种行为是基于我的观察,我没有找到任何来源证实它。 The official Android docs不是很清楚...

【讨论】:

  • 最后。我认为在 Android 5.0 中没有办法做到这一点。感谢您的指导!
  • 你能告诉我“uselevel”属性是什么意思吗?从android doc看没有意义
【解决方案3】:

只是想在框架源代码中添加一些引用,以确认 useLevel 在 Android API 版本 = 21 (>= 5.0) 中默认为 false,如 Christian García 之前声明。

在 android-20 中,文件 GradientDrawable.java,第 820-835 行:

if (shapeType == RING) {
    ....
    st.mUseLevelForShape = a.getBoolean(
                    com.android.internal.R.styleable.GradientDrawable_useLevel, true);
}

在 android-21 中,文件 GradientDrawable.java,第 1028-1047 行:

if (state.mShape == RING) {
    ....
    state.mUseLevelForShape = a.getBoolean(
                    R.styleable.GradientDrawable_useLevel, state.mUseLevelForShape);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多