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系统的规定

更多相关文章

  1. 跑马灯结合TextSwitcher实现系统公告栏
  2. Android 通过按钮弹出系统菜单(通过Button显示菜单)转
  3. 打开android 系统联系人
  4. android的系统属性设置和应用
  5. Android Build系统常见变量说明
  6. Android检测系统中是否存在某进程
  7. android中系统日期时间的获取
  8. 系统关闭GPRS数据流量和 GPS的方法
  9. Android(4.4)音频系统之mediaserver服务启动

随机推荐

  1. python中四种获取文件后缀名的方法
  2. Docker镜像管理快速入门
  3. 如何基于Apache Pulsar和Spark进行批流一
  4. Python机器学习常用库,你用过哪几个?
  5. python之模拟io模式
  6. 源码编译搭建LAMP架构
  7. 深度学习实践:从零开始做电影评论文本情感
  8. 「课代表」帮你总结了全网最全的Redis知
  9. 详解TensorFlow™ GPU 安装
  10. 金融中的机器学习和强化学习