在xml布局中声明ProgressBar:
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="@dimen/x16"
android:layout_marginLeft="@dimen/x30"
android:layout_marginRight="@dimen/x30"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_below="@id/progress_tv"
android:layout_marginTop="@dimen/x12"
android:layout_marginBottom="@dimen/x50"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/property_star_progress"
/>
property_star_progress.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="@dimen/x21" />
<solid android:color="#eaeaea"/>
</shape>
</item>
<item android:id="@android:id/progress">
<!--不能使用clip,clip会将progress的右边裁成直角,必须使用scale-->
<!--<clip>
<shape>
<corners
android:radius="@dimen/x21"/>
<gradient
android:angle="0"
android:startColor="#5a5df6"
android:endColor="#3f6ef6"/>
</shape>
</clip>-->
<scale
android:scaleWidth="100%"
android:drawable="@drawable/property_star_progress_scale"/>
<!--android:scaleWidth="50%"-->
<!--android:drawable="@drawable/property_star_progress_scale" bg_wuyezhixing-->
</item>
</layer-list>
scale标签是定义一个scaleDrawable图形对象,scaleWidth是设置引用的图片资源显示时是否要缩放(百分比),100%不缩放。详情见:ScaleDrawable
其中,property_star_progress_scale.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="@dimen/x21"/>
<gradient
android:angle="0"
android:startColor="#5a5df6"
android:endColor="#3f6ef6"/>
</shape>
效果: