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
|
package cgeo.geocaching;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;
public class cgCompassMini extends View {
private int arrowSkin = R.drawable.compass_arrow_mini_white;
private Context context = null;
private Double cacheLat = null;
private Double cacheLon = null;
private Bitmap compassArrow = null;
private Double azimuth = Double.valueOf(0);
private Double heading = Double.valueOf(0);
private PaintFlagsDrawFilter setfil = null;
private PaintFlagsDrawFilter remfil = null;
public cgCompassMini(Context contextIn) {
super(contextIn);
context = contextIn;
}
public cgCompassMini(Context contextIn, AttributeSet attrs) {
super(contextIn, attrs);
context = contextIn;
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.cgCompassMini);
int usedSkin = attributes.getInt(R.styleable.cgCompassMini_skin, 0);
if (usedSkin == 1) arrowSkin = R.drawable.compass_arrow_mini_black;
else arrowSkin = R.drawable.compass_arrow_mini_white;
}
@Override
public void onAttachedToWindow() {
compassArrow = BitmapFactory.decodeResource(context.getResources(), arrowSkin);
setfil = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);
remfil = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
}
@Override
public void onDetachedFromWindow() {
if (compassArrow != null) {
compassArrow.recycle();
compassArrow = null;
}
}
public void setContent(Double cacheLatIn, Double cacheLonIn) {
cacheLat = cacheLatIn;
cacheLon = cacheLonIn;
}
protected void updateAzimuth(Double azimuthIn) {
azimuth = azimuthIn;
updateDirection();
}
protected void updateHeading(Double headingIn) {
heading = headingIn;
updateDirection();
}
protected void updateCoords(Double latitudeIn, Double longitudeIn) {
if (latitudeIn == null || longitudeIn == null || cacheLat == null || cacheLon == null) {
return;
}
heading = cgBase.getHeading(latitudeIn, longitudeIn, cacheLat, cacheLon);
updateDirection();
}
protected void updateDirection() {
if (compassArrow == null || compassArrow.isRecycled() == true) {
return;
}
// compass margins
int compassRoseWidth = compassArrow.getWidth();
int compassRoseHeight = compassArrow.getWidth();
int marginLeft = (getWidth() - compassRoseWidth) / 2;
int marginTop = (getHeight() - compassRoseHeight) / 2;
invalidate(marginLeft, marginTop, (marginLeft + compassRoseWidth), (marginTop + compassRoseHeight));
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Double azimuthRelative = azimuth - heading;
if (azimuthRelative < 0) {
azimuthRelative = azimuthRelative + 360;
} else if (azimuthRelative >= 360) {
azimuthRelative = azimuthRelative - 360;
}
// compass margins
canvas.setDrawFilter(setfil);
int marginLeft = 0;
int marginTop = 0;
int compassArrowWidth = compassArrow.getWidth();
int compassArrowHeight = compassArrow.getWidth();
int canvasCenterX = (compassArrowWidth / 2) + ((getWidth() - compassArrowWidth) / 2);
int canvasCenterY = (compassArrowHeight / 2) + ((getHeight() - compassArrowHeight) / 2);
marginLeft = (getWidth() - compassArrowWidth) / 2;
marginTop = (getHeight() - compassArrowHeight) / 2;
canvas.rotate(-(azimuthRelative.floatValue()), canvasCenterX, canvasCenterY);
canvas.drawBitmap(compassArrow, marginLeft, marginTop, null);
canvas.rotate(azimuthRelative.floatValue(), canvasCenterX, canvasCenterY);
canvas.setDrawFilter(remfil);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 21 + getPaddingLeft() + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = 21 + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
|