android - 如何在特定时间内填充进度条?

标签 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>

启动画面运行 3 秒,然后转到下一个 Activity ,该 Activity 由这段代码控制:

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();
    }

}

正如我所说,您可以看到启动画面运行了 3 秒(3000 毫秒)。您可能还会注意到初始屏幕有一个进度条。

我的问题是:

如何在启动画面运行的那 3 秒内填充/加载进度不良?

我想在我的应用程序首次启动时通过填充进度条来赋予它那种加载效果。

这有可能吗?

最佳答案

我会摆脱你的计时器线程,而是使用 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();

关于android - 如何在特定时间内填充进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138363/

相关文章:

android - manifest.xml 中的 uses-feature required=false 未在 android 1.6 上编译

android - 使用导航图范围 : NavController is not available before onCreate() 注入(inject) View 模型

java - Bad Parcelable 异常

android - Android 上的 HttpClient 和自定义 TrustManager

android - 用新时间android重新启动倒数计时器

java - Android线程问题(线程在后台运行)

java - 无型号类别的 retrofit 2

android - 如何更新带有隐藏图标的Android应用程序

Android 使用自定义主题修改样式属性

Android sqlite begintransaction 执行时间过长