Android 自定义ProgressBar样式

来源:互联网 发布:原子笔推荐 知乎 编辑:程序博客网 时间:2024/06/05 09:04

首先,在activity_main中定义个一个ProgressBar 控件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.blue.studyui.MainActivity">       <!--indeterminateDrawable  的样式xml为自己定义的xml-->    <ProgressBar        android:id="@+id/progress_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:indeterminate="false"        android:indeterminateDrawable="@drawable/dialog_style_xml_color"        android:layout_marginTop="148dp"        android:layout_alignParentTop="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" /></RelativeLayout>

上面的dialog_style_xml_color.xml 定义了一个圆形的的进度条 代码如下

<?xml version="1.0" encoding="utf-8"?><!--形状ring(环形--><!--innerRadiusRation 浮点型,以环的宽度比率来表示内环的半径--><!--thicknessRation 以环的宽度比率来表示环的厚度--><!--useLevel boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.设置为true无渐变。false有渐变色--><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="360">    <shape        android:innerRadiusRatio="3"        android:shape="ring"        android:thicknessRatio="8"        android:useLevel="false">        <gradient            android:centerColor="#FFDC35"            android:centerY="0.50"            android:endColor="#CE0000"            android:startColor="#FFFFFF"            android:type="sweep"            android:useLevel="false" />    </shape></rotate>

这样就完成成功了,启动后就会看到 一个圆形 三种颜色的进度条在旋转了

运行结果

如果需要把一张图片放在进度条上面自己旋转可以这样定义
定义一个custom_progress_draw 的xml文件

<?xml version="1.0" encoding="utf-8"?><animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/load"    android:pivotX="50%"    android:pivotY="50%" />

然后我们只需要把ProgressBar的android:indeterminateDrawable 换成 custom_progress_draw 这个布局就可以了

如图

2 0
原创粉丝点击