|
6 | 6 | import android.graphics.Color; |
7 | 7 | import android.graphics.Paint; |
8 | 8 | import android.graphics.Path; |
9 | | -import android.os.Build; |
10 | | -import android.support.annotation.RequiresApi; |
11 | 9 | import android.util.AttributeSet; |
12 | | -import android.util.Log; |
| 10 | +import android.util.TypedValue; |
13 | 11 | import android.view.View; |
14 | 12 | import android.view.animation.LinearInterpolator; |
15 | 13 |
|
16 | | -import static android.content.ContentValues.TAG; |
17 | 14 |
|
18 | 15 | /** |
19 | 16 | * Created by allen on 2016/12/13. |
@@ -39,18 +36,8 @@ public class WaveViewByBezier extends View implements View.OnClickListener { |
39 | 36 | /** |
40 | 37 | * 一个周期波浪的长度 |
41 | 38 | */ |
42 | | - private int mWaveLength = 1000; |
| 39 | + private int mWaveLength; |
43 | 40 |
|
44 | | - |
45 | | - /** |
46 | | - * 屏幕宽度内波浪显示几个周期 |
47 | | - */ |
48 | | - private int mWaveCount; |
49 | | - |
50 | | - /** |
51 | | - * 波纹的中间轴(基准线) |
52 | | - */ |
53 | | - private int mCenterY; |
54 | 41 | /** |
55 | 42 | * 波浪的路径 |
56 | 43 | */ |
@@ -64,33 +51,33 @@ public class WaveViewByBezier extends View implements View.OnClickListener { |
64 | 51 | /** |
65 | 52 | * 振幅 |
66 | 53 | */ |
67 | | - private int mWaveAmplitude = 25; |
| 54 | + private int mWaveAmplitude = 50; |
68 | 55 |
|
69 | 56 | private ValueAnimator valueAnimator; |
70 | 57 |
|
71 | 58 |
|
72 | | - private boolean isStart = true; |
| 59 | + private boolean isStart = false; |
73 | 60 |
|
74 | 61 | public WaveViewByBezier(Context context) { |
75 | | - super(context); |
| 62 | + this(context, null); |
76 | 63 |
|
77 | 64 | } |
78 | 65 |
|
79 | 66 | public WaveViewByBezier(Context context, AttributeSet attrs) { |
80 | | - super(context, attrs); |
81 | | - init(); |
| 67 | + this(context, attrs, 0); |
82 | 68 |
|
83 | 69 | } |
84 | 70 |
|
85 | 71 | public WaveViewByBezier(Context context, AttributeSet attrs, int defStyleAttr) { |
86 | 72 | super(context, attrs, defStyleAttr); |
| 73 | + init(); |
87 | 74 | } |
88 | 75 |
|
89 | 76 |
|
90 | 77 | private void init() { |
| 78 | + mWaveAmplitude = dp2px(15); |
91 | 79 | initPaint(); |
92 | | - |
93 | | - initAnimation(); |
| 80 | + setOnClickListener(this); |
94 | 81 |
|
95 | 82 | } |
96 | 83 |
|
@@ -108,27 +95,61 @@ private void initPaint() { |
108 | 95 |
|
109 | 96 | } |
110 | 97 |
|
111 | | - |
112 | 98 | @Override |
113 | | - protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
114 | | - super.onSizeChanged(w, h, oldw, oldh); |
115 | | - mScreenHeight = h; |
116 | | - mScreenWidth = w; |
| 99 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 100 | + int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 101 | + int width = MeasureSpec.getSize(widthMeasureSpec); |
117 | 102 |
|
118 | | -// mWaveCount = (int) Math.round(mScreenWidth / mWaveLength + 1.5); |
119 | | - mWaveCount = 1; |
120 | | - Log.d(TAG, "onSizeChanged: =======" + mWaveCount); |
121 | | - mCenterY = mScreenHeight * 3 / 4; |
| 103 | + int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 104 | + int height = MeasureSpec.getSize(heightMeasureSpec); |
122 | 105 |
|
123 | | - setOnClickListener(this); |
| 106 | + setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height)); |
| 107 | + } |
124 | 108 |
|
| 109 | + /** |
| 110 | + * 测量宽度 |
| 111 | + * |
| 112 | + * @param mode |
| 113 | + * @param width |
| 114 | + * @return |
| 115 | + */ |
| 116 | + private int measureWidth(int mode, int width) { |
| 117 | + switch (mode) { |
| 118 | + case MeasureSpec.UNSPECIFIED: |
| 119 | + case MeasureSpec.AT_MOST: |
| 120 | + break; |
| 121 | + case MeasureSpec.EXACTLY: |
| 122 | + mScreenWidth = width; |
| 123 | + break; |
| 124 | + } |
| 125 | + return mScreenWidth; |
125 | 126 | } |
126 | 127 |
|
| 128 | + /** |
| 129 | + * 测量高度 |
| 130 | + * |
| 131 | + * @param mode |
| 132 | + * @param height |
| 133 | + * @return |
| 134 | + */ |
| 135 | + private int measureHeight(int mode, int height) { |
| 136 | + switch (mode) { |
| 137 | + case MeasureSpec.UNSPECIFIED: |
| 138 | + case MeasureSpec.AT_MOST: |
| 139 | + break; |
| 140 | + case MeasureSpec.EXACTLY: |
| 141 | + mScreenHeight = height; |
| 142 | + break; |
| 143 | + } |
| 144 | + return mScreenHeight; |
| 145 | + } |
| 146 | + |
| 147 | + |
127 | 148 | @Override |
128 | 149 | protected void onDraw(Canvas canvas) { |
129 | 150 | super.onDraw(canvas); |
130 | 151 |
|
131 | | - mWaveLength = getWidth(); |
| 152 | + mWaveLength = mScreenWidth; |
132 | 153 | mWavePath.reset(); |
133 | 154 |
|
134 | 155 | // mWavePath.moveTo(-mWaveLength + mOffset, mCenterY); |
@@ -141,21 +162,36 @@ protected void onDraw(Canvas canvas) { |
141 | 162 | // mWavePath.lineTo(0, mScreenHeight); |
142 | 163 | // mWavePath.close(); |
143 | 164 |
|
144 | | - mWavePath.moveTo(mOffset, mCenterY); |
| 165 | + mWavePath.moveTo(-mWaveLength + mOffset, mWaveAmplitude); |
145 | 166 |
|
146 | | - for (int i = 0; i < mWaveCount; i++) { |
147 | | - mWavePath.quadTo(mWaveLength /4+ mOffset, mCenterY + 60, mWaveLength / 2 + mOffset, mCenterY); |
148 | | - mWavePath.quadTo(mWaveLength *3/ 4 + mOffset, mCenterY - 60, + mOffset, mCenterY); |
149 | | - } |
150 | | - mWavePath.lineTo(mScreenWidth, mScreenHeight); |
151 | | - mWavePath.lineTo(0, mScreenHeight); |
| 167 | + mWavePath.quadTo(-mWaveLength * 3 / 4 + mOffset, -mWaveAmplitude, -mWaveLength / 2 + mOffset, mWaveAmplitude); |
| 168 | + |
| 169 | + mWavePath.quadTo(-mWaveLength / 4 + mOffset, 3 * mWaveAmplitude, 0 + mOffset, mWaveAmplitude); |
| 170 | + |
| 171 | + mWavePath.quadTo(mWaveLength / 4 + mOffset, -mWaveAmplitude, mWaveLength / 2 + mOffset, mWaveAmplitude); |
| 172 | + |
| 173 | + mWavePath.quadTo(mWaveLength * 3 / 4 + mOffset, 3 * mWaveAmplitude, mWaveLength + mOffset, mWaveAmplitude); |
| 174 | + |
| 175 | + mWavePath.lineTo(getWidth(), getHeight()); |
| 176 | + mWavePath.lineTo(0, getHeight()); |
152 | 177 | mWavePath.close(); |
| 178 | + |
| 179 | + |
| 180 | +// mWavePath.moveTo(mOffset, mCenterY); |
| 181 | +// |
| 182 | +// for (int i = 0; i < mWaveCount; i++) { |
| 183 | +// mWavePath.quadTo(mWaveLength /4+ mOffset, mCenterY + 60, mWaveLength / 2 + mOffset, mCenterY); |
| 184 | +// mWavePath.quadTo(mWaveLength *3/ 4 + mOffset, mCenterY - 60, + mOffset, mCenterY); |
| 185 | +// } |
| 186 | +// mWavePath.lineTo(mScreenWidth, mScreenHeight); |
| 187 | +// mWavePath.lineTo(0, mScreenHeight); |
| 188 | +// mWavePath.close(); |
153 | 189 | canvas.drawPath(mWavePath, mWavePaint); |
154 | 190 | } |
155 | 191 |
|
156 | 192 |
|
157 | 193 | private void initAnimation() { |
158 | | - valueAnimator = ValueAnimator.ofInt(0, getWidth()); |
| 194 | + valueAnimator = ValueAnimator.ofInt(0, mScreenWidth); |
159 | 195 | valueAnimator.setDuration(3000); |
160 | 196 | valueAnimator.setRepeatCount(ValueAnimator.INFINITE); |
161 | 197 | valueAnimator.setInterpolator(new LinearInterpolator()); |
@@ -166,14 +202,13 @@ public void onAnimationUpdate(ValueAnimator animation) { |
166 | 202 | postInvalidate(); |
167 | 203 | } |
168 | 204 | }); |
169 | | - valueAnimator.start(); |
170 | 205 | } |
171 | 206 |
|
172 | 207 | public void startAnimation() { |
173 | 208 | isStart = true; |
174 | 209 | if (valueAnimator != null) { |
175 | 210 |
|
176 | | - valueAnimator.reverse(); |
| 211 | + valueAnimator.start(); |
177 | 212 | } |
178 | 213 | } |
179 | 214 |
|
@@ -184,6 +219,7 @@ public void stopAnimation() { |
184 | 219 | this.clearAnimation(); |
185 | 220 | } |
186 | 221 | } |
| 222 | + |
187 | 223 | public void pauseAnimation() { |
188 | 224 | isStart = false; |
189 | 225 | if (valueAnimator != null) { |
@@ -195,17 +231,33 @@ public void resumeAnimation() { |
195 | 231 | isStart = true; |
196 | 232 | if (valueAnimator != null) { |
197 | 233 | valueAnimator.resume(); |
198 | | - onAnimationStart(); |
199 | 234 | } |
200 | 235 | } |
201 | 236 |
|
202 | 237 |
|
203 | 238 | @Override |
204 | 239 | public void onClick(View v) { |
205 | | - if (isStart){ |
206 | | - pauseAnimation(); |
207 | | - }else { |
208 | | - resumeAnimation(); |
| 240 | + initAnimation(); |
| 241 | + if (valueAnimator != null) { |
| 242 | + if (isStart) { |
| 243 | + isStart = false; |
| 244 | + valueAnimator.pause(); |
| 245 | + } else { |
| 246 | + isStart = true; |
| 247 | + valueAnimator.start(); |
| 248 | + } |
209 | 249 | } |
| 250 | + |
| 251 | + } |
| 252 | + |
| 253 | + |
| 254 | + /** |
| 255 | + * dp 2 px |
| 256 | + * |
| 257 | + * @param dpVal |
| 258 | + */ |
| 259 | + protected int dp2px(int dpVal) { |
| 260 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 261 | + dpVal, getResources().getDisplayMetrics()); |
210 | 262 | } |
211 | 263 | } |