|
| 1 | +package app.dinus.com.loadingdrawable; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Canvas; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.ColorFilter; |
| 7 | +import android.graphics.LinearGradient; |
| 8 | +import android.graphics.Paint; |
| 9 | +import android.graphics.Rect; |
| 10 | +import android.graphics.RectF; |
| 11 | +import android.graphics.Shader; |
| 12 | +import android.support.annotation.NonNull; |
| 13 | +import android.util.DisplayMetrics; |
| 14 | +import android.util.TypedValue; |
| 15 | +import android.view.animation.AccelerateInterpolator; |
| 16 | +import android.view.animation.DecelerateInterpolator; |
| 17 | +import android.view.animation.Interpolator; |
| 18 | + |
| 19 | +public class CollisionLoadingRenderer extends LoadingRenderer { |
| 20 | + private static final Interpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator(); |
| 21 | + private static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(); |
| 22 | + |
| 23 | + private static final float DEFAULT_WIDTH = 15.0f * 11; |
| 24 | + private static final float DEFAULT_HEIGHT = 15.0f * 5; |
| 25 | + |
| 26 | + private static final float DURATION_OFFSET = 0.25f; |
| 27 | + private static final float START_LEFT_DURATION_OFFSET = 0.25f; |
| 28 | + private static final float START_RIGHT_DURATION_OFFSET = 0.5f; |
| 29 | + private static final float END_RIGHT_DURATION_OFFSET = 0.75f; |
| 30 | + |
| 31 | + private static final int CIRCLE_COUNT = 7; |
| 32 | + |
| 33 | + private static final int[] DEFAULT_COLORS = new int[] { |
| 34 | + Color.RED, Color.GREEN |
| 35 | + }; |
| 36 | + |
| 37 | + private static final float[] DEFAULT_POSITIONS = new float[] { |
| 38 | + 0.0f, 1.0f |
| 39 | + }; |
| 40 | + |
| 41 | + private final Paint mPaint = new Paint(); |
| 42 | + private final RectF mTempBounds = new RectF(); |
| 43 | + |
| 44 | + private int[] mColors; |
| 45 | + private float[] mPositions; |
| 46 | + |
| 47 | + private float mEndXOffsetProgress; |
| 48 | + private float mStartXOffsetProgress; |
| 49 | + |
| 50 | + public CollisionLoadingRenderer(Context context) { |
| 51 | + super(context); |
| 52 | + setupPaint(); |
| 53 | + |
| 54 | + DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); |
| 55 | + mWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_WIDTH, displayMetrics); |
| 56 | + mHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_HEIGHT, displayMetrics); |
| 57 | + } |
| 58 | + |
| 59 | + private void setupPaint() { |
| 60 | + mColors = DEFAULT_COLORS; |
| 61 | + mPositions = DEFAULT_POSITIONS; |
| 62 | + |
| 63 | + mPaint.setAntiAlias(true); |
| 64 | + mPaint.setStrokeWidth(getStrokeWidth()); |
| 65 | + mPaint.setStyle(Paint.Style.FILL); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void draw(Canvas canvas, Rect bounds) { |
| 70 | + int saveCount = canvas.save(); |
| 71 | + |
| 72 | + RectF arcBounds = mTempBounds; |
| 73 | + arcBounds.set(bounds); |
| 74 | + |
| 75 | + LinearGradient gradient = new LinearGradient(arcBounds.left, 0, arcBounds.right, 0, |
| 76 | + mColors, mPositions, Shader.TileMode.CLAMP); |
| 77 | + mPaint.setShader(gradient); |
| 78 | + |
| 79 | + float cy = mHeight / 2 ; |
| 80 | + float circleRadius = computeCircleRadius(arcBounds); |
| 81 | + |
| 82 | + float sideOffset = 2.0f * (2 * circleRadius); |
| 83 | + float maxMoveOffset = 1.5f * (2 * circleRadius); |
| 84 | + |
| 85 | + for (int i = 0; i < CIRCLE_COUNT; i++) { |
| 86 | + if (i == 0 && mStartXOffsetProgress != 0) { |
| 87 | + float xMoveOffset = maxMoveOffset * mStartXOffsetProgress; |
| 88 | + // y = ax^2 --> if x = sideOffset, y = sideOffset ==> a = 1 / sideOffset |
| 89 | + float yMoveOffset = (float) (Math.pow(xMoveOffset, 2) / maxMoveOffset); |
| 90 | + canvas.drawCircle(circleRadius + sideOffset - xMoveOffset , cy - yMoveOffset, circleRadius, mPaint); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (i == CIRCLE_COUNT - 1 && mEndXOffsetProgress != 0) { |
| 95 | + float xMoveOffset = maxMoveOffset * mEndXOffsetProgress; |
| 96 | + // y = ax^2 --> if x = sideOffset, y = sideOffset / 2 ==> a = 1 / sideOffset |
| 97 | + float yMoveOffset = (float) (Math.pow(xMoveOffset, 2) / maxMoveOffset); |
| 98 | + canvas.drawCircle(circleRadius * (CIRCLE_COUNT * 2 - 1) + sideOffset + xMoveOffset , cy - yMoveOffset, circleRadius, mPaint); |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + canvas.drawCircle(circleRadius * (i * 2 + 1) + sideOffset , cy, circleRadius, mPaint); |
| 103 | + } |
| 104 | + |
| 105 | + canvas.restoreToCount(saveCount); |
| 106 | + } |
| 107 | + |
| 108 | + private float computeCircleRadius(RectF rectBounds) { |
| 109 | + float width = rectBounds.width(); |
| 110 | + float height = rectBounds.height(); |
| 111 | + |
| 112 | + //CIRCLE_COUNT + 4 is the sliding distance of both sides |
| 113 | + float radius = Math.min(width / (CIRCLE_COUNT + 4) / 2, height / 2); |
| 114 | + return radius; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void computeRender(float renderProgress) { |
| 119 | + |
| 120 | + // Moving the start offset to left only occurs in the first 25% of a |
| 121 | + // single ring animation |
| 122 | + if (renderProgress <= START_LEFT_DURATION_OFFSET) { |
| 123 | + float startLeftOffsetProgress = renderProgress / DURATION_OFFSET; |
| 124 | + mStartXOffsetProgress = DECELERATE_INTERPOLATOR.getInterpolation(startLeftOffsetProgress); |
| 125 | + |
| 126 | + invalidateSelf(); |
| 127 | + return ; |
| 128 | + } |
| 129 | + |
| 130 | + // Moving the start offset to left only occurs between 25% and 50% of a |
| 131 | + // single ring animation |
| 132 | + if (renderProgress <= START_RIGHT_DURATION_OFFSET) { |
| 133 | + float startRightOffsetProgress = (renderProgress - START_LEFT_DURATION_OFFSET) / DURATION_OFFSET; |
| 134 | + mStartXOffsetProgress = ACCELERATE_INTERPOLATOR.getInterpolation(1.0f - startRightOffsetProgress); |
| 135 | + |
| 136 | + invalidateSelf(); |
| 137 | + return ; |
| 138 | + } |
| 139 | + |
| 140 | + // Moving the end offset to right starts between 50% and 75% a single ring |
| 141 | + // animation completes |
| 142 | + if (renderProgress <= END_RIGHT_DURATION_OFFSET) { |
| 143 | + float endRightOffsetProgress = (renderProgress - START_RIGHT_DURATION_OFFSET) / DURATION_OFFSET; |
| 144 | + mEndXOffsetProgress = DECELERATE_INTERPOLATOR.getInterpolation(endRightOffsetProgress); |
| 145 | + |
| 146 | + invalidateSelf(); |
| 147 | + return ; |
| 148 | + } |
| 149 | + |
| 150 | + // Moving the end offset to left starts after 75% of a single ring |
| 151 | + // animation completes |
| 152 | + if (renderProgress <= 1.0f) { |
| 153 | + float endRightOffsetProgress = (renderProgress - END_RIGHT_DURATION_OFFSET) / DURATION_OFFSET; |
| 154 | + mEndXOffsetProgress = ACCELERATE_INTERPOLATOR.getInterpolation(1 - endRightOffsetProgress); |
| 155 | + |
| 156 | + invalidateSelf(); |
| 157 | + return ; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void setAlpha(int alpha) { |
| 163 | + mPaint.setAlpha(alpha); |
| 164 | + invalidateSelf(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void setColorFilter(ColorFilter cf) { |
| 169 | + mPaint.setColorFilter(cf); |
| 170 | + invalidateSelf(); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void reset() { |
| 175 | + } |
| 176 | + |
| 177 | + public void setColors(@NonNull int[] colors) { |
| 178 | + mColors = colors; |
| 179 | + } |
| 180 | + |
| 181 | + public void setPositions(@NonNull float[] positions) { |
| 182 | + mPositions = positions; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void setStrokeWidth(float strokeWidth) { |
| 187 | + super.setStrokeWidth(strokeWidth); |
| 188 | + mPaint.setStrokeWidth(strokeWidth); |
| 189 | + invalidateSelf(); |
| 190 | + } |
| 191 | +} |
0 commit comments