带进度条的APP启动页面

今天想给APP做一个带进度条的启动页面,照着别人的例子总出错。也没看懂,反正我也不知道怎么蒙对的。

我是小白,没搞清楚什么不能在主线程中更新UI等等一大堆原理,总之这样写没报错,记录一下:

new Handler(new Handler.Callback()

会自动生成方法。

activity_start.xml:只是在背景上加了一个进度条:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="@drawable/start"
 8     tools:context=".StartActivity">
 9     
10     <ProgressBar
11         android:id="@+id/progressBar"
12         style="@android:style/Widget.ProgressBar.Horizontal"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_marginBottom="50dp"
16         android:layout_marginLeft="@dimen/l_padding"
17         android:layout_marginRight="@dimen/l_padding"
18         android:max="100"
19         android:progress="50"
20         app:layout_constraintEnd_toStartOf="parent"
21         app:layout_constraintBottom_toBottomOf="parent"
22         app:layout_constraintStart_toStartOf="parent" />
23         
24 </androidx.constraintlayout.widget.ConstraintLayout>

StartActivity.java:只需要改背景图片、包名、跳转页

 1 package com.example.career;
 2 
 3 import androidx.annotation.NonNull;
 4 
 5 import android.app.Activity;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.os.Handler;
 9 import android.os.Message;
10 import android.view.View;
11 import android.widget.ProgressBar;
12 import android.widget.Toast;
13 
14 public class StartActivity extends Activity {
15 
16     private ProgressBar progressBar;
17     private int mProgress = 0;
18     private Handler mHandler;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_start);
24 
25         progressBar = findViewById(R.id.progressBar);
26 
27         mHandler = new Handler(new Handler.Callback() {
28 
29             @Override
30             public boolean handleMessage(@NonNull Message message) {
31 
32                 if (message.what == 0x111) {
33 
34                     progressBar.setProgress(mProgress);
35 
36                 } else {
37 
38          39                     progressBar.setVisibility(View.GONE);
40                     Intent intent = new Intent(StartActivity.this, MainActivity.class);  //跳转页
41                     startActivity(intent);
42                 }
43 
44                 return false;
45             }
46 
47         });
48 
49         //启动线程来执行任务
50 
51         new Thread(new Runnable() {
52             @Override
53             public void run() {
54                 while (true) {
55                     mProgress = doWork();
56                     Message m = new Message();
57                     if (mProgress < 100) {
58                         m.what = 0x111;
59                         mHandler.sendMessage(m);
60                     } else {
61                         m.what = 0x110;
62                         mHandler.sendMessage(m);
63                         break;
64                     }
65                 }
66             }
67 
68             private int doWork() {
69 
70                 mProgress += Math.random() * 10;
71 
72                 try {
73                     Thread.sleep(200);
74                 } catch (InterruptedException e) {
75                     e.printStackTrace();
76                 }
77                 return mProgress;
78 
79             }
80 
81         }).start();
82 
83     }
84 
85 }

 

posted @ 2020-09-07 16:19  风肖肖(肖遥)  阅读(317)  评论(0编辑  收藏  举报