BunnyEngine'
BunnyEngine'

Reputation: 47

Android custom map marker, canvas drawing and image sizing

I'm trying to create a custom marker like in the image below, either with outline or drop-shadow if possible. The rectangle inside represents a dynamic image.

enter image description here

Anyway, I got the basics down, but I can't figure out how to change the image's size, because I can only define the top left offset point and then the rest of the canvas gets filled with the image down to the bottom right edge, covering up the background. I also have no idea how to create the triangle pointing down, I tried rotating the canvas, drawing a rectangle and rotating it back (see second code snippet), but that doesn't work because it doesn't rotate around its center. Any ideas? Also am I doing this "properly"? Is this the right way to build custom markers or is it too slow / not optimal?

    //IMAGE MARKER - red background with image on top
    LatLng vogel3 = new LatLng(myLat+0.0005,myLong+0.0005);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap bmp = Bitmap.createBitmap(40, 40, conf);
    Canvas canvas = new Canvas(bmp);

    Paint color = new Paint();
    color.setColor(Color.RED);

    canvas.drawRect(0, 0, 40, 40, color);
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
        R.drawable.spatz_adult), 3, 5, color);

    //canvas1.drawText("bird", 30, 40, color);

    //add marker to Map
    map.addMarker(new MarkerOptions().position(vogel3)
        .icon(BitmapDescriptorFactory.fromBitmap(bmp))
        .title("custom marker")
        .snippet("eeeeeeeeeep")
      //.anchor(0.5f, 1)
    );

~

//trying to draw triangle
canvas.save();
canvas.rotate(45);        
canvas.drawRect(0, 0, 40, 40, color);
canvas.restore();

Upvotes: 4

Views: 2295

Answers (1)

Yuvraj Kakkar
Yuvraj Kakkar

Reputation: 1163

Try following sample code to resize image , It worked for me.

// RESIZE IMAGE
public Bitmap resizeBitmap(String imagePath,int targetW, int targetH) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = 1;
    if ((targetW > 0) || (targetH > 0)) {
        scaleFactor = Math.min(photoW/targetW, photoH/targetH);        
    }

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    return BitmapFactory.decodeFile(imagePath, bmOptions);            
}

Upvotes: 0

Related Questions