2

I'm trying to adjust 3 bitmaps to fill screen height (and width) with relative layout.

I've solved most of the problem but I still can't figure out why it works! I won't be able to relax until I do... so please try and help me understand the following:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    int screenHeight = displayMetrics.heightPixels;

    ImageView imgTop= (ImageView) findViewById(R.id.boardTop);
    ImageView imgMid= (ImageView) findViewById(R.id.boardMid);
    ImageView imgBot= (ImageView) findViewById(R.id.boardBot);

    top = BitmapFactory.decodeResource(getResources(), R.drawable.board_top);
    mid = BitmapFactory.decodeResource(getResources(), R.drawable.board_mid);
    bot = BitmapFactory.decodeResource(getResources(), R.drawable.board_bot);

    top = Bitmap.createScaledBitmap(top, top.getWidth(), (int) (screenHeight*0.2), false);
    mid = Bitmap.createScaledBitmap(mid, mid.getWidth(), (int) (screenHeight*0.6), false);
    bot = Bitmap.createScaledBitmap(bot, screenWidth, (int) (screenHeight*0.2), false);

    imgTop.setImageBitmap(top);
    imgMid.setImageBitmap(mid);
    imgBot.setImageBitmap(bot);

First I've used the code above and got that (I've hidden the pictures):

enter image description here

Then I've changed the 3 "createScaledBitmap" lines to the following:

    top = Bitmap.createScaledBitmap(top, screenWidth, (int) (screenHeight*0.2), false);
    mid = Bitmap.createScaledBitmap(mid, screenWidth, (int) (screenHeight*0.6), false);
    bot = Bitmap.createScaledBitmap(bot, screenWidth, (int) (screenHeight*0.2), false);

(I've changed them so that all of them recieved the screenWidth value).

After the change above, I've recieved this (I've given each bitmap a different color so it's easier to see):

enter image description here

So it somehow fixed the bitmaps to fill all the screen height, though it still didn't grant the bottom bitmap the full screen width (as it should because I've given it the same values as the 2 other bitmaps above- also in the XML file).

So how come this happened? I believe I don't know how those bitmap classes really work. Please help me understand thoroughly what happened here.

Thanks a lot!

Jacob.B
  • 771
  • 1
  • 9
  • 19
  • It might be to do with your original images. Have a look through this answer http://stackoverflow.com/questions/6278992/does-bitmap-createscaledbitmap-convert-an-32-bit-image-into-24-bit – John J Smith Aug 30 '14 at 22:53

0 Answers0