Skip to content

Instantly share code, notes, and snippets.

@hamurcuabi
Last active October 29, 2020 23:53
Show Gist options
  • Save hamurcuabi/e50a2128f6a7e3b7be6428d742972f7b to your computer and use it in GitHub Desktop.
Save hamurcuabi/e50a2128f6a7e3b7be6428d742972f7b to your computer and use it in GitHub Desktop.
LoadingAnimation
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
/**
* Created by hamurcuabi on 30,October,2020
**/
public class HorizontalDottedProgress extends View {
//actual dot radius
private int mDotRadius = 5;
//Bounced Dot Radius
private int mBounceDotRadius = 9;
//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 = 8;
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) {
for (int i = 0; i < mDotAmount; i++) {
if (i==mDotPosition%mDotAmount) {
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 * mDotAmount);
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++;
}
});
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment