forked from zhanghai/MaterialProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndeterminateHorizontalProgressDrawable.java
153 lines (124 loc) · 4.9 KB
/
IndeterminateHorizontalProgressDrawable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2015 Zhang Hai <[email protected]>
* All Rights Reserved.
*/
package me.zhanghai.android.materialprogressbar;
import android.animation.Animator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Keep;
import me.zhanghai.android.materialprogressbar.internal.ThemeUtils;
/**
* A backported {@code Drawable} for indeterminate horizontal {@code ProgressBar}.
*/
public class IndeterminateHorizontalProgressDrawable extends BaseIndeterminateProgressDrawable
implements MaterialProgressDrawable, ShowBackgroundDrawable {
private static final int PROGRESS_INTRINSIC_HEIGHT_DP = 4;
private static final int PADDED_INTRINSIC_HEIGHT_DP = 16;
private static final RectF RECT_BOUND = new RectF(-180, -1, 180, 1);
private static final RectF RECT_PADDED_BOUND = new RectF(-180, -4, 180, 4);
private static final RectF RECT_PROGRESS = new RectF(-144, -1, 144, 1);
private static final RectTransformX RECT_1_TRANSFORM_X = new RectTransformX(-522.6f, 0.1f);
private static final RectTransformX RECT_2_TRANSFORM_X = new RectTransformX(-197.6f, 0.1f);
private int mProgressIntrinsicHeight;
private int mPaddedIntrinsicHeight;
private boolean mShowBackground = true;
private float mBackgroundAlpha;
private RectTransformX mRect1TransformX = new RectTransformX(RECT_1_TRANSFORM_X);
private RectTransformX mRect2TransformX = new RectTransformX(RECT_2_TRANSFORM_X);
/**
* Create a new {@code IndeterminateHorizontalProgressDrawable}.
*
* @param context the {@code Context} for retrieving style information.
*/
public IndeterminateHorizontalProgressDrawable(Context context) {
super(context);
float density = context.getResources().getDisplayMetrics().density;
mProgressIntrinsicHeight = Math.round(PROGRESS_INTRINSIC_HEIGHT_DP * density);
mPaddedIntrinsicHeight = Math.round(PADDED_INTRINSIC_HEIGHT_DP * density);
mBackgroundAlpha = ThemeUtils.getFloatFromAttrRes(android.R.attr.disabledAlpha, 0, context);
mAnimators = new Animator[] {
Animators.createIndeterminateHorizontalRect1(mRect1TransformX),
Animators.createIndeterminateHorizontalRect2(mRect2TransformX)
};
}
/**
* {@inheritDoc}
*/
@Override
public boolean getShowBackground() {
return mShowBackground;
}
/**
* {@inheritDoc}
*/
@Override
public void setShowBackground(boolean show) {
if (mShowBackground != show) {
mShowBackground = show;
invalidateSelf();
}
}
/**
* {@inheritDoc}
*/
@Override
public int getIntrinsicHeight() {
return mUseIntrinsicPadding ? mPaddedIntrinsicHeight : mProgressIntrinsicHeight;
}
@Override
protected void onPreparePaint(Paint paint) {
paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas, int width, int height, Paint paint) {
if (mUseIntrinsicPadding) {
canvas.scale(width / RECT_PADDED_BOUND.width(), height / RECT_PADDED_BOUND.height());
canvas.translate(RECT_PADDED_BOUND.width() / 2, RECT_PADDED_BOUND.height() / 2);
} else {
canvas.scale(width / RECT_BOUND.width(), height / RECT_BOUND.height());
canvas.translate(RECT_BOUND.width() / 2, RECT_BOUND.height() / 2);
}
if (mShowBackground) {
paint.setAlpha(Math.round(mAlpha * mBackgroundAlpha));
drawBackgroundRect(canvas, paint);
paint.setAlpha(mAlpha);
}
drawProgressRect(canvas, mRect2TransformX, paint);
drawProgressRect(canvas, mRect1TransformX, paint);
}
private static void drawBackgroundRect(Canvas canvas, Paint paint) {
canvas.drawRect(RECT_BOUND, paint);
}
private static void drawProgressRect(Canvas canvas, RectTransformX transformX, Paint paint) {
int saveCount = canvas.save();
canvas.translate(transformX.mTranslateX, 0);
canvas.scale(transformX.mScaleX, 1);
canvas.drawRect(RECT_PROGRESS, paint);
canvas.restoreToCount(saveCount);
}
private static class RectTransformX {
public float mTranslateX;
public float mScaleX;
public RectTransformX(float translateX, float scaleX) {
mTranslateX = translateX;
mScaleX = scaleX;
}
public RectTransformX(RectTransformX that) {
mTranslateX = that.mTranslateX;
mScaleX = that.mScaleX;
}
@Keep
@SuppressWarnings("unused")
public void setTranslateX(float translateX) {
mTranslateX = translateX;
}
@Keep
@SuppressWarnings("unused")
public void setScaleX(float scaleX) {
mScaleX = scaleX;
}
}
}