hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.manager;
 
import android.graphics.RectF;
 
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.data.Entry;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.RectD;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.SingleD_XY;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.SingleF_XY;
 
 
/**
 * 数据源与绘出来的图之间的映射关系
 * -------------------
 * 人与照片直接的映射关系
 * <p>
 */
 
public class MappingManager {
 
    /**
     * 区域视图
     */
    RectF _contentRect;
 
    /**
     * 数据视图
     */
    RectD _maxViewPort;
    RectD _currentViewPort;
 
 
    /**
     * 数据视图的约束
     */
    float fatFactor = 1f;//肥胖因子
    RectD _constrainViewPort;
 
    int maxScaleX = 5;//最大放大倍数
 
    public MappingManager(RectF rectMain) {
        _contentRect = rectMain;
        _maxViewPort = new RectD();
        _currentViewPort = new RectD();
        _constrainViewPort = new RectD();
    }
 
    public void prepareRelation(double xMin, double xMax, double yMin, double yMax) {
 
        _maxViewPort.left = xMin;
        _maxViewPort.right = xMax;
        _maxViewPort.bottom = yMin;
        _maxViewPort.top = yMax;
 
        _currentViewPort.setRectD(_maxViewPort);
 
        setFatFactor(fatFactor);
    }
 
    /**
     * 根据像素位置变换成数值
     *
     * @param x
     * @param y
     * @return
     */
    public SingleD_XY getValueByPx(float x, float y) {
        SingleD_XY value = SingleD_XY.getInstance();
        value.setX(p2v_x(x)).setY(p2v_y(y));
        return value;
    }
 
    public SingleF_XY getPxByEntry(Entry entry) {
        return getPxByValue(entry.getX(), entry.getY());
    }
 
    /**
     * 根据数值变换成像素
     *
     * @param x
     * @param y
     * @return
     */
    public SingleF_XY getPxByValue(double x, double y) {
 
        SingleF_XY value = SingleF_XY.getInstance();
        value.setX(v2p_x(x)).setY(v2p_y(y));
        return value;
    }
 
    /**
     * x方向
     * ---------------
     * 数值转换成像素
     *
     * @param xValue
     * @return
     */
    public float v2p_x(double xValue) {
        double px = _contentRect.left + _contentRect.width() * (xValue - _currentViewPort.left) / _currentViewPort.width();
        return (float) px;
    }
 
    /**
     * y方向
     * ----------------
     * 数值转换成像素
     *
     * @param yValue
     * @return
     */
    public float v2p_y(double yValue) {
        double py = _contentRect.top + _contentRect.height() - _contentRect.height() * (yValue - _currentViewPort.bottom) / _currentViewPort.height();
        return (float) py;
    }
 
    /**
     * x方向
     * ---------------
     * 像素转换成数值
     *
     * @param xPix
     * @return
     */
    public double p2v_x(float xPix) {
        double value = xPix - _contentRect.left;
        value = value / _contentRect.width() * _currentViewPort.width();
        value = value + _currentViewPort.left;
        return value;
    }
 
    /**
     * y方向
     * --------------
     * 像素转换成数值
     *
     * @param yPix
     * @return
     */
    public double p2v_y(float yPix) {
        double value = yPix - (_contentRect.top + _contentRect.height());
        value = value / -_contentRect.height() * _currentViewPort.height();
        value = value + _currentViewPort.bottom;
        return value;
    }
 
 
    public void zoom(float scaleX, float scaleY, float cx, float cy) {
 
        double newWidth = scaleX * _currentViewPort.width();
        double newHeight = scaleY * _currentViewPort.height();
 
        double hitValueX = p2v_x(cx);
        double left = hitValueX - newWidth * (cx - _contentRect.left) / _contentRect.width();
 
        double hitValueY = p2v_y(cy);
        double bottom = hitValueY - newHeight * (_contentRect.bottom - cy) / _contentRect.height();
 
        if ((_maxViewPort.right - _maxViewPort.left) / newWidth> maxScaleX){
            //超过最大放大倍数
            return;
        }
 
        _currentViewPort.left = left;
        _currentViewPort.bottom = bottom;
        _currentViewPort.right = _currentViewPort.left + newWidth;
        _currentViewPort.top = _currentViewPort.bottom + newHeight;
 
        constrainViewPort(_currentViewPort);
    }
 
