-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInclinometerView.java
More file actions
125 lines (97 loc) · 3.84 KB
/
InclinometerView.java
File metadata and controls
125 lines (97 loc) · 3.84 KB
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
package io.github.iso53.nothingcompass.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.View;
import com.google.android.material.R;
import com.google.android.material.color.MaterialColors;
public class InclinometerView extends View {
private final int colorPrimary;
private final int colorSecondary;
private final Paint paint;
private float cx, cy;
private float bubbleX, bubbleY;
private float ringRadius;
private float bubbleRadius;
private boolean inCenter;
private static final float SMOOTH = 0.75f;
private static final float CENTER_THRESHOLD = 0.01f; // 1% of ring
private static final int VIBRATION_DURATION_MS = 15; // Slightly longer for center hit
private boolean wasInCenter = false;
private boolean isActive = true; // Default to true as it's the initial view
private boolean isHapticFeedbackEnabled = true;
public InclinometerView(Context c, AttributeSet a) {
super(c, a);
colorPrimary = MaterialColors.getColor(this, androidx.appcompat.R.attr.colorPrimary);
colorSecondary = MaterialColors.getColor(this, R.attr.colorSecondary);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setDither(true);
paint.setColor(colorSecondary);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(dp(1.5f));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
cx = w * 0.5f;
cy = h * 0.5f;
ringRadius = Math.min(w, h) * 0.45f;
bubbleRadius = ringRadius * 0.12f;
bubbleX = cx;
bubbleY = cy;
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(inCenter ? colorPrimary : colorSecondary);
// paint the outer ring
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(cx, cy, ringRadius, paint);
// paint lines
float half = ringRadius * 0.5f;
canvas.drawLine(cx, cy - half, cx, cy + half, paint);
canvas.drawLine(cx - half, cy, cx + half, cy, paint);
// paint the moving circle
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(bubbleX, bubbleY, bubbleRadius, paint);
}
public void updateTilt(float pitch, float roll) {
// Clamp so bubble never leaves circle
float len = (float) Math.sqrt(roll * roll + pitch * pitch);
if (len > 1f) {
roll /= len;
pitch /= len;
}
float targetX = cx + roll * ringRadius;
float targetY = cy + pitch * ringRadius;
// Proper smoothing toward target
bubbleX += (targetX - bubbleX) * SMOOTH;
bubbleY += (targetY - bubbleY) * SMOOTH;
float distFromCenter = (float) Math.hypot(bubbleX - cx, bubbleY - cy);
inCenter = distFromCenter < ringRadius * CENTER_THRESHOLD;
if (isActive && inCenter && !wasInCenter && isHapticFeedbackEnabled) {
performHapticFeedback();
}
wasInCenter = inCenter;
invalidate();
}
public void setIsActive(boolean active) {
this.isActive = active;
}
public void setHapticFeedbackEnabled(boolean enabled) {
this.isHapticFeedbackEnabled = enabled;
}
private void performHapticFeedback() {
Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
vibrator.vibrate(VibrationEffect.createOneShot(
VIBRATION_DURATION_MS,
VibrationEffect.DEFAULT_AMPLITUDE));
}
}
private float dp(float v) {
return v * getResources().getDisplayMetrics().density;
}
}