如何在特定时间内填写进度条?

Ama*_*mer 4 android

嘿家伙我的应用程序有以下启动画面,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src = "@drawable/timer_ic"
        android:layout_centerInParent="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Timer"
        android:textColor="#FFFFFF"
        android:id="@+id/textView"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="51dp" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="250sp"
        android:layout_height="5sp"
        android:progress="1"
        android:layout_marginTop="82dp"
        android:max="3"
        android:indeterminate="false"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:progressDrawable="@drawable/custom_progress_bar"
        android:background="#808080"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

启动屏幕运行3秒钟,然后进入下一个活动,该活动由以下代码控制:

package com.awesomeapps.misael.timer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ProgressBar;


public class SplashScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(3000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openMainActivity= new Intent("com.awesomeapps.timer.MAINACTIVITY");
                    startActivity(openMainActivity);
                }
            }
        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}
Run Code Online (Sandbox Code Playgroud)

就像我说的那样,你可以看到飞溅的screeb运行3秒(3000毫秒).您可能还注意到启动画面有一个进度条.

我的问题是:

我如何在我的SPLASHSCREEN正在运行的3秒内填写/加载进度条?

我想通过填充进度条为我的应用程序首次启动时提供那种加载效果.

是否有可能做到这一点?

希望你们能帮助我!在此先感谢您的帮助!:)

Hel*_*boy 9

我会摆脱你的计时器线程,而是使用ValueAnimator动画进度条,持续时间为3秒

ValueAnimator animator = ValueAnimator.ofInt(0, progressBar.getMax());
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation){
        progressBar.setProgress((Integer)animation.getAnimatedValue());
    }
});
animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        // start your activity here
    }
});
animator.start();
Run Code Online (Sandbox Code Playgroud)


Gab*_*han 0

使用等待 100 毫秒的计时器,而不是等待 3000 毫秒的计时器。然后每次将进度条增加其长度的 1/30,直到达到 100%。然后启动您的主要活动。您希望酒吧移动得越顺畅,计时器就应该越小。