wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
/*  Copyright (C) 2023-2024 Frank Ertl
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.withingssteelhr;
 
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
import nodomain.freeyourgadget.gadgetbridge.R;
 
public class RotaryControl extends View {
 
    public interface RotationListener {
        void onRotation(short movementAmount);
    }
 
    private Path circlePath;
 
    private int controlPointX;
    private int controlPointY;
 
    private int controlCenterX;
    private int controlCenterY;
    private int controlRadius;
 
    private int padding;
    private int controlPointSize;
    private int controlPointColor;
    private int lineColor;
    private int lineThickness;
    private double startAngle;
    private double angle ;
    private boolean isControlPointSelected = false;
    private Paint paint = new Paint();
    private Paint controlPointPaint = new Paint();
    private RotationListener rotationListener;
 
    public RotaryControl(Context context) {
        this(context, null);
    }
 
    public RotaryControl(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public RotaryControl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }
 
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotaryControl, defStyleAttr, 0);
 
        startAngle = a.getFloat(R.styleable.RotaryControl_start_angle, (float) Math.PI / 2);
        angle = startAngle;
        controlPointSize = a.getDimensionPixelSize(R.styleable.RotaryControl_controlpoint_size, 50);
        controlPointColor = a.getColor(R.styleable.RotaryControl_controlpoint_color, Color.GRAY);
        lineThickness = a.getDimensionPixelSize(R.styleable.RotaryControl_line_thickness, 20);
        lineColor = a.getColor(R.styleable.RotaryControl_line_color, Color.RED);
        calculateAndSetPadding();
        a.recycle();
    }
 
    private void calculateAndSetPadding() {
        int totalPadding = getPaddingLeft() + getPaddingRight() + getPaddingBottom() + getPaddingTop() + getPaddingEnd() + getPaddingStart();
        padding = totalPadding / 6;
    }
 
    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        int smallerDim = width > height ? height : width;
        int largestCenteredSquareLeft = (width - smallerDim) / 2;
        int largestCenteredSquareTop = (height - smallerDim) / 2;
        int largestCenteredSquareRight = largestCenteredSquareLeft + smallerDim;
        int largestCenteredSquareBottom = largestCenteredSquareTop + smallerDim;
        controlCenterX = largestCenteredSquareRight / 2 + (width - largestCenteredSquareRight) / 2;
        controlCenterY = largestCenteredSquareBottom / 2 + (height - largestCenteredSquareBottom) / 2;
        controlRadius = smallerDim / 2 - lineThickness / 2 - padding;
 
        super.onSizeChanged(width, height, oldWidth, oldHeight);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        drawRotationCircle(canvas);
        drawControlPoint(canvas);
 
    }
 
    private void drawControlPoint(Canvas canvas) {
        controlPointX = (int) (controlCenterX + controlRadius * Math.cos(angle));
        controlPointY = (int) (controlCenterY - controlRadius * Math.sin(angle));
        controlPointPaint.setColor(controlPointColor);
        controlPointPaint.setStyle(Paint.Style.FILL);
        controlPointPaint.setAlpha(128);
        Path controlPointPath = new Path();
        controlPointPath.addCircle(controlPointX, controlPointY, controlPointSize, Path.Direction.CW);
        canvas.drawPath(controlPointPath, controlPointPaint);
    }
 
    private void drawRotationCircle(Canvas canvas) {
        DashPathEffect dashPath = new DashPathEffect(new float[]{8,22}, (float)1.0);
        paint.setPathEffect(dashPath);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(lineThickness);
        paint.setAntiAlias(true);
        paint.setColor(lineColor);
        circlePath = new Path();
        circlePath.addCircle(controlCenterX, controlCenterY, controlRadius, Path.Direction.CW);
        canvas.drawPath(circlePath, paint);
    }
 
    private void updateRotationPosition(double touchX, double touchY) {
        double distanceX = touchX - controlCenterX;
        double distanceY = controlCenterY - touchY;
        double c = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
        double currentAngle = Math.acos(distanceX / c);
        if (distanceY < 0) {
            currentAngle = -currentAngle;
        }
 
        int movementAmount = (int) ((currentAngle - angle) * 100);
 
        int i = (int) movementAmount;
        if (movementAmount != 0) {
            if (Math.abs(movementAmount) > 15) {
                movementAmount /= movementAmount;
            }
 
            rotationListener.onRotation((short) -movementAmount);
        }
 
        angle = currentAngle;
    }
 
    public void setRotationListener(RotationListener listener) {
        rotationListener = listener;
    }
 
    public void reset() {
        angle = startAngle;
        invalidate();
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                double x = ev.getX();
                double y = ev.getY();
                if (x < controlPointX + controlPointSize && x > controlPointX - controlPointSize && y < controlPointY + controlPointSize && y > controlPointY - controlPointSize) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    isControlPointSelected = true;
                    updateRotationPosition(x, y);
                }
                break;
            }
 
            case MotionEvent.ACTION_MOVE: {
                if (isControlPointSelected) {
                    double x = ev.getX();
                    double y = ev.getY();
                    updateRotationPosition(x, y);
                }
                break;
            }
 
            case MotionEvent.ACTION_UP: {
                getParent().requestDisallowInterceptTouchEvent(false);
                isControlPointSelected = false;
                break;
            }
        }
 
        invalidate();
        return true;
    }
}