How to make custom dotted progress bar in android?

后端 未结 3 1021
误落风尘
误落风尘 2020-12-09 23:15

Custom progress bar with dots applied animation and given the traversing visual effect. Posting this code here because it can help you to understand and implement new design

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 23:39

    MainActivity.java :

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    }
    

    activity_main.xml :

    
    
    
    
        
    
    
    

    HorizontalDottedProgress.java : This is a custom class to create dots with animation applied.

    public class HorizontalDottedProgress extends View{
    //actual dot radius
    private int mDotRadius = 5;
    
    //Bounced Dot Radius
    private int mBounceDotRadius = 8;
    
    //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 = 10;
    
    public HorizontalDottedProgress(Context context) {
        super(context);
    }
    
    public HorizontalDottedProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public HorizontalDottedProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    //Method to draw your customized dot on the canvas
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        Paint paint = new Paint();
    
        //set the color for the dot that you want to draw
        paint.setColor(Color.parseColor("#fd583f"));
    
        //function to create dot
        createDot(canvas,paint);
    }
    
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //Animation called when attaching to the window, i.e to your screen
        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){
                    canvas.drawCircle(10+(i*20), mBounceDotRadius, mBounceDotRadius, paint);
                }else {
                    canvas.drawCircle(10+(i*20), mBounceDotRadius, mDotRadius, paint);
                }
            }
    
    
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
    
        //calculate the view width
        int calculatedWidth = (20*9);
    
        width = calculatedWidth;
        height = (mBounceDotRadius*2);
    
    
    
        //MUST CALL THIS
        setMeasuredDimension(width, height);
    }
    
    private void startAnimation() {
        BounceAnimation bounceAnimation = new BounceAnimation();
        bounceAnimation.setDuration(100);
        bounceAnimation.setRepeatCount(Animation.INFINITE);
        bounceAnimation.setInterpolator(new LinearInterpolator());
        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;
                }
                Log.d("INFOMETHOD","----On Animation Repeat----");
    
            }
        });
        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();
        }
    }
    }
    

    snap shot:

提交回复
热议问题