-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomWaveView.java
124 lines (102 loc) · 3.62 KB
/
CustomWaveView.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
package com.example.customprogressbar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
public class CustomWaveView extends View {
private Context mContext;
private Paint stroke_paint, shadow_paint, fill_paint;
public CustomWaveView(Context context) {
super(context);
init(context);
}
public CustomWaveView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomWaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mContext = context;
//set stroke paint
stroke_paint = new Paint();
stroke_paint.setDither(true);
stroke_paint.setAntiAlias(true);
stroke_paint.setColor(Color.parseColor("#E1E3EF"));
stroke_paint.setStyle(Paint.Style.STROKE);
stroke_paint.setStrokeCap(Paint.Cap.ROUND);
stroke_paint.setStrokeJoin(Paint.Join.ROUND);
stroke_paint.setStrokeWidth(dpToPx(5));
//set shadow paint
shadow_paint = new Paint();
shadow_paint.setDither(true);
shadow_paint.setAntiAlias(true);
shadow_paint.setColor(Color.parseColor("#cccccc"));
shadow_paint.setStyle(Paint.Style.STROKE);
shadow_paint.setStrokeCap(Paint.Cap.ROUND);
shadow_paint.setStrokeJoin(Paint.Join.ROUND);
shadow_paint.setStrokeWidth(dpToPx(3));
//set fill paint
// fill_paint = new Paint();
// fill_paint.setDither(true);
// fill_paint.setAntiAlias(true);
// fill_paint.setColor(Color.parseColor("#E1E3EF"));
// fill_paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//handling height
int height = canvas.getHeight();
int midHeight = height / 2;
float amplitude = height / 6;
//handling width
int width = canvas.getWidth();
int waveCount = ProgressBarActivity.childCount;
int children_per_sinWave = 2;
float sinWaveCount = (float)waveCount/children_per_sinWave;
float sinWaveWidth = width/sinWaveCount;
float period = (float) Math.toDegrees(2 * Math.PI); //360
float frequency = sinWaveWidth/period;
float time = 1 / frequency;
float shadow_offset = dpToPx(6);
/* border stroke loop */
for (int i = 0; i < 180 * 6; i = i + 1) {
float x1 = i;
float y1 = midHeight + amplitude * (float) Math.sin(Math.toRadians(x1) * time);
float x2 = i + 1;
float y2 = midHeight + amplitude * (float) Math.sin(Math.toRadians(x2) * time);
//border stroke
canvas.drawLine(x1, y1, x2, y2, stroke_paint);
}
/*shadow stroke loop*/
for (int i = 0; i < 180 * 6; i = i + 20) {
float x1 = i;
float y1 = midHeight + amplitude * (float) Math.sin(Math.toRadians(x1) * time);
float x2 = i + 1;
float y2 = midHeight + amplitude * (float) Math.sin(Math.toRadians(x2) * time);
//shadow stroke
canvas.drawLine(x1, y1+shadow_offset, x2, y2+shadow_offset, shadow_paint);
}
/*fill color loop*/
// Path path = new Path();
// for (int i = 0; i < 180 * 6; i++) {
// float x1 = i;
// float y1 = midHeight + amplitude * (float) Math.sin(Math.toRadians(x1) * time);
// path.lineTo(x1,y1);
// path.lineTo(i,0);
//
// //fill path
// canvas.drawPath(path,fill_paint);
// }
}
private float dpToPx(int x){
return TypedValue.applyDimension((int)TypedValue.COMPLEX_UNIT_DIP,x,mContext.getResources().getDisplayMetrics());
}
}