我创建了一个自定义虚线进度视图,其中3个点受到@Naveen Shriyan在此问题中的回答How to make custom dotted progress bar in android?
public class DottedProgressView extends View {
//actual dot radius
private int mDotRadius = 25;
private Paint paint = new Paint();
//Bounced Dot Radius
private int mBounceDotRadius = 25;
//to get identified in which position dot has to bounce
private int mDotPosition;
//specify how many dots you need in a progressbar
private int mDotAmount = 3;
public DottedProgressView(Context context) {
super(context);
}
public DottedProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DottedProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//function to create dot
createDot(canvas,paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width;
int height;
//calculate the view width
int calculatedWidth = (75*3);
width = calculatedWidth;
height = (mDotRadius*2+10);
//MUST CALL THIS
setMeasuredDimension(width, height);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
startAnimation();
}
private void createDot(Canvas canvas, Paint paint) {
//here i have setted progress bar with 10 dots , so repeat and wnen i = mDotPosition then increase the radius of dot i.e mBounceDotRadius
for(int i = 0; i < mDotAmount; i++ ){
if(i == mDotPosition){
paint.setColor(Color.parseColor("#FB8C00"));
canvas.drawCircle(30+(i*75), mDotRadius, mDotRadius, paint);
} else if (i == mDotPosition - 1) {
paint.setColor(Color.parseColor("#FFA726"));
canvas.drawCircle(30+(i*75), mDotRadius, mDotRadius, paint);
}
else {
paint.setColor(Color.parseColor("#FFE0B2"));
canvas.drawCircle(30+(i*75), mDotRadius, mDotRadius, paint);
}
}
}
private void startAnimation() {
BounceAnimation bounceAnimation = new BounceAnimation();
bounceAnimation.setDuration(180);
bounceAnimation.scaleCurrentDuration(1.5f);
bounceAnimation.setRepeatCount(Animation.INFINITE);
bounceAnimation.setInterpolator(new FastOutSlowInInterpolator());
bounceAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {
mDotPosition++;
//when mDotPosition == mDotAmount , then start again applying animation from 0th positon , i.e mDotPosition = 0;
if (mDotPosition == mDotAmount) {
mDotPosition = 0;
}
}
});
startAnimation(bounceAnimation);
}
private class BounceAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
//call invalidate to redraw your view againg.
invalidate();
}
}
}
我还希望在动画发生时为每个点添加淡出动画,以使3点之间的过渡更加平滑。我怎样才能实现这一目标?