我有一个持续5秒钟的闪屏,我想用ProgressBar
代表进度。
public class SplashActivity extends Activity {
private static final long SPLASHTIME = 5000;
private ProgressBar progressBar;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
SplashHandler handlerSplash = new SplashHandler();
progressBar = (ProgressBar) findViewById(R.id.progression);
progressBar.setMax((int) ((SPLASHTIME) / 1000));
progressBar.setProgress(0);
Message msg = new Message();
msg.what = 0;
handlerSplash.sendMessageDelayed(msg, SPLASHTIME);
ThreadProgressBar threadProgress = new ThreadProgressBar();
threadProgress.start();
}
private class ThreadProgressBar extends Thread {
ProgressBarHandler handlerProgress = new ProgressBarHandler();
public void run() {
try {
while (progressBar.getProgress() <= progressBar.getMax()) {
Thread.sleep(1000);
handlerProgress.sendMessage(handlerProgress.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
}
}
}
private class ProgressBarHandler extends Handler {
public void handleMessage(Message msg) {
progressBar
.incrementProgressBy((int) (SPLASHTIME / SPLASHTIME));
}
}
private class SplashHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
default:
case 0:
super.handleMessage(msg);
// new ProgressBarIncrease().execute();
Intent intent = new Intent();
intent.setClass(SplashActivity.this, RdvTab.class);
startActivity(intent);
}
}
}
答案 0 :(得分:1)
我想因为SplashActivity
本身就是一个活动,所以可以阻止用户界面吗? AsyncTask
有时不会逐渐运行,如果您希望更新能够顺利显示,那么如何更改为工作人员Thread
和Handlder
。
Handler
inside SplashActivity
sendEmptyMessage()
到Handler
。 5秒后,向Handler
发送消息以解除对话框并启动,然后工作人员Thread
结束。更新:抱歉反馈缓慢。这个怎么样?我还没有测试它,但它不是最好的实现,你可以使用这个逻辑
public class SplashActivity extends Activity {
private static final int SPLASHTIME = 5000;
// u can change the value here for smoother effect
private static final long UPDATEINTERVAL = 1000;
private ProgressBar progressBar;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
progressBar = (ProgressBar) findViewById(R.id.progression);
new Thread(new Runnable(){
@Override
public void run(){
int progress = 0;
while(true){
// send current progress to handler
mHandler.sendEmptyMessage(progress);
// break from the loop after SPLASHSCREEN millis
if(progress > SPLASHSCREEN)
break;
// increase the progress
progress = (int) (progress + UPDATEINTERVAL);
// sleep the worker thread
Thread.sleep(UPDATEINTERVAL);
}
}
}).start();
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message message){
// get the current progress
int progress = message.what;
if(progress <= SPLASHTIME){
progressBar.setProgress(progress);
}
else{
// finish splashActivity here & do what u want do to after splashscreen, for example
finish();
Intent intent = new Intent(SpashActivity.this, MenuActivity.class);
startActivity(intent);
}
}
}
}
答案 1 :(得分:0)
您应该将启动画面时间分成相等的数量,并按这些间隔发送进度更新。
例如:假设总时间分为50个相等的间隔,如下所示
protected void onPreExecute() {
progressbarStep = progressbarWidth/50;
timeStep = 5000/50;
progress = 0;
}
@Override
protected Void doInBackground(Void... params) {
try {
while(progress < progressbarWidth){
progress += progressbarStep;
publishProgress(progress);
SystemClock.sleep(timeStep);
}
return null;
} catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
}