Draw a custom shape in android using XML

前端 未结 2 2068
误落风尘
误落风尘 2021-02-04 20:02

I am adding a custom marker to the android map view. here is the image I am using for it.It looks like as more markers are placed say about 100 the entire app is getting slow. H

2条回答
  •  后悔当初
    2021-02-04 20:24

    It's possible, but looks like does not make sense. Because Google Map requires only bitmap. So you need create bitmap and draw your shape with help of canvas.

    marker.xml

    
    
    
    
        
            
                
            
        
    
    
    
        
            
            
        
    
    
    
    
    
        
            
            
        
    
    
    

    dimen.xml

    
        4dp
        40dp
        32dp
    
    

    part of code to convert shape into bitmap (from fragment/activity with 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));
    }
    

    result would be next

提交回复
热议问题