【问题标题】:An Android ProgressBar with a variable increment speed dependant on progress一个 Android ProgressBar,其增量速度取决于进度
【发布时间】:2016-03-02 04:38:17
【问题描述】:

我有一个简单的进度条,它的最大值设置为 100。

我需要让它以 2 种不同的速度增加。

最初最快到 60,然后一旦达到 60,它会一直缓慢到 100,使用低增量速度。

我使用一个处理程序和一个保存该值的接口来做到这一点,但它在 UI 上跳帧很多。

有人可以展示它是如何使用避免跳过警告的单独线程实现的吗?

非常感谢。

【问题讨论】:

  • 请发布您的代码

标签: android multithreading handler runnable android-progressbar


【解决方案1】:

试试这个代码

public class MainActivity extends AppCompatActivity {

    private final static int FAST_SPEED = 2;
    private final static int SLOW_SPEED = 1;
    private final static int UPDATE_TIME = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressBar = (ProgressBar) findViewById(R.id.progress);
        postUpdate();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mUpdateHandler.removeCallbacks(mUpdateRunnable);
    }

    private void postUpdate() {
        mUpdateHandler.postDelayed(mUpdateRunnable, UPDATE_TIME);
    }

    private final Handler mUpdateHandler = new Handler();
    private final Runnable mUpdateRunnable = new Runnable() {
        @Override
        public void run() {
            if (mProgress > 100) {
                return;
            } else if (mProgress <= 60) {
                mProgress += FAST_SPEED;
            } else if (mProgress > 60) {
                mProgress += SLOW_SPEED;
            }
            mProgressBar.setProgress(mProgress);
            postUpdate();
       }
    };

    private ProgressBar mProgressBar;
    private int mProgress;
}

您可以更改 FAST_SPEED、SLOW_SPEED 和 UPDATE_TIME 值来自定义进度条填充速度

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多