Android 中实现进度随进度条一起移动

  • 获取ProgressBar的宽度
        progressBar = (CcbProgressBar) view.findViewById(R.id.progressBar);
        tvProgress = (TextView) view.findViewById(R.id.tv_dlg_progress);

        // 得到progressBar控件的宽度
        ViewTreeObserver vto = progressBar.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                progressBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                width = progressBar.getWidth();

                MbsLogManager.logD("VersionForceUpdateDialogWrapper -> width: " + width);
            }
        });

  • 通过计算进度条的进度,把下方的字体向右移动
        if (null == progressBar) {
            return this;
        }
        MbsLogManager.logD("VersionForceUpdateDialogWrapper -> max: " + max);
        MbsLogManager.logD("VersionForceUpdateDialogWrapper -> progress: " + progress);
        progressBar.setMax((int) max);
        progressBar.setProgress((int) progress);

        if (null == tvProgress) {
            return this;
        }
        int downloadProgress = (int)(((double)progress/(double) max)*100);
        tvProgress.setText(downloadProgress+"%");

        //每一段要移动的距离
        scrollDistance = (float) ( width / 100);

        // 得到字体的宽度
        tvWidth = tvProgress.getWidth();
        currentPosition = (int)(scrollDistance * downloadProgress);
        MbsLogManager.logD("VersionForceUpdateDialogWrapper -> currentPosition: " + currentPosition);
        //做一个平移动画的效果
        if (tvWidth + currentPosition <= width - tvProgress.getPaddingRight()) {
            tvProgress.setTranslationX(currentPosition);
        }

你可能感兴趣的:(Android 中实现进度随进度条一起移动)