我有一个自定义搜索栏。我想在搜索栏中进行动画处理。我也进步拇指图像,但没有进度栏。拇指和进度值不同。如何进行动画制作?
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();
}
---------------------------自定义搜寻栏------------
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);
}
}
答案 0 :(得分:0)
尝试一下:
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);
}
}
称呼它:
ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to);
anim.setDuration(1000);
progress.startAnimation(anim);
答案 1 :(得分:0)
首先,创建一个ValueAnimator
private ValueAnimator valueAnimator = new ValueAnimator();
然后,为其设置一个侦听器:
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
}
});
}
然后,要开始播放动画,请按如下所示设置值:
public void setAnimatedValues() {
valueAnimator.setValues(
new PropertyValuesHolder("firstAnimatedValue", fromValue1, toValue1),
new PropertyValuesHolder("secondAnimatedValue", fromValue2, fromValue2)
);
valueAnimator.start();
}
启动动画器时,应使用中间值多次调用侦听器。