美文网首页
2019-05-14 自己实现的一个动态进度条

2019-05-14 自己实现的一个动态进度条

作者: 菜神一把梭 | 来源:发表于2019-05-14 14:18 被阅读0次
       private void backToOrigin() {
            taskRunMan.setVisibility(View.VISIBLE);
            taskEndMan.setVisibility(View.GONE);
            taskFlagY.setImageResource(R.mipmap.task_flag_y);
            taskCloudSecond.setVisibility(View.GONE);
            taskTotalPoints.setVisibility(View.VISIBLE);
            taskFirstCloud.setVisibility(View.GONE);
            RelativeLayout.LayoutParams taskRunManAttribuse = (RelativeLayout.LayoutParams) taskRunMan.getLayoutParams();
            //int width = new BigDecimal(taskRunManAttribuseSet.leftMargin + (completeScore / (totalScore * 1f)) * (progressWidth - taskRunMan.getWidth())).intValue();
            taskRunManAttribuse.setMargins(taskRunManAttribuseSet.leftMargin, taskRunManAttribuseSet.topMargin, taskRunManAttribuseSet.rightMargin, taskRunManAttribuseSet.bottomMargin);
            taskRunMan.setLayoutParams(taskRunManAttribuse);
            RelativeLayout.LayoutParams tasCompTipsManAttribuse = (RelativeLayout.LayoutParams) taskCompleteTips.getLayoutParams();
            // int widths = new BigDecimal(taskCompTipsAttribuseSet.leftMargin + (completeScore / (totalScore * 1f)) * (progressWidth - taskCompleteTips.getWidth())).intValue();
            tasCompTipsManAttribuse.setMargins(taskCompTipsAttribuseSet.leftMargin, taskCompTipsAttribuseSet.topMargin, taskCompTipsAttribuseSet.rightMargin, taskCompTipsAttribuseSet.bottomMargin);
            taskCompleteTips.setLayoutParams(tasCompTipsManAttribuse);
            taskRunMan.setX(taskRunManOrigiinX);
            taskCompleteTips.setX(taskCompTipsOriginX);
        }
    
        private float lastProgress;
        private void progressTranslate(final int translateDis) {
            lastProgress  =0;
            backToOrigin();
            progressDuration = ((float) translateDis) / totalScore * progressTime;
            float dist = divide(translateDis + "", totalScore + "").floatValue() * (progressWidth - 40);
            translateRunManAnimation(0, dist);
    
            ValueAnimator animator = ValueAnimator.ofInt(0, translateDis).setDuration(Float.valueOf(progressDuration).longValue());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    float progress = Float.parseFloat(valueAnimator.getAnimatedValue().toString());
                    LogUtil.e("progress", progress);
                    taskProgressBar.setProgress(Float.valueOf(progress).intValue());
                    if (progress >= 5 && taskFirstCloud.getVisibility() == View.GONE) {
                        taskFirstCloud.setVisibility(View.VISIBLE);
                    }
                    taskCompleteTips.setText("已获得" + Float.valueOf(progress).intValue()/jindu + "积分");
                    if (progress == totalScore) {
                        taskRunMan.setVisibility(View.INVISIBLE);
                        taskEndMan.setVisibility(View.VISIBLE);
                        taskFlagY.setImageResource(R.mipmap.task_flag_r);
                        taskCloudSecond.setVisibility(View.VISIBLE);
                        taskTotalPoints.setVisibility(View.INVISIBLE);
                        taskCompleteTips.setText("已获得" + (Float.valueOf(progress).intValue()/jindu+extralScore) + "积分");
                        View view = getLayoutInflater().inflate(R.layout.task_finish_dialog, null);
                        TextView textView = view.findViewById(R.id.gain_points);
                        textView.setText("+" + extralScore);
                        final TaskDialog mMyDialog = new TaskDialog(TaskActivity.this, 0, 0, view, R.style.TaskDialogTheme);
                        mMyDialog.setCancelable(true);
                        if (TaskActivity.this == null || TaskActivity.this.isDestroyed() || TaskActivity.this.isFinishing())
                            return;
                        if(!SharedPreferenceUtils.getSharedPreferenceUtils().getAllTasksFinished()){
                            mMyDialog.show();
                            SharedPreferenceUtils.getSharedPreferenceUtils().setAllTasksFinished(true);
                           mhadler.postDelayed(new Runnable() {
                               @Override
                               public void run() {
                                   if (TaskActivity.this == null || TaskActivity.this.isDestroyed() || TaskActivity.this.isFinishing() || !mMyDialog.isShowing())
                                       return;
                                       mMyDialog.dismiss();
    
                               }
                           },2000);
                        }
                    }
                    if(progress !=0 && progress != lastProgress) {
                        float startPercent = divide("32.5", "217").floatValue(),
                                stopPercent = divide("187", "217").floatValue();
                        float percentTime = divide(progress + "", totalScore + "").floatValue();
                        if ((startPercent) < percentTime && percentTime <= stopPercent) {
                            float distCom = divide(progress - lastProgress + "", totalScore + "")
                                    .multiply(StringToDecimal(progressWidth+"")).floatValue();
                            //taskCompleteTips.setX(taskCompleteTips.getX() + distCom);
                            float startdis =StringToDecimal(totalScore+"").multiply(StringToDecimal(startPercent+"")).floatValue();
                           float bb= taskCompTipsOriginX+divide(progress-startdis+ "", totalScore + "")
                                    .multiply(StringToDecimal(progressWidth+"")).floatValue();
                            taskCompleteTips.setX(bb);
                            LogUtil.e("progressw",taskCompleteTips.getX()+"-"+bb);
                        }else if(percentTime >= stopPercent){
                            taskCompleteTips.setX(taskCompTipsOriginX + (stopPercent-startPercent)*progressWidth);
                        }
                        lastProgress = progress;
                    }
    
                }
    
            });
            animator.start();
        }
    
        float progressTime = 3000;
        private synchronized void translateRunManAnimation(float fromx, final float dist) {
            TranslateAnimation translateAnimation = new TranslateAnimation(fromx, dist, 0, 0);
            translateAnimation.setDuration(Float.valueOf(progressDuration).longValue());
            translateAnimation.setFillEnabled(true);
            translateAnimation.setFillAfter(true);
            translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    taskRunMan.clearAnimation();
                    taskRunMan.setX(taskRunMan.getX() + dist);
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            taskRunMan.setAnimation(translateAnimation);//给imageView添加的动画效果
            taskRunMan.startAnimation(translateAnimation);
        }
    

    相关文章

      网友评论

          本文标题:2019-05-14 自己实现的一个动态进度条

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