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@0c68f7e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .idea
    • misc.xml
  • app/src/main
    • java/com/allen/androidcustomview
      • MainActivity.java
      • widget
        • WaveAnimatorView.java
        • WaveViewByBezier.java
        • WaveViewBySinCos.java
    • res
      • layout
        • activity_main.xml
        • wave_layout.xml
      • values
        • colors.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/MainActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/MainActivity.java
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.allen.androidcustomview.bean.CircleBean;
1313
import com.allen.androidcustomview.utils.DisplayUtils;
1414
import com.allen.androidcustomview.widget.BubbleView;
15+
import com.allen.androidcustomview.widget.WaveViewByBezier;
1516

1617
import java.util.ArrayList;
1718
import java.util.List;
@@ -26,10 +27,12 @@ public class MainActivity extends AppCompatActivity {
2627

2728
private List<CircleBean> circleBeanList = new ArrayList<>();
2829

30+
private WaveViewByBezier waveView;
2931
@Override
3032
protected void onCreate(Bundle savedInstanceState) {
3133
super.onCreate(savedInstanceState);
3234
setContentView(R.layout.activity_main);
35+
waveView = (WaveViewByBezier) findViewById(R.id.wave);
3336
hxbIv = (ImageView) findViewById(R.id.hxb_iv);
3437
hxbTv = (TextView) findViewById(R.id.center_tv);
3538
bezierView = (BubbleView) findViewById(R.id.circle_view);
@@ -140,4 +143,18 @@ public int getStatusBarHeight() {
140143
return result;
141144
}
142145

146+
147+
@Override
148+
protected void onResume() {
149+
super.onResume();
150+
Log.d(TAG, "onResume: ");
151+
waveView.startAnimation();
152+
}
153+
154+
@Override
155+
protected void onPause() {
156+
super.onPause();
157+
Log.d(TAG, "onPause: ");
158+
waveView.stopAnimation();
159+
}
143160
}
Collapse file

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

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/widget/WaveAnimatorView.java
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import android.graphics.Path;
1010
import android.graphics.Shader;
1111
import android.util.AttributeSet;
12+
import android.util.Log;
1213
import android.view.View;
1314
import android.view.animation.LinearInterpolator;
1415

1516
import com.allen.androidcustomview.R;
1617

18+
import static android.content.ContentValues.TAG;
19+
1720
/**
1821
* Created by allen on 2016/12/13.
1922
* <p>
@@ -139,7 +142,7 @@ public WaveAnimatorView(Context context, AttributeSet attrs, int defStyleAttr) {
139142
private void getAttr(AttributeSet attrs) {
140143
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.WaveAnimatorView);
141144

142-
mWaveLength = typedArray.getDimensionPixelOffset(R.styleable.WaveAnimatorView_waveLength, mdefaultWaveLength);
145+
mWaveLength = typedArray.getDimensionPixelOffset(R.styleable.WaveAnimatorView_waveLength, dip2px(mContext, mdefaultWaveLength));
143146
mWaveDuration = typedArray.getInt(R.styleable.WaveAnimatorView_waveDuration, 2000);
144147
mWaveType = typedArray.getInt(R.styleable.WaveAnimatorView_waveType, SIN);
145148
mWaveAmplitude = typedArray.getDimensionPixelOffset(R.styleable.WaveAnimatorView_waveAmplitude, dip2px(mContext, 20));
@@ -184,7 +187,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
184187
mScreenWidth = w;
185188

186189
mWaveCount = (int) Math.round(mScreenWidth / mWaveLength + 1.5);
187-
190+
Log.d(TAG, "onSizeChanged: ======="+mWaveCount);
188191
mCenterY = mScreenHeight*3/4;
189192

190193
}
@@ -273,8 +276,8 @@ private void Animation() {
273276

274277
ValueAnimator waveAnimator = ValueAnimator.ofInt(0, mWaveLength);
275278
waveAnimator.setDuration(mWaveDuration);
276-
waveAnimator.setRepeatCount(ValueAnimator.INFINITE);
277279
waveAnimator.setInterpolator(new LinearInterpolator());
280+
waveAnimator.setRepeatCount(ValueAnimator.INFINITE);
278281
waveAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
279282
@Override
280283
public void onAnimationUpdate(ValueAnimator animation) {
Collapse file

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

Copy file name to clipboard
+179Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.allen.androidcustomview.widget;
2+
3+
import android.animation.ValueAnimator;
4+
import android.content.Context;
5+
import android.graphics.Canvas;
6+
import android.graphics.Color;
7+
import android.graphics.Paint;
8+
import android.graphics.Path;
9+
import android.util.AttributeSet;
10+
import android.util.Log;
11+
import android.view.View;
12+
import android.view.animation.LinearInterpolator;
13+
14+
import static android.content.ContentValues.TAG;
15+
16+
/**
17+
* Created by allen on 2016/12/13.
18+
* <p>
19+
* 波浪动画
20+
*/
21+
22+
public class WaveViewByBezier extends View implements View.OnClickListener {
23+
24+
/**
25+
* 屏幕高度
26+
*/
27+
private int mScreenHeight;
28+
/**
29+
* 屏幕宽度
30+
*/
31+
private int mScreenWidth;
32+
33+
/**
34+
* 波浪的画笔
35+
*/
36+
private Paint mWavePaint;
37+
/**
38+
* 一个周期波浪的长度
39+
*/
40+
private int mWaveLength = 1000;
41+
42+
43+
/**
44+
* 屏幕宽度内波浪显示几个周期
45+
*/
46+
private int mWaveCount;
47+
48+
/**
49+
* 波纹的中间轴(基准线)
50+
*/
51+
private int mCenterY;
52+
/**
53+
* 波浪的路径
54+
*/
55+
Path mWavePath;
56+
57+
/**
58+
* 平移偏移量
59+
*/
60+
private int mOffset;
61+
62+
/**
63+
* 振幅
64+
*/
65+
private int mWaveAmplitude = 25;
66+
67+
private ValueAnimator valueAnimator;
68+
69+
70+
public WaveViewByBezier(Context context) {
71+
super(context);
72+
init();
73+
74+
}
75+
76+
public WaveViewByBezier(Context context, AttributeSet attrs) {
77+
super(context, attrs);
78+
init();
79+
80+
}
81+
82+
public WaveViewByBezier(Context context, AttributeSet attrs, int defStyleAttr) {
83+
super(context, attrs, defStyleAttr);
84+
init();
85+
}
86+
87+
88+
private void init() {
89+
initPaint();
90+
91+
initAnimation();
92+
93+
}
94+
95+
96+
/**
97+
* 初始化画笔
98+
*/
99+
private void initPaint() {
100+
mWavePath = new Path();
101+
102+
mWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
103+
mWavePaint.setStrokeWidth(1);
104+
mWavePaint.setColor(Color.GRAY);
105+
mWavePaint.setStyle(Paint.Style.FILL_AND_STROKE);
106+
mWavePaint.setAntiAlias(true);
107+
108+
}
109+
110+
111+
@Override
112+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
113+
super.onSizeChanged(w, h, oldw, oldh);
114+
mScreenHeight = h;
115+
mScreenWidth = w;
116+
117+
mWaveCount = (int) Math.round(mScreenWidth / mWaveLength + 1.5);
118+
Log.d(TAG, "onSizeChanged: =======" + mWaveCount);
119+
mCenterY = mScreenHeight * 3 / 4;
120+
121+
setOnClickListener(this);
122+
123+
}
124+
125+
@Override
126+
protected void onDraw(Canvas canvas) {
127+
super.onDraw(canvas);
128+
129+
mWavePath.reset();
130+
131+
mWavePath.moveTo(-mWaveLength + mOffset, mCenterY);
132+
133+
for (int i = 0; i < mWaveCount; i++) {
134+
mWavePath.quadTo(-mWaveLength * 3 / 4 + i * mWaveLength + mOffset, mCenterY + 60, -mWaveLength / 2 + i * mWaveLength + mOffset, mCenterY);
135+
mWavePath.quadTo(-mWaveLength / 4 + i * mWaveLength + mOffset, mCenterY - 60, i * mWaveLength + mOffset, mCenterY);
136+
}
137+
mWavePath.lineTo(mScreenWidth, mScreenHeight);
138+
mWavePath.lineTo(0, mScreenHeight);
139+
mWavePath.close();
140+
canvas.drawPath(mWavePath, mWavePaint);
141+
}
142+
143+
144+
private void initAnimation() {
145+
valueAnimator = ValueAnimator.ofInt(0, mWaveLength);
146+
valueAnimator.setDuration(3000);
147+
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
148+
valueAnimator.setInterpolator(new LinearInterpolator());
149+
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
150+
@Override
151+
public void onAnimationUpdate(ValueAnimator animation) {
152+
mOffset = (int) animation.getAnimatedValue();
153+
postInvalidate();
154+
}
155+
});
156+
}
157+
158+
public void startAnimation() {
159+
if (valueAnimator != null) {
160+
mOffset=0;
161+
162+
valueAnimator.start();
163+
}
164+
}
165+
166+
public void stopAnimation() {
167+
if (valueAnimator != null) {
168+
mOffset=0;
169+
valueAnimator.end();
170+
this.clearAnimation();
171+
}
172+
}
173+
174+
175+
@Override
176+
public void onClick(View v) {
177+
initAnimation();
178+
}
179+
}

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.