Android 实现自定义圆形进度条的三种常用方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android 实现自定义圆形进度条的三种常用方法

jacklicto   2023-03-23 我要评论

Android 自定义 进度条,一般有三种方式,最早一般使用UI给的图片使用帧动画,完成,后面两种,一种是使用自定义颜色,另外一种是使用带相近色的图片加动画完成。

下面具体 说一下三种方式,推荐使用第二种方式,如果这种达不到效果,或者比较高也可使用第一种方式;

一、通过帧动画实现

1.首先在res 下新建设 anim 文件夹,在里面新建XML 文件;

定义res/anim/loading.xml如下:

例:loading.xml

<?xml version="1.0" encoding="UTF-8"?>
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:duration="150" android:drawable="@drawable/pic1" />

<item android:duration="150" android:drawable="@drawable/pic2" />
<item android:duration="150" android:drawable="@drawable/pic3" />
<item android:duration="150" android:drawable="@drawable/pic4" />
<item android:duration="150" android:drawable="@drawable/pic5" />
<item android:duration="150" android:drawable="@drawable/pic6" />
<item android:duration="150" android:drawable="@drawable/pic7"

<item android:duration="150" android:drawable="@drawable/pic8"/>

</animation-list>

二、通过自定义颜色实现

定义res/drawable/progress_color.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<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>

第三种 使用有进度的UI图片:

定义res/drawable/progress_pic.xml如下:

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

在Activity 布局中直接使用就可以:

<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.demo.MainActivity" >

<ProgressBar
android:id="@+id/loading_process_dialog_progressBar"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:indeterminate="false"
android:indeterminateDrawable="@anim/loading" />

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="60dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/progress_small" />

<ProgressBar
android:id="@+id/loading_process_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="60dp"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/progress_pic" />

</LinearLayout>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们