Search code examples
androidbitmapscale

How can I scale a Bitmap in Android while maintaining the proportions?


I want to draw a human body using a Bitmap for each body part (because I will apply different color filters and effects to each part).

So I drew a body in Photoshop, divided it into parts and saved each one into a separate file with background transparent and all of them with the same dimensions. Therefore if i draw one by one in a view over each other the final result should be the whole body as it was before cutting the parts out.

But I do not know how to manage the possible scaling of the view that the layout could make to maintaining the relative position of each body part and achieve a perfect representation of a human body.


Solution

  • If you want to scale a Bitmap you can use this function:

            Bitmap scaledBmp = Bitmap.createScaledBitmap(originalBmp, int finalWidth, int finalHeight, false);
    

    This function will scale "originalBmp" to finalWidth and finalHeight. If you want to keep the propotions replace finalWidth with "originalBmp.getWidth()*0.8", that would make the bitmap 20% smaller (also do it with the Height).