-1

I have a png image inside my Drawable folder. The same image I use it in many markers but in different sizes. Instead of creating many images with different sizes, I wanted to create XMLs inside the Drawable folder reusing the orignial image.

I tried solutions on this answers: Scale a drawable resource in XML (Android) but they are not working in my cases. When I reach

BitmapDescriptor scaledIcon = BitmapDescriptorFactory.fromResource(R.drawable.scaled_marker_image); //scaled_marker_image is the XML that resize the original image
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Marker").icon(scaledIcon));

Basically in the MarjerOptions().icon(scaledIcon) I get nullPointerException

Any ideas how to achieve this?

Community
  • 1
  • 1
IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34
  • I'm kind of guessing here (that's why I'm adding as a comment), but the answer is that: "you can't do it". That's because the BitmapDescriptor, is trying to find a `BitmapDrawable` on the supplied resource ID to extract the Bitmap from, but instead found a `ScaleDrawable`. So for the map option you really must use a different method. – Budius Aug 17 '15 at 13:47
  • and creating a Bitmap from that resource and then using BitmapDescriptorFactory.fromBitmap? – IIRed-DeathII Aug 17 '15 at 13:52

1 Answers1

0

XML would look like this (as shown in Scale a drawable resource in XML (Android) answer)

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/marker_image"
    android:insetTop="30dp"
    android:insetLeft="10dp"
    android:insetRight="10dp"
    android:insetBottom="0dp"
    />

We define a method to transform a Drawable to Bitmap

public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

And then, to set the drawable to the marker:

Drawable drawable = getResources().getDrawable(getResources().getDrawable(R.drawable.mi_posicion_marker2));
BitmapDescriptor scaledIcon = BitmapDescriptorFactory.fromBitmap(drawableToBitmap(drawable));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Marker").icon(scaledIcon));
Community
  • 1
  • 1
IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34