百度地图-marker 标 内容

效果:
这里写图片描述
动态变化,而不是直接使用图片;

public class MarkerWithText {

private static final int RECT_PADDING = 6;

public static Bitmap getMarkerWithText(int color, String text, Context context) {
    return drawTextToBitmap(context, R.drawable.ic_marker_white, color, text);
}

public static Bitmap getMarkerWithText(int color, String text,int bitmapResId, Context context) {
    return drawTextToBitmap(context, bitmapResId, color, text);
}

/**
 * Copied from:
 * http://stackoverflow.com/questions/18335642/how-to-draw-text-
 * in-default-marker-of-google-map-v2?lq=1
 */
private static Bitmap drawTextToBitmap(Context gContext, int gResId, int color, String gText) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }
    bitmap = bitmap.copy(bitmapConfig, true);

    // copy bitmap to canvas, replace white with colour
    Paint paint = new Paint();
    paint.setColorFilter(new LightingColorFilter(0x000000, color));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);
    paint.setTextSize((int) (15 * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) * 5 / 12; // At 5/12 from
                                                                // the top
                                                                // so it
                                                                // stays on
                                                                // the
                                                                // center

    canvas.drawText(gText, x, y, paint);

    return bitmap;
}

public static Bitmap getMarkerWithTextAndDetail(int gResId, String text, String detail,
        Resources res) {
    return drawTextAndDetailToBitmap(res, gResId, text, detail);
}

/**
 * Based on:
 * http://stackoverflow.com/questions/18335642/how-to-draw-text-in-
 * default-marker-of-google-map-v2?lq=1
 */
private static Bitmap drawTextAndDetailToBitmap(Resources resources, int gResId, String gText,
        String gDetail) {
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }
    bitmap = bitmap.copy(bitmapConfig, true);

    // copy bitmap to canvas, replace white with colour
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);
    paint.setTextSize((int) (10 * scale));
    paint.setFakeBoldText(true);
    paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    bounds.offsetTo(-6, bounds.height() / 2);

    // paint and bounds for details
    Paint dpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    dpaint.setColor(Color.WHITE);
    dpaint.setTextSize((int) (10 * scale));
    paint.setFakeBoldText(true);
    dpaint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

    Rect dbounds;

    if (gDetail != null) {
        dbounds = new Rect();
        dpaint.getTextBounds(gDetail, 0, gDetail.length(), dbounds);
        dbounds.offsetTo(0, bounds.bottom + 2);
    } else {
        dbounds = new Rect(0, 0, 0, 0);
    }

    // include text and detail bounds
    Rect brect = new Rect(bounds);

    brect.union(dbounds);

    // position and inflate w/ padding
    int x = (bitmap.getWidth() - brect.width()) / 2;
    int y = bounds.top + (bitmap.getHeight() - brect.height()) / 2;
    brect.offsetTo(x, y - (bounds.height()));
    brect.set(brect.left - RECT_PADDING, brect.top - RECT_PADDING, brect.right + RECT_PADDING,
            brect.bottom + RECT_PADDING);

    // draw text
    x = (bitmap.getWidth() - bounds.width()) / 2;
    y = bounds.top + (bitmap.getHeight() - (bounds.height() + dbounds.height())) / 2 +(int) (1 * scale);
    canvas.drawText(gText, x, y, paint);

    if (gDetail != null) {
        // draw detail
        x = (bitmap.getWidth() - dbounds.width()) / 2;
        y = y + bounds.height() + 2;
        canvas.drawText(gDetail, x, y, dpaint);
    }

    return bitmap;
}

}

最后,BitmapDescriptorFactory.fromBitmap()赋值给MarkerOptions.icon();
参考:http://stackoverflow.com/questions/18335642/how-to-draw-text-in-default-marker-of-google-map-v2?lq=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值