进度条小应用

来源:互联网 发布:windows上类似imovie 编辑:程序博客网 时间:2024/06/05 05:16
进度条小应用
    今天做了一个进度条小应用,给大家分享一下。下面是我做的界面。

首先编写xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>


<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:background="#ffcfef"
android:text="手机杀毒" />




<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/ic_scanner_malware">


<ImageView
android:id="@+id/iv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/act_scanning_03" />
</FrameLayout>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_margin="10dp">
<TextView
android:id="@+id/tv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="text" />
<ProgressBar
android:id="@+id/pb_main"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:progressDrawable="@drawable/my_progress"/>
</LinearLayout>


</LinearLayout>
</LinearLayout>

编写的时候遇到的一些问题:

    1 之前的时候用的是eclipse,把所有的东西都清空后直接能把线性布局给拉过来,但是android studio 不行,得自己写,同时编写第一个的时候不会有提示。但是第二个之后就会有提示。

    2 这里的进度条用的不是系统给的,所以在drawable文件中写一个Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@drawable/security_progress_bg">

</item>
<item
android:id="@android:id/progress"
android:drawable="@drawable/security_progress">

</item>

</layer-list>
Mainactivity的编写主要分两部分:
1 左边图形的旋转:即用的是旋转动画,详解请看上一篇博客
private void showAnimation() {
RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setInterpolator(new LinearInterpolator());
iv_main.startAnimation(rotateAnimation);

}

右边进度条:这时需使用异步任务来做:

1 主线程 显示提示视图  onPreExecute()

2 分线程 做长时间的工作(扫描应用,进度条前进)doInBackground(Integer... voids)  onProgressUpdate(Double... values)

3 主视图更新界面  onPostExecute(String aVoid)

具体程序见下:

private void showScan() {
final int appCount = 60;
//异步任务
new AsyncTask<Integer,Double,String>(){
//1 主线程 显示提示视图
@Override
protected void onPreExecute() {
tv_main.setText("正在扫描中。。。");

pb_main.setMax(60);
}
//2 分线程 做长时间的工作(扫描应用)

@Override
protected String doInBackground(Integer... voids) {



for(int i = 0;i<appCount;i++){
SystemClock.sleep(40);
publishProgress(i + 0.0);
}
return "成功";
}
//主线程 更新界面

@Override
protected void onPostExecute(String aVoid) {
pb_main.setVisibility(View.GONE);
tv_main.setText("扫描完成,没有病毒");
iv_main.clearAnimation();

Toast.makeText(MainActivity.this,aVoid,Toast.LENGTH_SHORT).show();
}

@Override
protected void onProgressUpdate(Double... values) {
// pb_main.incrementProgressBy(1);
pb_main.setProgress(values[0].intValue());
}
// Pararms[] params
}.execute();
}
以上。