0

I have a custom seek bar. I want to progress animate in seek bar. I also progress thumb image but progress bar not. Thumb and progress values are different. How can I progress animate?

    seekbarElectra.setMax((int) totalSpanElectra);
    seekbarElectra.setProgress((int) thumbPositionElectra);

    progressItemListElectra = new ArrayList<ProgressItem>();
    // green span
    mProgressItemElectra = new ProgressItem();
    mProgressItemElectra.progressItemPercentage = (greenSpanElectra / totalSpanElectra) * 100;
    mProgressItemElectra.color = R.color.green;
    progressItemListElectra.add(mProgressItemElectra);

    // greyspan
    mProgressItemElectra = new ProgressItem();
    mProgressItemElectra.progressItemPercentage = (darkGreySpanElectra / totalSpanElectra) * 100;
    mProgressItemElectra.color = R.color.colorgray;
    progressItemListElectra.add(mProgressItemElectra);
    seekbarElectra.initData(progressItemListElectra);
    seekbarElectra.invalidate();
}

---------------------------custom seekbar------------

    if (mProgressItemsList.size() > 0) {
        int progressBarWidth = getWidth();
        int progressBarHeight = getHeight();
        int thumboffset = getThumbOffset();
        int lastProgressX = 0;
        int progressItemWidth, progressItemRight;
        for (int i = 0; i < mProgressItemsList.size(); i++) {
            ProgressItem progressItem = mProgressItemsList.get(i);

            Paint progressPaint = new Paint();

            progressPaint.setColor(getResources().getColor(progressItem.color));

            progressItemWidth = (int) (progressItem.progressItemPercentage * progressBarWidth / 100);

            progressItemRight = lastProgressX + progressItemWidth;
            // for last item give right to progress item to the width
            if (i == mProgressItemsList.size() - 1 && progressItemRight != progressBarWidth) {
                progressItemRight = progressBarWidth;
            }
            Rect progressRect = new Rect();
            progressRect.set(lastProgressX, thumboffset / 2,progressItemRight, progressBarHeight - thumboffset / 2);
            canvas.drawRect(progressRect, progressPaint);
            lastProgressX = progressItemRight;
        }
        super.onDraw(canvas);
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
cahbs
  • 11
  • 1
  • 4

2 Answers2

0

Try this:

public class ProgressBarAnimation extends Animation{
private ProgressBar progressBar;
private float from;
private float  to;

public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
    super();
    this.progressBar = progressBar;
    this.from = from;
    this.to = to;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    super.applyTransformation(interpolatedTime, t);
    float value = from + (to - from) * interpolatedTime;
    progressBar.setProgress((int) value);
}

}

to call it :

 ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to);
 anim.setDuration(1000);
 progress.startAnimation(anim);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

First, create a ValueAnimator

private ValueAnimator valueAnimator = new ValueAnimator();

Then, set a listener for it:

private void setListener() {
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int firstAnimatedValue = (int) valueAnimator.getAnimatedValue("firstAnimatedValue");
            int secondAnimatedValue = (int) valueAnimator.getAnimatedValue("secondAnimatedValue");

            // update the properties here
        }
    });
}

Then, to start the animation, set the values as following:

public void setAnimatedValues() {
    valueAnimator.setValues(
            new PropertyValuesHolder("firstAnimatedValue", fromValue1, toValue1),
            new PropertyValuesHolder("secondAnimatedValue", fromValue2, fromValue2)
    );

    valueAnimator.start();
}

When you start the animator, the listener should be called multiple times, with the intermediary values.