Android中ProgressBar的三个效果

 

 

 

先上一张界面图,如图,我要实现的效果是点击开始,进度条开始加载。点击暂停,进度条停止加载。点击继续,进度条继续加载

 

xml布局文件:

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<ProgressBar
android:layout_gravity="center_horizontal"
android:id="@+id/progress"
android:layout_width="300px"
android:layout_height="wrap_content"
android:max="1000"
style="?android:progressBarStyleHorizontal"
android:layout_marginTop="50px"

/>


<Button
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"

android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续"
android:layout_gravity="center_horizontal"
/>

</LinearLayout>




Activity文件添加事件
package com.contentprovide.liuliu.progreessbar_demo;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Message;

public class MainActivity extends AppCompatActivity {

ProgressBar progress;
int value=0;
int scondvalue=0;
Handler handle = new Handler();
Button star,stop,resume;

线程,把进度条的变化效果放在里面
Runnable run = new Runnable() {
@Override
public void run() {
value+=5;
scondvalue += 10;
progress.setProgress(value);
progress.setProgress(value);
handle.postDelayed(run,200);
}
};




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = (ProgressBar) findViewById(R.id.progress);
star = (Button) findViewById(R.id.star);
stop = (Button) findViewById(R.id.stop);
resume = (Button) findViewById(R.id.resume);

        开始事件
star.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handle.postDelayed(run,200);//把Runable对象放进handle中,每隔200毫秒执行一次,注意:在Runable中也要放这句话

}
});


stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handle.removeCallbacks(run);//移除线程
}
});

resume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handle.postDelayed(run,200);
}
});


}

}





posted @ 2017-12-04 16:38  西红柿里没有番茄  阅读(522)  评论(0编辑  收藏  举报