美文网首页android 技术知识
Android 中实现进度随进度条一起移动

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

作者: 追梦小乐 | 来源:发表于2020-01-07 09:40 被阅读0次
    • 获取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 中实现进度随进度条一起移动

        本文链接:https://www.haomeiwen.com/subject/qgfractx.html