您的位置:首页 > 移动开发 > Android开发

android设计实现窗体启动时,显示一个水平进度条,当进行完成后,隐藏该进度条,并显示一张图片

2017-09-24 18:01 746 查看



public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar pb = (ProgressBar) findViewById(R.id.pb1);
final TextView tv = (TextView) findViewById(R.id.tv);
final ImageView iv = (ImageView) findViewById(R.id.iv);
pb.setMax(100);
pb.setProgress(0);

new Thread() {
public void run() {

// 模拟耗时任务
int index = 0;
while (index++ < 200) {
try {
Thread.sleep((int) (Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
// 修改值
// 做了UI的处理,所以直接可以再子线程中修改UI
pb.setProgress(index);
// 需要使用UI线程
runOnUiThread(new Runnable() {

@Override
public void run() {
tv.setText(pb.getProgress() + "/" + pb.getMax());
}
});
}

runOnUiThread(new Runnable() {
public void run() {
pb.setVisibility(ProgressBar.GONE);//设置ProgressBar隐藏
tv.setVisibility(TextView.INVISIBLE);//设置TextView隐藏
iv.setVisibility(TextView.VISIBLE);//显示图片
}
});
};
}.start();

}

}



xml的设计如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xykj.id05_10_11work7.MainActivity" >

<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/pb1"
android:layout_below="@id/pb1"
android:text="0/100" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"

android:src="@drawable/img01"
android:id="@+id/iv"
android:visibility="invisible" />

</RelativeLayout>



需要注意的是开启线程的时候要符合android系统的规定
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