Draw a custom form in android using XML

I am adding a custom marker to the Android map view. here is the image i use for it. It seems like more markers are added, about 100 the whole application is slow. Here is the image I use for this.

enter image description here

Instead of using this image, I plan to draw this image as a form in XML. How to do it?

I also follow this guide to display a custom marker. Is this custom drawing delaying the application?

What is the best way to do this?

+5
source share
2 answers

Perhaps, but it does not seem to make sense. Because a Google map only needs a bitmap. Therefore, you need to create a bitmap and draw your shape using the canvas.

marker.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:width="20dp" android:height="20dp" android:top="10dp"> <rotate android:fromDegrees="45" android:pivotX="75%" android:pivotY="40%"> <shape> <solid android:color="#fe696d" /> </shape> </rotate> </item> <item android:width="@dimen/marker_width" android:height="@dimen/marker_height" android:bottom="8dp"> <shape> <solid android:color="#4b4b4b" /> <corners android:topLeftRadius="@dimen/marker_corner_radius" android:topRightRadius="4dp" /> </shape> </item> <item android:width="32dp" android:height="26dp" android:bottom="4dp" android:top="16dp"> <shape> <solid android:color="#fe696d" /> <corners android:bottomLeftRadius="@dimen/marker_corner_radius" android:bottomRightRadius="@dimen/marker_corner_radius" /> </shape> </item> 

dimen.xml

 <resources> <dimen name="marker_corner_radius">4dp</dimen> <dimen name="marker_height">40dp</dimen> <dimen name="marker_width">32dp</dimen> </resources> 

part of the code for converting a figure to a bitmap (from a fragment / activity using a map)

 @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.marker, null); Canvas canvas = new Canvas(); int width = getResources().getDimensionPixelOffset(R.dimen.marker_width); int height = getResources().getDimensionPixelOffset(R.dimen.marker_height); drawable.setBounds(0, 0, width, height); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); canvas.setBitmap(bitmap); drawable.draw(canvas); BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap); LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().icon(bitmapDescriptor).position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } 

the result will be as follows

enter image description here

+4
source

The easiest way is to simply add a small png or jpg marker. It will be faster to draw and does not take up so much space. I donโ€™t think you can get this form in xml - the small triangle below makes it difficult, the forms are really limited. You could do this in the xml part and the image part, but then why bother with xml?

+1
source

Source: https://habr.com/ru/post/1241081/


All Articles