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 — 贝塞尔曲线 · tmexcept/AndroidCustomView@8609c6c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .idea
    • misc.xml
  • app/src/main
    • java/com/allen/androidcustomview
      • activity
        • WaveByBezierActivity.java
      • widget
        • WaveViewByBezier.java
    • res/layout
      • activity_wave_by_bezier.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.idea/misc.xml‎

Copy file name to clipboardExpand all lines: .idea/misc.xml
+1-1Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/WaveByBezierActivity.java
-8Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
package com.allen.androidcustomview.activity;
22

3-
import android.os.Build;
4-
import android.support.annotation.RequiresApi;
53
import android.support.v7.app.AppCompatActivity;
64
import android.os.Bundle;
75

86
import com.allen.androidcustomview.R;
9-
import com.allen.androidcustomview.widget.WaveAnimatorView;
107
import com.allen.androidcustomview.widget.WaveViewByBezier;
118

129
public class WaveByBezierActivity extends AppCompatActivity {
1310

14-
private WaveAnimatorView waveAnimatorView;
1511
private WaveViewByBezier waveViewByBezier;
1612
@Override
1713
protected void onCreate(Bundle savedInstanceState) {
1814
super.onCreate(savedInstanceState);
1915
setContentView(R.layout.activity_wave_by_bezier);
2016

21-
waveAnimatorView = (WaveAnimatorView) findViewById(R.id.wave_animator);
22-
2317
waveViewByBezier = (WaveViewByBezier) findViewById(R.id.wave_bezier);
2418
}
2519

@@ -32,13 +26,11 @@ protected void onPause() {
3226
@Override
3327
protected void onResume() {
3428
super.onResume();
35-
// waveViewByBezier.resumeAnimation();
3629
}
3730

3831
@Override
3932
protected void onRestart() {
4033
super.onRestart();
41-
waveViewByBezier.resumeAnimation();
4234
}
4335

4436
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/WaveViewByBezier.java
+100-48Lines changed: 100 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import android.graphics.Color;
77
import android.graphics.Paint;
88
import android.graphics.Path;
9-
import android.os.Build;
10-
import android.support.annotation.RequiresApi;
119
import android.util.AttributeSet;
12-
import android.util.Log;
10+
import android.util.TypedValue;
1311
import android.view.View;
1412
import android.view.animation.LinearInterpolator;
1513

16-
import static android.content.ContentValues.TAG;
1714

1815
/**
1916
* Created by allen on 2016/12/13.
@@ -39,18 +36,8 @@ public class WaveViewByBezier extends View implements View.OnClickListener {
3936
/**
4037
* 一个周期波浪的长度
4138
*/
42-
private int mWaveLength = 1000;
39+
private int mWaveLength;
4340

44-
45-
/**
46-
* 屏幕宽度内波浪显示几个周期
47-
*/
48-
private int mWaveCount;
49-
50-
/**
51-
* 波纹的中间轴(基准线)
52-
*/
53-
private int mCenterY;
5441
/**
5542
* 波浪的路径
5643
*/
@@ -64,33 +51,33 @@ public class WaveViewByBezier extends View implements View.OnClickListener {
6451
/**
6552
* 振幅
6653
*/
67-
private int mWaveAmplitude = 25;
54+
private int mWaveAmplitude = 50;
6855

6956
private ValueAnimator valueAnimator;
7057

7158

72-
private boolean isStart = true;
59+
private boolean isStart = false;
7360

7461
public WaveViewByBezier(Context context) {
75-
super(context);
62+
this(context, null);
7663

7764
}
7865

7966
public WaveViewByBezier(Context context, AttributeSet attrs) {
80-
super(context, attrs);
81-
init();
67+
this(context, attrs, 0);
8268

8369
}
8470

8571
public WaveViewByBezier(Context context, AttributeSet attrs, int defStyleAttr) {
8672
super(context, attrs, defStyleAttr);
73+
init();
8774
}
8875

8976

9077
private void init() {
78+
mWaveAmplitude = dp2px(15);
9179
initPaint();
92-
93-
initAnimation();
80+
setOnClickListener(this);
9481

9582
}
9683

@@ -108,27 +95,61 @@ private void initPaint() {
10895

10996
}
11097

111-
11298
@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);
117102

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);
122105

123-
setOnClickListener(this);
106+
setMeasuredDimension(measureWidth(widthMode, width), measureHeight(heightMode, height));
107+
}
124108

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;
125126
}
126127

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+
127148
@Override
128149
protected void onDraw(Canvas canvas) {
129150
super.onDraw(canvas);
130151

131-
mWaveLength = getWidth();
152+
mWaveLength = mScreenWidth;
132153
mWavePath.reset();
133154

134155
// mWavePath.moveTo(-mWaveLength + mOffset, mCenterY);
@@ -141,21 +162,36 @@ protected void onDraw(Canvas canvas) {
141162
// mWavePath.lineTo(0, mScreenHeight);
142163
// mWavePath.close();
143164

144-
mWavePath.moveTo(mOffset, mCenterY);
165+
mWavePath.moveTo(-mWaveLength + mOffset, mWaveAmplitude);
145166

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());
152177
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();
153189
canvas.drawPath(mWavePath, mWavePaint);
154190
}
155191

156192

157193
private void initAnimation() {
158-
valueAnimator = ValueAnimator.ofInt(0, getWidth());
194+
valueAnimator = ValueAnimator.ofInt(0, mScreenWidth);
159195
valueAnimator.setDuration(3000);
160196
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
161197
valueAnimator.setInterpolator(new LinearInterpolator());
@@ -166,14 +202,13 @@ public void onAnimationUpdate(ValueAnimator animation) {
166202
postInvalidate();
167203
}
168204
});
169-
valueAnimator.start();
170205
}
171206

172207
public void startAnimation() {
173208
isStart = true;
174209
if (valueAnimator != null) {
175210

176-
valueAnimator.reverse();
211+
valueAnimator.start();
177212
}
178213
}
179214

@@ -184,6 +219,7 @@ public void stopAnimation() {
184219
this.clearAnimation();
185220
}
186221
}
222+
187223
public void pauseAnimation() {
188224
isStart = false;
189225
if (valueAnimator != null) {
@@ -195,17 +231,33 @@ public void resumeAnimation() {
195231
isStart = true;
196232
if (valueAnimator != null) {
197233
valueAnimator.resume();
198-
onAnimationStart();
199234
}
200235
}
201236

202237

203238
@Override
204239
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+
}
209249
}
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());
210262
}
211263
}
Collapse file

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

Copy file name to clipboard
+1-12Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
43
android:id="@+id/activity_wave_by_bezier"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
76
android:orientation="vertical"
8-
android:paddingBottom="@dimen/activity_vertical_margin"
9-
android:paddingLeft="@dimen/activity_horizontal_margin"
10-
android:paddingRight="@dimen/activity_horizontal_margin"
11-
android:paddingTop="@dimen/activity_vertical_margin"
12-
tools:context="com.allen.androidcustomview.activity.WaveByBezierActivity">
7+
android:paddingTop="100dp">
138

149

1510
<com.allen.androidcustomview.widget.WaveViewByBezier
1611
android:id="@+id/wave_bezier"
1712
android:layout_width="match_parent"
1813
android:layout_height="200dp" />
1914

20-
21-
<com.allen.androidcustomview.widget.WaveAnimatorView
22-
android:id="@+id/wave_animator"
23-
android:layout_width="match_parent"
24-
android:layout_height="200dp"
25-
android:layout_marginTop="50dp" />
2615
</LinearLayout>

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.