    public void zoom(float level, double oldStartW, double oldStartH, float cx, float cy, boolean canX, boolean canY) {
 
        double newWidth = canX ? oldStartW * level : oldStartW;
        double newHeight = canY ? oldStartH * level : oldStartH;
 
        double hitValueX = p2v_x(cx);
        double left = hitValueX - newWidth * (cx - _contentRect.left) / _contentRect.width();
 
        double hitValueY = p2v_y(cy);
        double bottom = hitValueY - newHeight * (_contentRect.bottom - cy) / _contentRect.height();
 
        if ((_maxViewPort.right - _maxViewPort.left) / newWidth> maxScaleX){
            //超过最大放大倍数
            return;
        }
 
        _currentViewPort.left = left;
        _currentViewPort.bottom = bottom;
        _currentViewPort.right = _currentViewPort.left + newWidth;
        _currentViewPort.top = _currentViewPort.bottom + newHeight;
 
        constrainViewPort(_currentViewPort);
    }
 
    /**
     * 根据移动的像素偏离---》计算出数据视图的偏离
     *
     * @param dx
     * @param dy
     */
    public void translate(float dx, float dy) {
 
        double w = _currentViewPort.width();
        double h = _currentViewPort.height();
 
        double ddx = w * dx / _contentRect.width();
        double ddy = h * dy / _contentRect.height();
 
        _currentViewPort.left += -ddx;
        _currentViewPort.bottom += ddy;
 
        // 约束 currentViewPort 的位置
        constrainViewPort(_currentViewPort, w, h);
 
        _currentViewPort.right = _currentViewPort.left + w;
        _currentViewPort.top = _currentViewPort.bottom + h;
    }
 
    /**
     * 约束当前的viewport
     * ---------------------
     * 针对平移的方式
     */
    private void constrainViewPort(RectD currentViewPort, double currentWidth, double currentHeight) {
 
        currentViewPort.left = Math.max(_constrainViewPort.left, Math.min(_constrainViewPort.right - currentWidth, currentViewPort.left));
        currentViewPort.bottom = Math.max(_constrainViewPort.bottom, Math.min(_constrainViewPort.top - currentHeight, currentViewPort.bottom));
    }
 
    /**
     * 约束当前的viewport
     * ---------------------
     * 针对缩放的方式
     *
     * @param currentViewPort
     */
    private void constrainViewPort(RectD currentViewPort) {
 
        currentViewPort.left = Math.max(_constrainViewPort.left, currentViewPort.left);
        currentViewPort.right = Math.min(_constrainViewPort.right, currentViewPort.right);
 
        currentViewPort.bottom = Math.max(_constrainViewPort.bottom, currentViewPort.bottom);
        currentViewPort.top = Math.min(_constrainViewPort.top, currentViewPort.top);
    }
 
    public RectD get_constrainViewPort() {
        return _constrainViewPort;
    }
 
    public void set_constrainViewPort(RectD _constrainViewPort) {
        this._constrainViewPort = _constrainViewPort;
    }
 
    public RectD get_maxViewPort() {
        return _maxViewPort;
    }
 
    public void set_maxViewPort(RectD _maxViewPort) {
        this._maxViewPort = _maxViewPort;
    }
 
    public RectD get_currentViewPort() {
        return _currentViewPort;
    }
 
    public void set_currentViewPort(RectD _currentViewPort) {
        this._currentViewPort = _currentViewPort;
    }
 
    public float getFatFactor() {
        return fatFactor;
    }
 
    /**
     * 设置约束视图的肥胖因子
     * -------------------
     * 和最大的数据视图做比较:
     * 1. 约束视图 是 最大视图的 1.1 倍,那么这个肥胖因子就是 1.1
     *
     * @param fatFactor
     */
    public void setFatFactor(float fatFactor) {
 
        if (fatFactor < 1) {
            throw new RuntimeException("肥胖因子必须大于1!");
        }
 
        this.fatFactor = fatFactor;
 
        double w = _maxViewPort.width();
        double h = _maxViewPort.height();
 
        fatFactor = fatFactor - 1;
 
        _constrainViewPort.left = _maxViewPort.left - w * fatFactor;
        _constrainViewPort.right = _maxViewPort.right + w * fatFactor;
        _constrainViewPort.top = _maxViewPort.top + h * fatFactor;
        _constrainViewPort.bottom = _maxViewPort.bottom - h * fatFactor;
    }
 
    public void setMaxScaleX(int maxScaleX) {
        this.maxScaleX = maxScaleX;
    }
 
}