day0ops
day0ops

Reputation: 7482

Resizing a custom marker on Android maps

Im quite new to Android so forgive me if I have missed something.

I've got the following code which displays a custom marker on maps. This custom marker also has some text on it. It works well up to the point where I want to resize the marker by a certain amount when the text is longer.

The code I originally had for the custom marker was,

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

How can I resize this bitmap and also increase the font size accordingly without loosing any resolution ? Unfortunately I cant give screenshots due to licensing/permission issues.

Upvotes: 1

Views: 1683

Answers (1)

day0ops
day0ops

Reputation: 7482

Found the solution which was to add the following before creating the canvas.

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

This will scale the bitmap accordingly. I can similarly scale the font using the same scalingFactor.

Upvotes: 0

Related Questions