|
| 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.Paint; |
| 8 | +import android.graphics.Rect; |
| 9 | +import android.graphics.RectF; |
| 10 | +import android.support.v4.view.animation.FastOutSlowInInterpolator; |
| 11 | +import android.util.DisplayMetrics; |
| 12 | +import android.util.Log; |
| 13 | +import android.util.TypedValue; |
| 14 | +import android.view.animation.AccelerateInterpolator; |
| 15 | +import android.view.animation.DecelerateInterpolator; |
| 16 | +import android.view.animation.Interpolator; |
| 17 | + |
| 18 | +public class SwapLoadingRenderer extends LoadingRenderer { |
| 19 | + private static final Interpolator MATERIAL_INTERPOLATOR = new FastOutSlowInInterpolator(); |
| 20 | + |
| 21 | + private static final int CIRCLE_COUNT = 5; |
| 22 | + |
| 23 | + //(CIRCLE_COUNT - 1) / 2 is the Circle interval width; the 2 * 2 is the both side inset |
| 24 | + private static final float DEFAULT_WIDTH = 15.0f * (CIRCLE_COUNT + (CIRCLE_COUNT - 1) / 2 + 2 * 2); |
| 25 | + //the 2 * 2 is the both side inset |
| 26 | + private static final float DEFAULT_HEIGHT = 15.0f * (1 + 2 * 2); |
| 27 | + private static final float DEFAULT_STROKE_WIDTH = 1.5f; |
| 28 | + |
| 29 | + private static final int DEFAULT_COLOR = Color.RED; |
| 30 | + |
| 31 | + private final Paint mPaint = new Paint(); |
| 32 | + private final RectF mTempBounds = new RectF(); |
| 33 | + |
| 34 | + private int mColor; |
| 35 | + |
| 36 | + private int mSwapIndex; |
| 37 | + |
| 38 | + private float mSwapThreshold; |
| 39 | + private float mSwapXOffsetProgress; |
| 40 | + |
| 41 | + public SwapLoadingRenderer(Context context) { |
| 42 | + super(context); |
| 43 | + |
| 44 | + DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); |
| 45 | + mWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_WIDTH, displayMetrics); |
| 46 | + mHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_HEIGHT, displayMetrics); |
| 47 | + mStrokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_STROKE_WIDTH, displayMetrics); |
| 48 | + |
| 49 | + mSwapThreshold = 1.0f / CIRCLE_COUNT; |
| 50 | + setupPaint(); |
| 51 | + } |
| 52 | + |
| 53 | + private void setupPaint() { |
| 54 | + mColor = DEFAULT_COLOR; |
| 55 | + |
| 56 | + mPaint.setAntiAlias(true); |
| 57 | + mPaint.setStrokeWidth(getStrokeWidth()); |
| 58 | + mPaint.setStyle(Paint.Style.FILL); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void draw(Canvas canvas, Rect bounds) { |
| 63 | + mPaint.setColor(mColor); |
| 64 | + |
| 65 | + int saveCount = canvas.save(); |
| 66 | + |
| 67 | + RectF arcBounds = mTempBounds; |
| 68 | + arcBounds.set(bounds); |
| 69 | + |
| 70 | + float cy = mHeight / 2 ; |
| 71 | + float circleRadius = computeCircleRadius(arcBounds); |
| 72 | + |
| 73 | + float sideOffset = 2.0f * (2 * circleRadius); |
| 74 | + float intervalWidth = circleRadius; |
| 75 | + |
| 76 | + float circleDiameter = mSwapIndex == CIRCLE_COUNT - 1 |
| 77 | + ? circleRadius * 2 * (CIRCLE_COUNT + 1) |
| 78 | + : circleRadius * 3; |
| 79 | + |
| 80 | + //x^2 + y^2 = (3 * circleRadius / 2) ^ 2 |
| 81 | + float xMoveOffset = mSwapIndex == CIRCLE_COUNT - 1 |
| 82 | + ? -mSwapXOffsetProgress * circleDiameter |
| 83 | + : mSwapXOffsetProgress * circleDiameter; |
| 84 | + //the y axial symmetry |
| 85 | + float xCoordinate = mSwapIndex == CIRCLE_COUNT - 1 |
| 86 | + ? xMoveOffset + circleDiameter / 2 |
| 87 | + : xMoveOffset - circleDiameter / 2; |
| 88 | + float yMoveOffset = (float) (mSwapIndex % 2 == 0 && mSwapIndex != CIRCLE_COUNT -1 |
| 89 | + ? Math.sqrt(Math.pow(circleDiameter / 2, 2.0f) - Math.pow(xCoordinate, 2.0f)) |
| 90 | + : -Math.sqrt(Math.pow(circleDiameter / 2, 2.0f) - Math.pow(xCoordinate, 2.0f))); |
| 91 | + |
| 92 | + for (int i = 0; i < CIRCLE_COUNT; i++) { |
| 93 | + if (i == mSwapIndex) { |
| 94 | + mPaint.setStyle(Paint.Style.FILL); |
| 95 | + canvas.drawCircle(circleRadius * (i * 2 + 1) + sideOffset + i * intervalWidth + xMoveOffset |
| 96 | + , cy - yMoveOffset, circleRadius - getStrokeWidth() / 2, mPaint); |
| 97 | + } else if (i == (mSwapIndex + 1) % CIRCLE_COUNT) { |
| 98 | + mPaint.setStyle(Paint.Style.STROKE); |
| 99 | + |
| 100 | + canvas.drawCircle(circleRadius * (i * 2 + 1) + sideOffset + i * intervalWidth - xMoveOffset |
| 101 | + , cy + yMoveOffset, circleRadius - getStrokeWidth() / 2, mPaint); |
| 102 | + } else { |
| 103 | + mPaint.setStyle(Paint.Style.STROKE); |
| 104 | + |
| 105 | + canvas.drawCircle(circleRadius * (i * 2 + 1) + sideOffset + i * intervalWidth, cy, |
| 106 | + circleRadius - getStrokeWidth() / 2, mPaint); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + canvas.restoreToCount(saveCount); |
| 112 | + } |
| 113 | + |
| 114 | + private float computeCircleRadius(RectF rectBounds) { |
| 115 | + float width = rectBounds.width(); |
| 116 | + float height = rectBounds.height(); |
| 117 | + |
| 118 | + //CIRCLE_COUNT + 4 is the sliding distance of both sides |
| 119 | + float radius = Math.min(width / (CIRCLE_COUNT + (CIRCLE_COUNT - 1) / 2 + 2 * 2) / 2, height / 2); |
| 120 | + return radius; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void computeRender(float renderProgress) { |
| 125 | + mSwapIndex = (int) (renderProgress / mSwapThreshold); |
| 126 | + mSwapXOffsetProgress = MATERIAL_INTERPOLATOR.getInterpolation( |
| 127 | + (renderProgress - mSwapIndex * mSwapThreshold) / mSwapThreshold); |
| 128 | + |
| 129 | + invalidateSelf(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void setAlpha(int alpha) { |
| 134 | + mPaint.setAlpha(alpha); |
| 135 | + invalidateSelf(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void setColorFilter(ColorFilter cf) { |
| 140 | + mPaint.setColorFilter(cf); |
| 141 | + invalidateSelf(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void reset() { |
| 146 | + } |
| 147 | + |
| 148 | + public void setColor(int color) { |
| 149 | + mColor = color; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void setStrokeWidth(float strokeWidth) { |
| 154 | + super.setStrokeWidth(strokeWidth); |
| 155 | + mPaint.setStrokeWidth(strokeWidth); |
| 156 | + invalidateSelf(); |
| 157 | + } |
| 158 | +} |
0 commit comments