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

时间:2016-07-01 06:20:12

标签: 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秒后再转到下一个活动,该活动由这段代码控制:

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

}

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

我的问题是:

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

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

是否可以这样做?

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

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

您可以使用倒数计时器代替Thread或Handler

 private CountDownTimer countDownTimer =
        new CountDownTimer(10000, 100) {
            public void onTick(long millisUntilFinished) {
                progressBar.setProgress(Math.abs((int) millisUntilFinished / 100 - 100));

                }
            }

            public void onFinish() {
               // you can do your action
            }

        };

开始倒计时器

  countDownTimer.start();

答案 2 :(得分:0)

不要让计时器等待3000毫秒,而是要有一个等待100毫秒的计时器。然后每次将进度条增加其长度的1/30,直到达到100%。然后启动您的主要活动。您希望条移动越平滑,您的计时器应该越小。

答案 3 :(得分:0)

试用此代码

您的问题您需要更改启动

我正在回答您尝试自定义进度条或使用进度条

SplashScreenActivity .java

public class  SplashScreenActivity extends Activity {
    private int SPLASH_TIME = 3000;



    @Override
    public void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);



        setContentView(R.layout.splash);




        (new Handler()).postDelayed(new Runnable() {

            public void run() {

        Intent intent = new Intent(SplashScreenActivity.this, YouNewActivity.class);
        startActivity(intent);
        SplashScreenActivity.this.finish();
        overridePendingTransition(R.anim.right_in, R.anim.left_out);

            }
        }, SPLASH_TIME);

    }




    @Override
    public void onBackPressed() {

    }

}

<强> activity_splash.xml:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <com.example.custom.customcircleprogressbar.CricleProgressBarCustom
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></com.example.custom.customcircleprogressbar.CricleProgressBarCustom>

    </RelativeLayout>

</RelativeLayout>
&#13;
&#13;
&#13;

<强> CircleProgressBarCustom.java

public class CricleProgressBarCustom extends View {

//Normal dot radius
private int dotRadius = 10;

//Expanded Dot Radius
private int bounceDotRadius = 13;

//to get identified in which position dot has to expand its radius
private int dotPosition = 1;

//specify how many dots you need in a progressbar
private int dotAmount = 10;

//specify the circle radius
private int circleRadius = 50;


public CricleProgressBarCustom(Context context) {
    super(context);
}

public CricleProgressBarCustom(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CricleProgressBarCustom(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    //Animation called when attaching to the window, i.e to your screen
    startAnimation();
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //take the point to the center of the screen
    canvas.translate(this.getWidth()/2,this.getHeight()/2);

    Paint progressPaint = new Paint();
    progressPaint.setColor(Color.parseColor("#ff014e"));

    //call create dot method
    createDotInCircle(canvas,progressPaint);
}

private void createDotInCircle(Canvas canvas,Paint progressPaint) {
    //angle for each dot angle = (360/number of dots) i.e  (360/10)
    int angle = 36;

    for(int i = 1; i <= dotAmount; i++){

        if(i == dotPosition){
             // angle should be in radians  i.e formula (angle *(Math.PI / 180))
            float x = (float) (circleRadius * (Math.cos((angle * i) * (Math.PI / 180))));
            float y = (float) (circleRadius * (Math.sin((angle * i) * (Math.PI / 180))));

            canvas.drawCircle(x,y, bounceDotRadius, progressPaint);

        }else{
            // angle should be in radians  i.e formula (angle *(Math.PI / 180))
            float x = (float) (circleRadius * (Math.cos((angle * i) * (Math.PI / 180))));
            float y = (float) (circleRadius * (Math.sin((angle * i) * (Math.PI / 180))));

            canvas.drawCircle(x,y, dotRadius, progressPaint);

        }

    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = 0;
    int height = 0;

    //Dynamically setting width and height to progressbar 100 is circle radius, dotRadius * 3 to cover the width and height of Progressbar
     width = 100 + (dotRadius*3);
     height = 100 + (dotRadius*3);

    //MUST CALL THIS
    setMeasuredDimension(width, height);
}

private void startAnimation() {
    BounceAnimation bounceAnimation = new BounceAnimation();
    bounceAnimation.setDuration(150);
    bounceAnimation.setRepeatCount(Animation.INFINITE);
    bounceAnimation.setInterpolator(new LinearInterpolator());
    bounceAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            dotPosition++;
            //when dotPosition == dotAmount , then start again applying animation from 0th positon , i.e  dotPosition = 0;
            if (dotPosition > dotAmount) {
                dotPosition = 1;
            }


        }
    });
    startAnimation(bounceAnimation);
}


private class BounceAnimation extends Animation {
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        //call invalidate to redraw your view againg.
        invalidate();
    }
}
}

答案 4 :(得分:0)

    new Thread(new Runnable() {
        public void run() {
            doWork();
            startApp();
            finish();
        }
    }).start();
}

private void doWork() {
    for (int progress=1; progress<=100; progress+=1) {
        try {
            Thread.sleep(50);  // 5 seconds
            load.setProgress(progress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private void startApp() {
    Intent intent = new Intent(Splash.this, MainActivity.class);
    startActivity(intent);
}