Skip to content

Commit

Permalink
Flip own icon
Browse files Browse the repository at this point in the history
  • Loading branch information
sh123 committed Jul 6, 2023
1 parent 6c37008 commit e6a0509
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class MapActivity extends AppCompatActivity {
private boolean _rotateMap = false;
private boolean _shouldFollowLocation = false;

private String _info;
private String _positionInfo;
private double _prevBearing = 0.0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -66,11 +67,6 @@ protected void onCreate(Bundle savedInstanceState) {

Context context = getApplicationContext();
Configuration.getInstance().setUserAgentValue(Aprs.APRS_ID + " " + BuildConfig.VERSION_NAME);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

// my symbol
AprsSymbolTable aprsSymbolTable = AprsSymbolTable.getInstance(context);
String mySymbolCode = sharedPreferences.getString(PreferenceKeys.APRS_SYMBOL, "/[");

// map
_mapView = findViewById(R.id.map);
Expand Down Expand Up @@ -100,20 +96,29 @@ public void onSensorChanged(SensorEvent sensorEvent) {
@Override
public void onLocationChanged(Location location, IMyLocationProvider source) {
super.onLocationChanged(location, source);
_info = String.format(Locale.US, "%dkm/h, %d°, %dm, %s, %f, %f",
_positionInfo = String.format(Locale.US, "%dkm/h, %d°, %dm, %s, %f, %f",
UnitTools.metersPerSecondToKilometersPerHour((int)location.getSpeed()),
(int)location.getBearing(),
(int)location.getAltitude(),
UnitTools.decimalToMaidenhead(location.getLatitude(), location.getLongitude()),
location.getLatitude(),
location.getLongitude()
);

double currentBearing = location.getBearing();

if (_prevBearing > 180 && currentBearing <= 180)
updateMyIcon(false);
else if (_prevBearing <= 180 && currentBearing > 180)
updateMyIcon(true);

_prevBearing = currentBearing;
}

@Override
public void draw(Canvas pCanvas, MapView pMapView, boolean pShadow) {
super.draw(pCanvas, pMapView, pShadow);
if (_info == null || !_shouldFollowLocation) return;
if (_positionInfo == null || !_shouldFollowLocation) return;

// create paint
Paint paint = new Paint();
Expand All @@ -122,7 +127,7 @@ public void draw(Canvas pCanvas, MapView pMapView, boolean pShadow) {

// query bounds from text
Rect bounds = new Rect();
paint.getTextBounds(_info, 0, _info.length(), bounds);
paint.getTextBounds(_positionInfo, 0, _positionInfo.length(), bounds);

// draw background
paint.setColor(Color.WHITE);
Expand All @@ -137,17 +142,12 @@ public void draw(Canvas pCanvas, MapView pMapView, boolean pShadow) {
paint.setColor(Color.BLACK);
paint.setAlpha(255);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
pCanvas.drawText(_info, pCanvas.getWidth() - bounds.width(), bounds.height(), paint);
pCanvas.drawText(_positionInfo, pCanvas.getWidth() - bounds.width(), bounds.height(), paint);
}
};
Bitmap myBitmapIcon = aprsSymbolTable.bitmapFromSymbol(mySymbolCode, true);
if (AprsSymbolTable.needsRotation(mySymbolCode)) {
Matrix matrix = new Matrix();
matrix.postRotate(-90);
myBitmapIcon = Bitmap.createBitmap(myBitmapIcon, 0, 0, myBitmapIcon.getWidth(), myBitmapIcon.getHeight(), matrix, true);
}
_myLocationNewOverlay.setDirectionIcon(myBitmapIcon);
_myLocationNewOverlay.setPersonIcon(myBitmapIcon);

// set own icon
updateMyIcon(false);

// my location overlay
_myLocationNewOverlay.enableMyLocation();
Expand All @@ -161,6 +161,28 @@ public void draw(Canvas pCanvas, MapView pMapView, boolean pShadow) {
_mapStations = new MapStations(context, _mapView, this);
}

public void updateMyIcon(boolean shouldFlip) {
Context context = getApplicationContext();
Configuration.getInstance().setUserAgentValue(Aprs.APRS_ID + " " + BuildConfig.VERSION_NAME);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

// my symbol
AprsSymbolTable aprsSymbolTable = AprsSymbolTable.getInstance(context);
String mySymbolCode = sharedPreferences.getString(PreferenceKeys.APRS_SYMBOL, "/[");

Bitmap myBitmapIcon = aprsSymbolTable.bitmapFromSymbol(mySymbolCode, true);
if (AprsSymbolTable.needsRotation(mySymbolCode)) {
Matrix matrix = new Matrix();
matrix.postRotate(-90);
if (shouldFlip) {
matrix.postScale(-1, 1, myBitmapIcon.getWidth() / 2f, myBitmapIcon.getHeight() / 2f);
}
myBitmapIcon = Bitmap.createBitmap(myBitmapIcon, 0, 0, myBitmapIcon.getWidth(), myBitmapIcon.getHeight(), matrix, true);
}
_myLocationNewOverlay.setDirectionIcon(myBitmapIcon);
_myLocationNewOverlay.setPersonIcon(myBitmapIcon);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.map_menu, menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static BitmapDrawable drawLabel(Context context, String text, float textS
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);

Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);

Canvas canvas = new Canvas(bitmap);
Expand Down

0 comments on commit e6a0509

Please sign in to comment.