【问题标题】:Resizing a custom marker on Android maps在 Android 地图上调整自定义标记的大小
【发布时间】: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


    【解决方案1】:

    找到了在创建画布之前添加以下内容的解决方案。

    int newWidth = (int) (origBitmap.getWidth() * scalingFactor);
    int newHeight = (int) (origBitmap.getHeight() * scalingFactor);
    Bitmap bitmap = Bitmap.createScaledBitmap(origBitmap, newWidth, newHeight, true);
    

    这将相应地缩放位图。我可以使用相同的 scalingFactor 类似地缩放字体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多