Android自定义地图标记,画布绘图和图像大小调整

时间:2014-03-19 22:48:55

标签: android canvas google-maps-markers

我尝试创建一个自定义标记,如下图所示,如果可能的话,可以使用轮廓或阴影。内部的矩形表示动态图像。

enter image description here

无论如何,我已经掌握了基础知识,但我无法弄清楚如何更改图像的大小,因为我只能定义左上角的偏移点,然后画布的其余部分被填充将图像缩小到右下角,遮盖背景。我也不知道如何创建指向下方的三角形,我尝试旋转画布,绘制一个矩形并将其向后旋转(参见第二个代码片段),但这并不起作用,因为它不会旋转它的中心。有任何想法吗?我也是这样做的#34;正确"?这是构建自定义标记的正确方法还是太慢/不是最佳?

    //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();

1 个答案:

答案 0 :(得分:0)

尝试使用以下示例代码调整图片大小,这对我有用。

// 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);            
}