Skip to content

Commit e3598a7

Browse files
committed
Vector location icon
1 parent aad8dca commit e3598a7

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

app/src/main/java/org/nitri/opentopo/MapFragment.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
240240
mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(getActivity()),
241241
mMapView);
242242

243-
Bitmap bmMapLocation = BitmapFactory.decodeResource(getResources(),
244-
R.drawable.ic_map_location);
245-
246-
Bitmap bmMapBearing = BitmapFactory.decodeResource(getResources(),
247-
R.drawable.ic_map_bearing);
248-
243+
Bitmap bmMapLocation = Util.getBitmapFromDrawable(requireActivity(), R.drawable.ic_position, 204);
244+
if (bmMapLocation == null) {
245+
bmMapLocation = BitmapFactory.decodeResource(getResources(),
246+
R.drawable.ic_map_location);
247+
}
249248
mLocationOverlay.setPersonIcon(bmMapLocation);
250249
mLocationOverlay.setPersonHotspot(bmMapLocation.getWidth() / 2f, bmMapLocation.getHeight() / 2f);
251250

251+
Bitmap bmMapBearing = Util.getBitmapFromDrawable(requireActivity(), R.drawable.ic_direction, 204);
252+
if (bmMapBearing == null) {
253+
bmMapBearing = BitmapFactory.decodeResource(getResources(),
254+
R.drawable.ic_map_bearing);
255+
}
252256
mLocationOverlay.setDirectionArrow(bmMapLocation, bmMapBearing);
253257

254258
mScaleBarOverlay = new ScaleBarOverlay(mMapView);

app/src/main/java/org/nitri/opentopo/Util.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
import android.content.Context;
44
import android.content.res.Resources;
5+
import android.graphics.Bitmap;
6+
import android.graphics.Canvas;
7+
import android.graphics.drawable.BitmapDrawable;
8+
import android.graphics.drawable.Drawable;
9+
import android.graphics.drawable.ScaleDrawable;
10+
import android.graphics.drawable.VectorDrawable;
511
import android.os.Build;
612
import androidx.annotation.AttrRes;
713
import androidx.annotation.ColorInt;
14+
import androidx.annotation.DrawableRes;
15+
import androidx.appcompat.content.res.AppCompatResources;
816
import androidx.core.content.ContextCompat;
17+
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
18+
919
import android.text.Html;
1020
import android.text.Spanned;
1121
import android.text.TextUtils;
@@ -231,4 +241,32 @@ public static double elevationFromNmea(String nmea) {
231241
return NO_ELEVATION_VALUE;
232242
}
233243

244+
/**
245+
* Bitmap from vector drawable
246+
*
247+
* @param context
248+
* @param drawableId
249+
* @return
250+
*/
251+
public static Bitmap getBitmapFromDrawable(Context context, @DrawableRes int drawableId, int alpha) {
252+
Drawable drawable = AppCompatResources.getDrawable(context, drawableId);
253+
drawable.setAlpha(alpha);
254+
if (drawable instanceof BitmapDrawable) {
255+
return ((BitmapDrawable) drawable).getBitmap();
256+
} else if (drawable instanceof VectorDrawableCompat) {
257+
return createBitmap(drawable);
258+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) {
259+
return createBitmap(drawable);
260+
} else {
261+
throw new IllegalArgumentException("unsupported drawable type");
262+
}
263+
}
264+
265+
private static Bitmap createBitmap(Drawable drawable) {
266+
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
267+
Canvas canvas = new Canvas(bitmap);
268+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
269+
drawable.draw(canvas);
270+
return bitmap;
271+
}
234272
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<vector android:height="44dp" android:viewportHeight="234.47806"
2+
android:viewportWidth="128.0909" android:width="24.036362dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#ffffff"
4+
android:pathData="M0.545,117.217a63.5,63.5 0,1 0,127 0a63.5,63.5 0,1 0,-127 0z"
5+
android:strokeColor="#000000" android:strokeLineJoin="miter" android:strokeWidth="1.090909"/>
6+
<path android:fillColor="#2962ff"
7+
android:pathData="M64.045,117.217m-52.917,0a52.917,52.917 0,1 1,105.833 0a52.917,52.917 0,1 1,-105.833 0"
8+
android:strokeColor="#000000" android:strokeLineJoin="miter" android:strokeWidth="1.11111104"/>
9+
<path android:fillColor="#2962ff"
10+
android:pathData="m21.712,53.717c42.333,-10.583 42.333,-10.583 42.333,-10.583l42.333,10.583 -42.333,-52.917z"
11+
android:strokeColor="#000000" android:strokeLineCap="butt"
12+
android:strokeLineJoin="miter" android:strokeWidth="1"/>
13+
<path android:fillAlpha="0" android:fillColor="#2962ff"
14+
android:pathData="M21.712,233.634C64.045,223.05 64.045,223.05 64.045,223.05l42.333,10.583 -42.333,-52.917z"
15+
android:strokeAlpha="0" android:strokeColor="#000000"
16+
android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="1"/>
17+
</vector>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<vector android:height="44dp" android:viewportHeight="234.47806"
2+
android:viewportWidth="128.09091" android:width="24.036364dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#ffffff"
4+
android:pathData="M0.545,117.217a63.5,63.5 0,1 0,127 0a63.5,63.5 0,1 0,-127 0z"
5+
android:strokeColor="#000000" android:strokeLineJoin="miter" android:strokeWidth="1.09090912"/>
6+
<path android:fillAlpha="0" android:fillColor="#2962ff"
7+
android:pathData="m21.712,233.634c42.333,-10.583 42.333,-10.583 42.333,-10.583l42.333,10.583 -42.333,-52.917z"
8+
android:strokeAlpha="0" android:strokeColor="#000000"
9+
android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="1"/>
10+
<path android:fillAlpha="0" android:fillColor="#2962ff"
11+
android:pathData="m21.712,53.717c42.333,-10.583 42.333,-10.583 42.333,-10.583l42.333,10.583 -42.333,-52.917z"
12+
android:strokeAlpha="0" android:strokeColor="#000000"
13+
android:strokeLineCap="butt" android:strokeLineJoin="miter" android:strokeWidth="1"/>
14+
<path android:fillColor="#2962ff"
15+
android:pathData="M64.045,117.217m-52.917,0.012a52.917,52.917 134.987,1 1,105.833 -0.023a52.917,52.917 134.987,1 1,-105.833 0.023"
16+
android:strokeColor="#000000" android:strokeLineJoin="miter" android:strokeWidth="1.11111116"/>
17+
</vector>

0 commit comments

Comments
 (0)