How to Resize a Bitmap in Android?

后端 未结 16 2324
有刺的猬
有刺的猬 2020-11-22 03:13

I have a bitmap taken of a Base64 String from my remote database, (encodedImage is the string representing the image with Base64):

profileImage          


        
16条回答
  •  萌比男神i
    2020-11-22 03:38

    Someone asked how to keep aspect ratio in this situation:

    Calculate the factor you are using for scaling and use it for both dimensions. Let´s say you want an image to be 20% of the screen in height

    int scaleToUse = 20; // this will be our percentage
    Bitmap bmp = BitmapFactory.decodeResource(
        context.getResources(), R.drawable.mypng);
    int sizeY = screenResolution.y * scaleToUse / 100;
    int sizeX = bmp.getWidth() * sizeY / bmp.getHeight();
    Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false);
    

    for getting the screen resolution you have this solution: Get screen dimensions in pixels

提交回复
热议问题