GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 完善LoadingButton · stady/AndroidCustomView@3d85b63

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main/java/com/allen/androidcustomview/widget
    • LoadingButton.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/com/allen/androidcustomview/widget/LoadingButton.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/LoadingButton.java
+66-105Lines changed: 66 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.allen.androidcustomview.widget;
22

3-
import android.animation.Animator;
4-
import android.animation.AnimatorSet;
5-
import android.animation.ValueAnimator;
3+
64
import android.content.Context;
75
import android.graphics.Canvas;
86
import android.graphics.Color;
@@ -28,27 +26,27 @@ public class LoadingButton extends Button {
2826

2927
private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
3028

31-
private Paint circlePaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
32-
private Paint circlePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
33-
private Paint circlePaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
29+
private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
30+
31+
32+
private static final int POINT_COLOR_1 = 0x4CFFFFFF;
33+
private static final int POINT_COLOR_2 = 0x7FFFFFFF;
34+
private static final int POINT_COLOR_3 = 0xFFFFFFFF;
3435

35-
private int circleColor = 0xffffff;
36-
// private int circleColor = 0xf66b12;
37-
private int duration = 1000;
36+
private int duration = 300;
3837

3938
private float centerX;
4039
private float centerY;
4140

4241
private float radius;
4342

44-
private AnimatorSet animatorSet;
45-
private ValueAnimator animator1;
46-
private ValueAnimator animator2;
47-
private ValueAnimator animator3;
48-
4943

5044
private boolean isLoading = false;
5145

46+
private int mLoadingIndex = 0;
47+
private Runnable mRunnable;
48+
49+
5250
public LoadingButton(Context context) {
5351
this(context, null);
5452
}
@@ -58,84 +56,18 @@ public LoadingButton(Context context, @Nullable AttributeSet attrs) {
5856

5957
initPaint();
6058

61-
animatorSet = new AnimatorSet();
62-
circleAnimator1();
63-
circleAnimator2();
64-
circleAnimator3();
65-
66-
animatorSet.addListener(new Animator.AnimatorListener() {
67-
@Override
68-
public void onAnimationStart(Animator animation) {
69-
70-
}
71-
72-
@Override
73-
public void onAnimationEnd(Animator animation) {
74-
animatorSet.start();
75-
}
76-
77-
@Override
78-
public void onAnimationCancel(Animator animation) {
79-
80-
}
81-
82-
@Override
83-
public void onAnimationRepeat(Animator animation) {
84-
85-
}
86-
});
87-
animatorSet.play(animator1).with(animator2).with(animator3);
88-
89-
}
90-
91-
private void circleAnimator1() {
92-
animator1 = ValueAnimator.ofInt(255, 76, 165);
93-
animator1.setDuration(duration);
94-
animator1.setRepeatCount(ValueAnimator.INFINITE);
95-
animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
96-
@Override
97-
public void onAnimationUpdate(ValueAnimator animation) {
98-
circlePaint1.setAlpha((Integer) animation.getAnimatedValue());
99-
invalidate();
100-
}
101-
});
10259
}
10360

104-
private void circleAnimator2() {
105-
animator2 = ValueAnimator.ofInt(165, 255, 76);
106-
animator2.setDuration(duration);
107-
animator2.setRepeatCount(ValueAnimator.INFINITE);
108-
animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
109-
@Override
110-
public void onAnimationUpdate(ValueAnimator animation) {
111-
circlePaint2.setAlpha((Integer) animation.getAnimatedValue());
112-
invalidate();
113-
}
114-
});
115-
}
11661

117-
private void circleAnimator3() {
118-
animator3 = ValueAnimator.ofInt(76, 165, 255);
119-
animator3.setDuration(duration);
120-
animator3.setRepeatCount(ValueAnimator.INFINITE);
121-
animator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
62+
private void initPaint() {
63+
initTextPaint();
64+
circlePaint = getPaint(dp2px(1), Paint.Style.FILL);
65+
mRunnable = new Runnable() {
12266
@Override
123-
public void onAnimationUpdate(ValueAnimator animation) {
124-
circlePaint3.setAlpha((Integer) animation.getAnimatedValue());
67+
public void run() {
12568
invalidate();
12669
}
127-
});
128-
}
129-
130-
private void initPaint() {
131-
initTextPaint();
132-
133-
circlePaint1 = getPaint(dp2px(1), circleColor, Paint.Style.FILL);
134-
circlePaint1.setAlpha(255);
135-
circlePaint2 = getPaint(dp2px(1), circleColor, Paint.Style.FILL);
136-
circlePaint2.setAlpha(165);
137-
circlePaint3 = getPaint(dp2px(1), circleColor, Paint.Style.FILL);
138-
circlePaint3.setAlpha(76);
70+
};
13971
}
14072

