MarkerWithText.java 文件源码

java
阅读 21 收藏 0 点赞 0 评论 0

项目:Tower-develop 作者:
/**
 * 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);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.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;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号