Selaa lähdekoodia

增加WaveView

zhanglong7 5 vuotta sitten
vanhempi
commit
283a0289bb

+ 148 - 0
ipu-mobile-ui/src/main/java/com/ai/ipu/mobile/ui/comp/view/WaveView.java

@ -0,0 +1,148 @@
1
package com.ai.ipu.mobile.ui.comp.view;
2
3
import android.animation.ValueAnimator;
4
import android.content.Context;
5
import android.graphics.Canvas;
6
import android.graphics.Paint;
7
import android.graphics.Path;
8
import android.util.AttributeSet;
9
import android.view.View;
10
import android.view.animation.LinearInterpolator;
11
12
public class WaveView extends View {
13
    private static final long PERIOD = 1500;
14
    private Paint mPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
15
    private Paint mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
16
    private Paint mPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
17
    private Path mPath1 = new Path();
18
    private Path mPath2 = new Path();
19
    private Path mPath3 = new Path();
20
21
    private static final int DX = 20;
22
    private double omega;
23
    private int mWaveLength;
24
    private int mWaveHeight;
25
    private float offset;
26
    private ValueAnimator mValueAnimatior;
27
28
    public WaveView(Context context) {
29
        super(context);
30
    }
31
32
    public WaveView(Context context, AttributeSet attrs) {
33
        super(context, attrs);
34
    }
35
36
    public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
37
        super(context, attrs, defStyleAttr);
38
    }
39
40
    @Override
41
    public void onWindowFocusChanged(boolean hasWindowFocus) {
42
        super.onWindowFocusChanged(hasWindowFocus);
43
        if (hasWindowFocus) {
44
            if (mWaveLength == 0) {
45
                init();
46
            }
47
        }
48
    }
49
50
    @Override
51
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
52
        super.onLayout(changed, left, top, right, bottom);
53
        if (mWaveLength==0){
54
            init();
55
        }
56
    }
57
58
    private void init() {
59
        mWaveLength = getWidth();
60
        mWaveHeight = getHeight();
61
        if (mWaveLength != 0) {
62
            omega = Math.PI * 4 / mWaveLength;
63
        }
64
        mPaint1.setStyle(Paint.Style.STROKE);
65
        mPaint2.setStyle(Paint.Style.STROKE);
66
        mPaint3.setStyle(Paint.Style.STROKE);
67
        mPaint1.setColor(0xFF3F93F8);
68
        mPaint2.setColor(0xff999999);
69
        mPaint3.setColor(0xff888888);
70
    }
71
72
    @Override
73
    protected void onDraw(Canvas canvas) {
74
        computePaths();
75
        canvas.drawPath(mPath1, mPaint1);
76
        canvas.drawPath(mPath2, mPaint2);
77
        canvas.drawPath(mPath3, mPaint3);
78
    }
79
80
    private void computePaths() {
81
        mPath1.reset();
82
        mPath2.reset();
83
        mPath3.reset();
84
        float x, y;
85
        for (x=0; x<mWaveLength+DX; x+=DX) {
86
            y = ((float) Math.sin(omega*x + offset) + 1) * mWaveHeight / 2;
87
            mPath1.lineTo(x, y);
88
        }
89
90
        for (x=0; x<mWaveLength+DX; x+=DX) {
91
            y = ((float) Math.sin(omega*x + Math.PI + offset) + 1) * mWaveHeight / 2;
92
            mPath2.lineTo(x, y);
93
        }
94
95
        for (x=0; x<mWaveLength+DX; x+=DX) {
96
            y = ((float) Math.sin(omega*x + Math.PI/2 + offset) + 1) * mWaveHeight / 2;
97
            mPath3.lineTo(x, y);
98
        }
99
    }
100
101
    /**
102
     * 设置3根波浪线的颜色值
103
     * @param c1
104
     * @param c2
105
     * @param c3
106
     */
107
    public void setColors(int c1, int c2, int c3) {
108
        mPaint1.setColor(c1);
109
        mPaint2.setColor(c2);
110
        mPaint3.setColor(c3);
111
    }
112
113
    private void startAnimator() {
114
        mValueAnimatior = ValueAnimator.ofFloat(0, (float) Math.PI*2);
115
        mValueAnimatior.setDuration(PERIOD);
116
        mValueAnimatior.setInterpolator(new LinearInterpolator());
117
        mValueAnimatior.setRepeatCount(ValueAnimator.INFINITE);
118
        mValueAnimatior.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
119
            @Override
120
            public void onAnimationUpdate(ValueAnimator animation) {
121
                offset = (Float) animation.getAnimatedValue();
122
                invalidate();
123
            }
124
        });
125
        mValueAnimatior.start();
126
    }
127
128
    @Override
129
    protected void onAttachedToWindow() {
130
        super.onAttachedToWindow();
131
        startAnimator();
132
    }
133
134
    @Override
135
    protected void onDetachedFromWindow() {
136
        super.onDetachedFromWindow();
137
        stopAnimator();
138
    }
139
140
    /**
141
     * 停止动画
142
     */
143
    public void stopAnimator() {
144
        if(mValueAnimatior != null && mValueAnimatior.isRunning()) {
145
            mValueAnimatior.cancel();
146
        }
147
    }
148
}