在Android中创建静态进度栏

时间:2016-07-05 14:50:01

标签: android android-progressbar

静态我的意思是,我有一些数据说20,其最大值可以是100.我想将其命名为Progress。然后我需要一个在打开Progress选项卡时调用的活动。加载进度的位置,当达到100的20时停止。

它应该最终看起来像。

Progress.
++++----------------
Progress1.
+++++++++++---------
Progress3.
+++++++-------------

我用Google搜索并获得了完全告诉我的要求的这个gif。 GIF

我尝试过并且可以实现的目标是:

活性

public class test extends Activity {
    private ProgressBar progressBar;
    private int progressStatus = 0, CurrValue=20;
    private TextView textView;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loop);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textView = (TextView) findViewById(R.id.textView1);
        progressBar.setScaleY(3f);
        // Start long running operation in a background thread
        Progress();
    }
    public void Progress(){
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < CurrValue) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                            textView.setText(progressStatus+"/"+progressBar.getMax());
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        //Just to display the progress slowly
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

布局

它有一个TextView和一个ProgressBar

我想在动画中显示多个进度条,就像上面的GIF链接一样。

1 个答案:

答案 0 :(得分:1)

这是一个想法:

  1. 创建ProgressUpdater类。它在构造函数中需要一个ProgressBar,监视进度的变量和最大值(假设0是min或min值,如果它不是0到max)。
  2. 它有一个UpdateProgress()方法,您可以调用它来检查变量并计算进度/更新ProgressBar。
  3. 您的活动包含一系列ProgressUpdater,它们在onCreate中与每个ProgressBar初始化。
  4. Progress()然后遍历ProgressUpdater数组并在每个上调用UpdateProgress()。您需要对Progress()进行一些更改以处理多个值,或者使用一些Listener并让它调用相应ProgressUpdater的UpdateProgress()方法。
  5. 听起来怎么样?