本文将从几个方面来讲解进度条的设置。
1.条形进度条 :不同图片的方式来实现
<ol start="1" class="dp-xml" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 26px; margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; color: black;"> </span></li></ol>
第一步:在xml中
<ProgressBar android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:id="@+id/bar"
style="@android:style/Widget.ProgressBar.Horizontal"/>
第二步:在activity中
final ProgressBar bar = (ProgressBar) findViewById(R.id.bar);
// 创建一个复杂更新进度的Handler
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x111) {
bar.setProgress(progressStatus);
}
}
};
// 启动线程来执行任务
new Thread() {
public void run() {
while (progressStatus < 100) {
// 获取耗时的完成百分比
progressStatus = doWork();
Message m = new Message();
m.what = 0x111;
// 发送消息到Handler
handler.sendMessage(m);
}
}
}.start();
}
//模拟一个耗时的操作
private int doWork() {
data[hasData++] = (int) (Math.random() * 100);
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
return hasData;
}
2 圆形进度条
这里讲自定义的圆形进度条,通过给进度条图片设置动画来实现
当然大家也可以选用更加漂亮的图片。
<ProgressBar
android:id="@+id/blogContentPro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/progressbar_large"
android:visibility="visible" />
progressbar_large.xml
?xml version="1.0" encoding="utf-8"?>
<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/base_loading_large_icon"
android:pivotX="50%"
android:pivotY="50%"
/>
3 自定义的 带文字的diloge类的进度条
第一步:创建自定义LoadingDialog 加载对应的布局文件
<pre style="background-color: rgb(255, 255, 255); font-family: 宋体; font-size: 9pt;"><pre name="code" class="java"> public static Dialog creatDialog(final Context context,final String text){
final Dialog dialog = new Dialog(context,R.style.new_circle_progress);
dialog.setContentView(R.layout.layout_progressbar);
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
TextView textView = (TextView)dialog.findViewById(R.id.progress_msg);
textView.setText(text);
return dialog;
}
第二步 :创建布局文件layout_progressbar。xmL
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@mipmap/loding_bg"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/ic_circle_progress"
/>
<TextView
android:id="@+id/progress_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="8dp"
tools:text="@string/progress_text"
android:textSize="@dimen/title_size"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
第三步:设置Style
<pre name="code" class="java"> <style name="new_circle_progress" parent="@android:style/Theme.Dialog">
<item name="android:indeterminateDrawable">@mipmap/commit_progress</item>
<item name="android:windowNoTitle">true</item><!-- 设置无标题 -->
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item><!--半透明-->
<item name="android:windowBackground">@color/transparent</item><!--背景透明-->
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowContentOverlay">@null</item>
</style>
第四步;设置动画 ic_circle_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate android:drawable="@mipmap/commit_progress" android:fromDegrees="0.0" android:toDegrees="360.0" android:pivotX="50.0%" android:pivotY="50.0%" />
</item>
</layer-list>