14173
/**
@@ -163,29 +95,57 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
16395
centerY = mHeight / 2;
16496

16597
radius = mHeight / 8;
166-
Log.d("allen", "onMeasure: "+centerX);
98+
Log.d("allen", "onMeasure: " + centerX);
16799
}
168100

169101

170102
@Override
171103
protected void onDraw(Canvas canvas) {
172104

173105
if (isLoading) {
174-
drawLoading(canvas);
106+
drawLoading(canvas, mLoadingIndex);
107+
mLoadingIndex = (mLoadingIndex + 1) % 3;
108+
postDelayed(mRunnable, duration);
109+
175110
} else {
176111
super.onDraw(canvas);
177112
}
178113

179114
}
180115

181-
private void drawLoading(Canvas canvas) {
182-
canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint1);
183-
184-
canvas.drawCircle(centerX, centerY, radius, circlePaint2);
116+
private void drawLoading(Canvas canvas, int index) {
117+
if (index < 0 || index > 2)
118+
return;
119+
switch (index) {
120+
case 0:
121+
circlePaint.setColor(POINT_COLOR_1);
122+
canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint);
123+
circlePaint.setColor(POINT_COLOR_2);
124+
canvas.drawCircle(centerX, centerY, radius, circlePaint);
125+
circlePaint.setColor(POINT_COLOR_3);
126+
canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint);
127+
break;
128+
case 1:
129+
circlePaint.setColor(POINT_COLOR_3);
130+
canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint);
131+
circlePaint.setColor(POINT_COLOR_1);
132+
canvas.drawCircle(centerX, centerY, radius, circlePaint);
133+
circlePaint.setColor(POINT_COLOR_2);
134+
canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint);
135+
break;
136+
case 2:
137+
circlePaint.setColor(POINT_COLOR_2);
138+
canvas.drawCircle(centerX - radius * 4, centerY, radius, circlePaint);
139+
circlePaint.setColor(POINT_COLOR_3);
140+
canvas.drawCircle(centerX, centerY, radius, circlePaint);
141+
circlePaint.setColor(POINT_COLOR_1);
142+
canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint);
143+
break;
144+
}
185145

186-
canvas.drawCircle(centerX + radius * 4, centerY, radius, circlePaint3);
187146
}
188147

148+
189149
/**
190150
* 绘制文字
191151
*
@@ -201,32 +161,33 @@ private void drawText(Canvas canvas, String textString) {
201161

202162

203163
public void startLoading() {
204-
if (animatorSet != null) {
205-
isLoading = true;
206-
animatorSet.start();
207-
}
164+
if (isLoading)
165+
return;
166+
isLoading = true;
167+
mLoadingIndex = 0;
168+
invalidate();
208169
}
209170

210171
public void stopLoading() {
211-
if (animatorSet != null) {
212-
isLoading = false;
213-
animatorSet.end();
214-
postInvalidate();
215-
}
172+
if (!isLoading)
173+
return;
174+
isLoading = false;
175+
}
176+
177+
public boolean isLoading() {
178+
return isLoading;
216179
}
217180

218181
/**
219182
* 统一处理paint
220183
*
221184
* @param strokeWidth
222-
* @param color
223185
* @param style
224186
* @return
225187
*/
226-
public Paint getPaint(int strokeWidth, int color, Paint.Style style) {
188+
public Paint getPaint(int strokeWidth, Paint.Style style) {
227189
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
228190
paint.setStrokeWidth(strokeWidth);
229-
paint.setColor(color);
230191
paint.setAntiAlias(true);
231192
paint.setStrokeCap(Paint.Cap.ROUND);
232193
paint.setStyle(style);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.