两种比较巧妙的方法,使用xml实现,只需要少量的代码。
第一种效果类似github的loading效果,周围圆环在转动,中间的logo不变化。
首先准备两张资源图片,一张为外围的圆环,一张用来中间显示的,
类似这样的:
loading_outer.png
loading_inner.png
ProgressBar的使用代码:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/progress_1"
android:indeterminateDuration="1000" />
progress_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360"
android:interpolator="@android:interpolator/linear">
<bitmap
android:antialias="true"
android:filter="true"
android:src="@drawable/loading_outer" />
</rotate>
</item>
<item android:drawable="@drawable/loading_inner" />
</layer-list>
over,就是这么简单!
第二种效果是一个进度条,可以是使用java代码设置进度:
ProgressBar的使用代码:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name"
android:textSize="14sp"
android:textColor="@color/black"
android:gravity="center_horizontal" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:progress="100"
android:progressDrawable="@drawable/circle_progress_background" />
<ProgressBar
android:id="@+id/circle_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:max="100"
android:rotation="-90"
android:indeterminate="false"
android:progressDrawable="@drawable/circle_progress_foreground" />
</FrameLayout>
circle_progress_background.xml代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="24dp"
android:shape="ring"
android:thickness="2dp">
<solid android:color="@color/colorAccent" />
</shape>
circle_progress_foreground.xml代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="24dp"
android:shape="ring"
android:thickness="2dp">
<solid android:color="@color/colorPrimary" />
</shape>
设置progress时,设置的是progressDrawable为circle_progress_foreground的ProgressBar的progress。
实现原理很简单,使用两个ProgressBar,前一个显示默认的圆环,后一个显示设置进度后的显示部分。