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 — 优化 · luohaiah/AndroidCustomView@57bc2ca

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main
    • java/com/allen/androidcustomview
      • activity
        • ProgressBarActivity.java
        • RadarActivity.java
      • widget
        • CircleProgressBarView.java
        • HorizontalProgressBar.java
        • RadarWaveView.java
        • WaveViewBySinCos.java
    • res
      • layout
        • activity_pregress_bar.xml
        • activity_radar.xml
      • values
        • attrs.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/ProgressBarActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/ProgressBarActivity.java
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.view.View;
66
import android.widget.Button;
77
import android.widget.TextView;
8-
import android.widget.Toast;
98

109
import com.allen.androidcustomview.R;
1110
import com.allen.androidcustomview.widget.CircleProgressBarView;
@@ -30,7 +29,7 @@ protected void onCreate(Bundle savedInstanceState) {
3029
textView = (TextView) findViewById(R.id.progress_tv);
3130
button = (Button) findViewById(R.id.startAnimationBtn);
3231

33-
circleProgressBarView.setProgress(60);
32+
circleProgressBarView.setProgressWithAnimation(60);
3433
circleProgressBarView.setProgressListener(new CircleProgressBarView.ProgressListener() {
3534
@Override
3635
public void currentProgressListener(float currentProgress) {
@@ -39,7 +38,7 @@ public void currentProgressListener(float currentProgress) {
3938
});
4039
circleProgressBarView.startProgressAnimation();
4140

42-
horizontalProgressBar.setProgress(60).setProgressListener(new HorizontalProgressBar.ProgressListener() {
41+
horizontalProgressBar.setProgressWithAnimation(60).setProgressListener(new HorizontalProgressBar.ProgressListener() {
4342
@Override
4443
public void currentProgressListener(float currentProgress) {
4544
}
@@ -50,8 +49,8 @@ public void currentProgressListener(float currentProgress) {
5049
button.setOnClickListener(new View.OnClickListener() {
5150
@Override
5251
public void onClick(View v) {
53-
horizontalProgressBar.setProgress(100);
54-
circleProgressBarView.setProgress(60).startProgressAnimation();
52+
horizontalProgressBar.setProgressWithAnimation(100);
53+
circleProgressBarView.setProgressWithAnimation(60).startProgressAnimation();
5554
circleProgressBarView.setProgressListener(new CircleProgressBarView.ProgressListener() {
5655
@Override
5756
public void currentProgressListener(float currentProgress) {
Collapse file

‎app/src/main/java/com/allen/androidcustomview/activity/RadarActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/RadarActivity.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
import android.os.Bundle;
55

66
import com.allen.androidcustomview.R;
7-
import com.allen.androidcustomview.widget.WaveView;
7+
import com.allen.androidcustomview.widget.RadarWaveView;
88

99
public class RadarActivity extends AppCompatActivity {
1010

11-
private WaveView radarView;
11+
private RadarWaveView radarView;
1212

1313
@Override
1414
protected void onCreate(Bundle savedInstanceState) {
1515
super.onCreate(savedInstanceState);
1616
setContentView(R.layout.activity_radar);
1717

18-
radarView = (WaveView) findViewById(R.id.radar_view);
18+
radarView = (RadarWaveView) findViewById(R.id.radar_view);
1919
}
2020

2121
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/CircleProgressBarView.java
+106-10Lines changed: 106 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
import android.animation.ValueAnimator;
44
import android.content.Context;
5+
import android.content.res.TypedArray;
56
import android.graphics.Canvas;
67
import android.graphics.Color;
78
import android.graphics.Paint;
89
import android.graphics.RectF;
910
import android.support.annotation.Nullable;
1011
import android.util.AttributeSet;
12+
import android.util.TypedValue;
1113
import android.view.View;
1214
import android.view.animation.DecelerateInterpolator;
15+
import android.view.animation.LinearInterpolator;
16+
17+
import com.allen.androidcustomview.R;
1318

1419
/**
1520
* Created by Allen on 2017/5/14.
@@ -19,8 +24,8 @@
1924

2025
public class CircleProgressBarView extends View {
2126

22-
private float width;
23-
private float height;
27+
private Context mContext;
28+
2429

2530
/**
2631
* 圆心x坐标
@@ -96,6 +101,12 @@ public class CircleProgressBarView extends View {
96101
*/
97102
private int startDelay = 500;
98103

104+
private boolean isDrawCenterProgressText;
105+
private int centerProgressTextSize = 10;
106+
private int centerProgressTextColor = Color.BLACK;
107+
108+
private Paint centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
109+
99110
private ProgressListener progressListener;
100111

101112
public CircleProgressBarView(Context context) {
@@ -104,9 +115,29 @@ public CircleProgressBarView(Context context) {
104115

105116
public CircleProgressBarView(Context context, @Nullable AttributeSet attrs) {
106117
super(context, attrs);
107-
118+
mContext = context;
119+
getAttr(attrs);
108120
initPaint();
121+
initTextPaint();
122+
}
123+
124+
private void getAttr(AttributeSet attrs) {
125+
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressBarView);
126+
127+
circleBgStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_circleBgStrokeWidth, defaultStrokeWidth);
128+
progressStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_progressStrokeWidth, defaultStrokeWidth);
129+
130+
circleBgColor = typedArray.getColor(R.styleable.CircleProgressBarView_circleBgColor, circleBgColor);
131+
progressColor = typedArray.getColor(R.styleable.CircleProgressBarView_progressColor, progressColor);
132+
133+
duration = typedArray.getColor(R.styleable.CircleProgressBarView_circleAnimationDuration, duration);
134+
135+
isDrawCenterProgressText = typedArray.getBoolean(R.styleable.CircleProgressBarView_isDrawCenterProgressText, false);
109136

137+
centerProgressTextColor = typedArray.getColor(R.styleable.CircleProgressBarView_centerProgressTextColor, centerProgressTextColor);
138+
centerProgressTextSize = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_centerProgressTextSize, sp2px(centerProgressTextSize));
139+
140+
typedArray.recycle();
110141
}
111142

112143
private void initPaint() {
@@ -127,16 +158,27 @@ private Paint getPaint(int strokeWidth, int color) {
127158
return paint;
128159
}
129160

161+
/**
162+
* 初始化文字画笔
163+
*/
164+
private void initTextPaint() {
165+
centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
166+
centerProgressTextPaint.setTextSize(centerProgressTextSize);
167+
centerProgressTextPaint.setColor(centerProgressTextColor);
168+
centerProgressTextPaint.setTextAlign(Paint.Align.CENTER);
169+
centerProgressTextPaint.setAntiAlias(true);
170+
}
171+
130172
private void initAnimation() {
131173
progressAnimator = ValueAnimator.ofFloat(0, mProgress);
132174
progressAnimator.setDuration(duration);
133175
progressAnimator.setStartDelay(startDelay);
134-
progressAnimator.setInterpolator(new DecelerateInterpolator());
176+
progressAnimator.setInterpolator(new LinearInterpolator());
135177
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
136178
@Override
137179
public void onAnimationUpdate(ValueAnimator valueAnimator) {
138180
float value = (float) valueAnimator.getAnimatedValue();
139-
181+
mProgress = value;
140182
currentProgress = value * 360 / 100;
141183

142184
if (progressListener != null) {
@@ -151,13 +193,11 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
151193
@Override
152194
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
153195
super.onSizeChanged(w, h, oldw, oldh);
154-
width = w;
155-
height = h;
156196

157197
centerX = w / 2;
158198
centerY = h / 2;
159199

160-
radius = Math.min(w, h) / 2 - circleBgStrokeWidth;
200+
radius = Math.min(w, h) / 2 - Math.max(circleBgStrokeWidth, progressStrokeWidth);
161201

162202
rectF.set(centerX - radius,
163203
centerY - radius,
@@ -171,8 +211,19 @@ protected void onDraw(Canvas canvas) {
171211
super.onDraw(canvas);
172212
canvas.drawCircle(centerX, centerY, radius, circleBgPaint);
173213
canvas.drawArc(rectF, 90, currentProgress, false, progressPaint);
214+
if (isDrawCenterProgressText) {
215+
drawCenterProgressText(canvas, (int) mProgress + "%");
216+
}
174217
}
175218

219+
private void drawCenterProgressText(Canvas canvas, String currentProgress) {
220+
Paint.FontMetricsInt fontMetrics = centerProgressTextPaint.getFontMetricsInt();
221+
int baseline = (int) ((rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
222+
//文字绘制到整个布局的中心位置
223+
canvas.drawText(currentProgress, rectF.centerX(), baseline, centerProgressTextPaint);
224+
}
225+
226+
176227
public void startProgressAnimation() {
177228
progressAnimator.start();
178229
}
@@ -189,12 +240,33 @@ public void stopProgressAnimation() {
189240
progressAnimator.end();
190241
}
191242

192-
public CircleProgressBarView setProgress(float progress) {
243+
244+
/**
245+
* 传入一个进度值,从0到progress动画变化
246+
*
247+
* @param progress
248+
* @return
249+
*/
250+
public CircleProgressBarView setProgressWithAnimation(float progress) {
193251
mProgress = progress;
194252
initAnimation();
195253
return this;
196254
}
197255

256+
/**
257+
* 实时进度,适用于下载进度回调时候之类的场景
258+
*
259+
* @param progress
260+
* @return
261+
*/
262+
public CircleProgressBarView setCurrentProgress(float progress) {
263+
mProgress = progress;
264+
currentProgress = progress * 360 / 100;
265+
invalidate();
266+
return this;
267+
}
268+
269+
198270
public interface ProgressListener {
199271
void currentProgressListener(float currentProgress);
200272
}
@@ -203,12 +275,36 @@ public CircleProgressBarView setProgressListener(ProgressListener listener) {
203275
progressListener = listener;
204276
return this;
205277
}
278+
206279
/**
207280
* 将一个小数四舍五入,保留两位小数返回
281+
*
208282
* @param originNum
209283
* @return
210284
*/
211285
public static float roundTwo(float originNum) {
212-
return (float) (Math.round(originNum * 10) / 10.0);
286+
return (float) (Math.round(originNum * 10) / 10.00);
287+
}
288+
289+
/**
290+
* dp 2 px
291+
*
292+
* @param dpVal
293+
*/
294+
protected int dp2px(int dpVal) {
295+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
296+
dpVal, getResources().getDisplayMetrics());
297+
}
298+
299+
/**
300+
* sp 2 px
301+
*
302+
* @param spVal
303+
* @return
304+
*/
305+
protected int sp2px(int spVal) {
306+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
307+
spVal, getResources().getDisplayMetrics());
308+
213309
}
214310
}
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/HorizontalProgressBar.java
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,32 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
351351
}
352352

353353

354-
public HorizontalProgressBar setProgress(float progress) {
354+
/**
355+
* 设置进度条带动画效果
356+
*
357+
* @param progress
358+
* @return
359+
*/
360+
public HorizontalProgressBar setProgressWithAnimation(float progress) {
355361
mProgress = progress;
356362
initAnimation();
357363
return this;
358364
}
359365

366+
/**
367+
* 实时显示进度
368+
*
369+
* @param progress
370+
* @return
371+
*/
372+
public HorizontalProgressBar setCurrentProgress(float progress) {
373+
mProgress = progress;
374+
currentProgress = progress * mWidth / 100;
375+
textString = formatNum(format2Int(progress));
376+
invalidate();
377+
return this;
378+
}
379+
360380
/**
361381
* 开启动画
362382
*/
Collapse file

‎…n/androidcustomview/widget/WaveView.java‎ ‎…roidcustomview/widget/RadarWaveView.java‎app/src/main/java/com/allen/androidcustomview/widget/WaveView.java renamed to app/src/main/java/com/allen/androidcustomview/widget/RadarWaveView.java app/src/main/java/com/allen/androidcustomview/widget/WaveView.java renamed to app/src/main/java/com/allen/androidcustomview/widget/RadarWaveView.java

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/RadarWaveView.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* 雷达水波扩散动画
2121
*/
2222

23-
public class WaveView extends View {
23+
public class RadarWaveView extends View {
2424

2525
/**
2626
* 圆形背景画笔
@@ -85,15 +85,15 @@ public class WaveView extends View {
8585
private AnimatorSet mAnimatorSet;
8686

8787

88-
public WaveView(Context context) {
88+
public RadarWaveView(Context context) {
8989
this(context, null);
9090
}
9191

92-
public WaveView(Context context, @Nullable AttributeSet attrs) {
92+
public RadarWaveView(Context context, @Nullable AttributeSet attrs) {
9393
this(context, attrs, 0);
9494
}
9595

96-
public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
96+
public RadarWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
9797
super(context, attrs, defStyleAttr);
9898

9999
initBgPaint();
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/WaveViewBySinCos.java
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ public WaveViewBySinCos(Context context, AttributeSet attrs) {
9393
}
9494

9595
private void getAttr(AttributeSet attrs) {
96-
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.WaveView);
97-
98-
waveType = typedArray.getInt(R.styleable.WaveView_waveType, SIN);
99-
waveFillType = typedArray.getInt(R.styleable.WaveView_waveFillType, BOTTOM);
100-
A = typedArray.getDimensionPixelOffset(R.styleable.WaveView_waveAmplitude, dp2px(10));
101-
waveColor = typedArray.getColor(R.styleable.WaveView_waveColor, waveColor);
102-
waveSpeed = typedArray.getFloat(R.styleable.WaveView_waveSpeed, waveSpeed);
103-
startPeriod = typedArray.getFloat(R.styleable.WaveView_waveStartPeriod, 0);
104-
waveStart = typedArray.getBoolean(R.styleable.WaveView_waveStart, false);
96+
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.RadarWaveView);
97+
98+
waveType = typedArray.getInt(R.styleable.RadarWaveView_waveType, SIN);
99+
waveFillType = typedArray.getInt(R.styleable.RadarWaveView_waveFillType, BOTTOM);
100+
A = typedArray.getDimensionPixelOffset(R.styleable.RadarWaveView_waveAmplitude, dp2px(10));
101+
waveColor = typedArray.getColor(R.styleable.RadarWaveView_waveColor, waveColor);
102+
waveSpeed = typedArray.getFloat(R.styleable.RadarWaveView_waveSpeed, waveSpeed);
103+
startPeriod = typedArray.getFloat(R.styleable.RadarWaveView_waveStartPeriod, 0);
104+
waveStart = typedArray.getBoolean(R.styleable.RadarWaveView_waveStart, false);
105105

106106
typedArray.recycle();
107107
}
Collapse file

‎app/src/main/res/layout/activity_pregress_bar.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/activity_pregress_bar.xml
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:cpbv="http://schemas.android.com/apk/res-auto"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
@@ -10,7 +11,13 @@
1011
android:id="@+id/circle_progress_view"
1112
android:layout_width="match_parent"
1213
android:layout_height="200dp"
13-
android:layout_marginTop="20dp" />
14+
android:layout_marginTop="20dp"
15+
cpbv:centerProgressTextColor="@color/colorAccent"
16+
cpbv:centerProgressTextSize="20sp"
17+
cpbv:circleBgStrokeWidth="10dp"
18+
cpbv:isDrawCenterProgressText="true"
19+
cpbv:progressStrokeWidth="10dp" />
20+
1421

1522
<TextView
1623
android:id="@+id/progress_tv"
Collapse file

‎app/src/main/res/layout/activity_radar.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/activity_radar.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
android:layout_height="match_parent"
55
android:background="@mipmap/beijing2x">
66

7-
<com.allen.androidcustomview.widget.WaveView
7+
<com.allen.androidcustomview.widget.RadarWaveView
88
android:id="@+id/radar_view"
99
android:layout_gravity="center"
1010
android:layout_width="wrap_content"

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.