android 技术知识

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

2020-01-07  本文已影响0人  追梦小乐
        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);
        }

上一篇下一篇

猜你喜欢

热点阅读