Android Tutorial LoadBitmap / LoadBitmapResize / LoadBitmapSample

Discussion in 'Tutorials & Examples' started by Erel, Aug 11, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This tutorial explains the differences between the three methods.

    The bottom line is that the best option is to use LoadBitmapResize and set the container gravity, if possible, to Gravity.CENTER.

    LoadBitmap - Simply loads the bitmap as-is. Loading unknown bitmap files with LoadBitmap is considered a programming bug as it can easily lead to out of memory errors.
    The memory required to load a bitmap is not related to the file size but rather to the image size. It is approximately width * height * 4.

    LoadBitmapSample - Returns a bitmap that its size is equal or larger than the passed width and height values. This means that the container gravity should be set to FILL (this is the default value). Otherwise the image might be larger than the container.
    This also means that the aspect ratio will be lost.

    LoadBitmapResize - Returns a bitmap that its size equals to the passed width and height values. If the KeepAspectRatio parameter is set to True (in most cases it should be) then the aspect ratio will be kept. In that case the width or height (not both) might be smaller than the passed values. This is similar to B4i FIT content mode.
    The bitmap scale is set based on the device scale.

    Usage example:
    Code:
    'loading an icon that should be 32 x 32 pixels (the dip units are important!).
    Dim bmp As Bitmap = LoadBitmapResize(File.DirAssets, "someimage.png"32dip32dipTrue)
    The image file should be 64 x 64 pixels or more. The actual physical size will be more or less the same on all devices.

    View.SetBackgroundImage returns a BitmapDrawable object. This allows us to set the gravity to center and keep the bitmap aspect ratio:
    Code:
    Dim bg As Bitmap = LoadBitmapResize(File.DirAssets, "bg.png", ImageView1.Width, ImageView1.Height, True)
    ImageView1.SetBackgroundImage(bg).Gravity = 
    Gravity.CENTER
     
  2. LucaMs

    LucaMs Expert Licensed User

    What means this?

    I really don't understand the example. You load an image file whose ratio is 1:1 using LoadBitmapResize with 32dip x 32dip, same ratio, 1:1.
    What would be the purpose in the example?


    I tried another example, using an image file whose ratio is 1.5:1.
    Two ImageViews:
    1) ratio 1:1
    2) ratio 1.5:1

    1) used LoadBitmapResize(... ImageView1.Width, ImageView1.Height, True)
    2) used the simple LoadBitmap.

    Bad result for the first ImageView.


    I thought the result would be what you get using the routine created and published by Klaus in this thread. I think this is a feature that is still missing.
     
    Last edited: Aug 11, 2017
  3. Erel

    Erel Administrator Staff Member Licensed User

    The purpose is to have a 32dip x 32dip bitmap. This is not something that you can do with LoadBitmap.

    Have you set the ImageView gravity to CENTER?
     
    LucaMs likes this.
  4. LucaMs

    LucaMs Expert Licensed User

    And with LoadBitmapSample(... 32dip, 32dip)?

    No; and with this setting you get the same result of FitCenterBitmap (Klaus' routine). I mean it's perferct ;)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The size will be equal or larger:
    This means that you cannot set the gravity to center and you lose the aspect ratio if the container doesn't have the same aspect ratio as the bitmap.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice