wxr
2022-10-28 bab36f8002d92e7125dfc40023f566266e3fdb38
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.touch;
 
import android.graphics.RectF;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewParent;
 
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.charts.LineChart;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.manager.MappingManager;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.RectD;
 
public class GodTouchListener implements View.OnTouchListener {
 
    GestureDetector _GestureDetector;
    ScaleGestureDetector _ScaleGestureDetector;
    LineChart _LineChart;
 
    public GodTouchListener(LineChart lineChart) {
        _LineChart = lineChart;
        _GestureDetector = new GestureDetector(lineChart.getContext(), new GestureListener());
        _ScaleGestureDetector = new ScaleGestureDetector(lineChart.getContext(), new ScaleListener());
    }
 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
 
        ViewParent parent = v.getParent();
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
 
        boolean hit = _GestureDetector.onTouchEvent(event);
        hit |= _ScaleGestureDetector.onTouchEvent(event);
        if (hit) {
            _LineChart.invalidate();
            return true;
        }
 
        return false;
    }
 
 
    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
 
        float _lastSpanX, _lastSpanY;
 
        public ScaleListener() {
            super();
        }
 
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
 
            float spanX = detector.getCurrentSpanX();
            float kx = spanX / _lastSpanX;
 
            float spanY = detector.getCurrentSpanY();
            float ky = spanY / _lastSpanY;
 
            RectF godRect = _LineChart.get_GodRect();
            RectF mainRect = _LineChart.get_MainPlotRect();
 
            godRect.right = godRect.left + godRect.width() * kx;
            godRect.bottom = godRect.top + godRect.height() * ky;
 
            constrainRect(godRect, mainRect);
 
            nofityViewPortChanged(godRect);
 
            _lastSpanX = spanX;
            _lastSpanY = spanY;
 
            return true;
        }
 
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
 
            _lastSpanX = detector.getCurrentSpanX();
            _lastSpanY = detector.getCurrentSpanY();
 
            return true;
        }
 
        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            super.onScaleEnd(detector);
        }
    }
 
 
    RectD _rectD_ob = new RectD();
 
    class GestureListener extends GestureDetector.SimpleOnGestureListener {
 
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }
 
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
 
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return super.onFling(e1, e2, velocityX, velocityY);
        }
 
        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
        }
 
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            RectF godRect = _LineChart.get_GodRect();
            RectF mainRect = _LineChart.get_MainPlotRect();
 
 
            float w = godRect.width();
            float h = godRect.height();
            godRect.left += -distanceX;
            godRect.right = godRect.left + w;
            godRect.top += -distanceY;
            godRect.bottom = godRect.top + h;
 
            constrainRect(godRect, mainRect);
 
            nofityViewPortChanged(godRect);
 
            return true;
        }
 
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return super.onSingleTapConfirmed(e);
        }
    }
 
 
    private void nofityViewPortChanged(RectF godRect) {
 
        // 计算出godRect对应的viewport
        MappingManager mappingManager = _LineChart.get_MappingManager();
        double left = mappingManager.p2v_x(godRect.left);
        double right = mappingManager.p2v_x(godRect.right);
        double bottom = mappingManager.p2v_y(godRect.bottom);
        double top = mappingManager.p2v_y(godRect.top);
 
        _rectD_ob.left = left;
        _rectD_ob.top = top;
        _rectD_ob.right = right;
        _rectD_ob.bottom = bottom;
        _LineChart.notifyOB_ViewportChanged(_rectD_ob);
    }
 
 
    private void constrainRect(RectF godRect, RectF maxRect) {
        float w = godRect.width();
        float h = godRect.height();
 
        // 1. 保持比例
        if (godRect.left < maxRect.left) {
            godRect.left = maxRect.left;
            godRect.right = godRect.left + w;
        }
        if (godRect.top < maxRect.top) {
            godRect.top = maxRect.top;
            godRect.bottom = godRect.top + h;
        }
        if (godRect.right > maxRect.right) {
            godRect.right = maxRect.right;
            godRect.left = godRect.right - w;
        }
        if (godRect.bottom > maxRect.bottom) {
            godRect.bottom = maxRect.bottom;
            godRect.top = godRect.bottom - h;
        }
 
        // 2. 限定值的范围
        if (godRect.left < maxRect.left) {
            godRect.left = maxRect.left;
        }
        if (godRect.top < maxRect.top) {
            godRect.top = maxRect.top;
        }
        if (godRect.right > maxRect.right) {
            godRect.right = maxRect.right;
        }
        if (godRect.bottom > maxRect.bottom) {
            godRect.bottom = maxRect.bottom;
        }
    }
 
}