相信大家在開發(fā)中或多或少會用到一些進(jìn)度條,但很多時候我們都會自定義一些進(jìn)度條,或者使用第三方框架,比如SmartRefreshLayout等,但Android原生的進(jìn)度條的功能也很強(qiáng)大,Android進(jìn)度條有4種風(fēng)格可以使用。
Android進(jìn)度條有4種風(fēng)格可以使用。
- 默認(rèn)值是progressBarStyle。
- 設(shè)置成progressBarStyleSmall后,圖標(biāo)變小。
- 設(shè)置成progressBarStyleLarge后,圖標(biāo)變大
- 設(shè)置成progressBarStyleHorizontal后,變成橫向長方形。
自定義圓形進(jìn)度條ProgressBar的一般有三種方式:
一、通過動畫實(shí)現(xiàn)
定義res/anim/loading_anim.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/loading_blue_16_f0"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f1"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f2"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f3"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f4"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f5"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f6"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f7"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f8"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f9"/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f10"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f11"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f12"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f13"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f14" />
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f15"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f16"
/>
<item android:duration="150" android:drawable="@drawable/loading_blue_16_f17"
/>
</animation-list>
在layout文件中引用如下:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:indeterminate="false"
android:indeterminateDrawable="@anim/loading_anim" />
二、通過自定義顏色實(shí)現(xiàn)
定義res/drawable/loading_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>
在layout文件中引用如下:
<ProgressBar
android:id="@+id/progressBar2"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/loading_color"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
三、使用一張圖片進(jìn)行自定義
定義res/drawable/loading_img.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="@drawable/exchange_loading"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="360.0"/>
</item>
</layer-list>
在layout文件中引用如下:
<ProgressBar
android:id="@+id/progressBar3"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/lodaing_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
另外,在平時我經(jīng)常用的一個第三方框架就是SmartRefreshLayout
SmartRefreshLayout是一個“聰明”或者說“智能”的下拉刷新布局,由于它的“智能”,它不只是如其它的刷新布局所說的支持所有的View,還支持多層嵌套的視圖結(jié)構(gòu)。
除了“聰明”之外,SmartRefreshLayout還具備了很多的特點(diǎn)。SmartRefreshLayout 沒有使用到:序列化、反序列化、JNI、反射,所以并不需要添加混淆過濾代碼
它繼承自ViewGroup 而不是其它的FrameLayout或者LinearLayout,提高了性能。
github網(wǎng)址: https://github.com/scwang90/SmartRefreshLayout
使用姿勢
1.在 buld.gradle 中添加依賴
compile 'com.android.support:appcompat-v7:25.3.1'//版本隨意
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3-alpha-6'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3-alpha-6'//沒有使用特殊Header,可以不加這行
2.在XML布局文件中添加 SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:background="#fff" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
3.在 Activity 或者 Fragment 中添加代碼
RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000);
}
});
refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadmore(2000);
}
});
使用指定的 Header 和 Footer
1.方法一 全局設(shè)置
public class App extends Application {
//static 代碼段可以防止內(nèi)存泄露
static {
//設(shè)置全局的Header構(gòu)建器
SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局設(shè)置主題顏色
return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定為經(jīng)典Header,默認(rèn)是 貝塞爾雷達(dá)Header
}
});
//設(shè)置全局的Footer構(gòu)建器
SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定為經(jīng)典Footer,默認(rèn)是 BallPulseFooter
return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
}
});
}
}
(注意:方法一 設(shè)置的Header和Footer的優(yōu)先級是最低的,如果同時還使用了方法二、三,將會被其它方法取代)
2.方法二 XML布局文件指定
<com.scwang.smartrefresh.layout.SmartRefreshLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444"
app:srlPrimaryColor="#444444"
app:srlAccentColor="@android:color/white"
app:srlEnablePreviewInEditMode="true">
<!--srlAccentColor srlPrimaryColor 將會改變 Header 和 Footer 的主題顏色-->
<!--srlEnablePreviewInEditMode 可以開啟和關(guān)閉預(yù)覽功能-->
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/padding_common"
android:background="@android:color/white"
android:text="@string/description_define_in_xml"/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
(注意:方法二 XML設(shè)置的Header和Footer的優(yōu)先級是中等的,會被方法三覆蓋。)
3.方法三 Java代碼設(shè)置
final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
//設(shè)置 Header 為 Material風(fēng)格
refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
//設(shè)置 Footer 為 球脈沖
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
在無意中使用這個框架,感覺很爽,很智能,使用上基本上就是傻瓜式的搬運(yùn),不過想自己運(yùn)用的更加豐富就需要對框架的源碼進(jìn)行解析