android - 自定义进度条中的圆形渐变

标签 android android-layout progress-bar android-progressbar

如何像屏幕上那样为 ProgressBar 制作圆形渐变?

enter image description here

我现在拥有的:

enter image description here

pb_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="12dip" />

        <stroke
            android:width="1dip"
            android:color="@color/primary_white" />

        <gradient
            android:angle="270"
            android:centerColor="@color/primary_black"
            android:centerY="0.5"
            android:endColor="@color/primary_black"
            android:startColor="@color/primary_black"/>

        <padding
            android:bottom="4dp"
            android:left="4dp"
            android:right="4dp"
            android:top="4dp" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="12dip" />
            <gradient
                android:angle="0"
                android:endColor="@color/primary_teal"
                android:startColor="@color/primary_blue_dark" />             
        </shape>
    </clip>
</item>

<ProgressBar
        android:id="@+id/pb_timer"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:progressDrawable="@drawable/pb_shape" />

我已经尝试将 android:gradientRadius="12dip" 添加到 pb_shape 但没有效果。

最佳答案

似乎我找到了解决方法,感谢您提供正确的搜索方向@krislarson 和 this post

结果代码: pb_shape.xml:

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

    <shape android:shape="rectangle">

        <corners android:radius="12dip" />

        <stroke
            android:width="1dip"
            android:color="@color/primary_white" />

        <gradient
            android:angle="270"
            android:centerColor="@color/primary_black"
            android:centerY="0.5"
            android:endColor="@color/primary_black"
            android:gradientRadius="12dip"
            android:startColor="@color/primary_black" />
    </shape>
</item>

<item android:id="@android:id/progress">
    <scale
        android:drawable="@drawable/pb_custom_progress"
        android:scaleWidth="98%" />
</item>
</layer-list>

pb_custom_progress.xml 神奇之处在于:

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

<!--make it rounded-->
<corners
    android:radius="20dp"
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp" />

<gradient
    android:angle="0"
    android:endColor="@color/primary_teal"
    android:startColor="@color/primary_blue_dark" />

<!--create invisible stroke for padding-->
<stroke
    android:width="6dip"
    android:color="@android:color/transparent"/>

</shape>

结果:

enter image description here

关于android - 自定义进度条中的圆形渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37135595/

相关文章:

android - Android Studio项目已停止构建。检索项目的父项时出错:android:TextAppearance.Material.Widget.Button.Inverse

c# - 使用数组 C# 复制文件的进度条

android - Activity 中出现错误

android - 如何在没有手指抬起的情况下检测滑动事件

Android webview 非常奇怪的行为

java - 在Android中使用addView输出文本结果?

java - 单击按钮更改 TextView LayoutParameters

javascript - 如何创建类似于 Grooveshark 的进度 "bar"?

twitter-bootstrap - Bootstrap - 将进度条值放在右侧

javascript - 如何取消转义从 Android 上的 Javascript 返回的字符串?