Search code examples
javaandroidxmlbitmapdrawable

How to center and scale an overlayed drawable icon on a bitmap


I have a bitmap thumbnail for a map and I want to add the a map marker in the center but am struggling to do so. I was able to overlay the two bitmaps but the the one in front is significantly smaller and not centered. Is there a way to center the bitmap and scale it up? This is what it looks like:

enter image description here

I looked at this Android: How to overlay-a-bitmap/draw-over a bitmap? and it helped but the map marker is very small and off centered. Thanks


Solution

  • Update:

    I ended up solving it this way. I'll leave my solution for anyone who needs it in the future. Here it goes:

        public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp2, 500, 500, false);
        Bitmap bitmapWithOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bitmapWithOverlay);
    
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.drawBitmap(resizedBitmap, ((bmp1.getWidth()/2)-250), ((bmp1.getHeight()/2)-450), null);
    
        return bitmapWithOverlay;
    }
    

    and here is the process of getting the bitmap from a drawable

            Bitmap icon = BitmapFactory.decodeResource(getActivity().getBaseContext().getResources(),
                R.drawable.your_icon);