【发布时间】:2015-12-18 00:15:59
【问题描述】:
我对 Android 还很陌生,所以如果我遗漏了什么,请原谅我。
我有以下代码,可在地图上显示自定义标记。这个自定义标记上也有一些文本。当文本较长时,它可以很好地调整标记的大小。
我最初用于自定义标记的代码是,
private Bitmap drawTextToBitmap(final int mResId, final String mText) {
Resources resources = getResources();
// Get the screen's density scale
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, mResId);
Bitmap.Config bitmapConfig = bitmap.getConfig();
if ( bitmapConfig == null ) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// Set font color
paint.setColor(Color.WHITE);
// Set font size and scale it
paint.setTextSize((int) (14 * scale));
// Set shadow width
paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);
// Set anti-aliasing
paint.setAntiAlias(true);
// Make font bold
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = ((bitmap.getHeight() + bounds.height())/2)-25;
canvas.drawText(mText, x, y, paint);
return bitmap;
}
如何在不丢失任何分辨率的情况下调整此位图的大小并相应地增加字体大小?不幸的是,由于许可/许可问题,我无法提供屏幕截图。
【问题讨论】:
标签: android bitmap google-maps-markers android-canvas google-maps-android-api-2