Images for different resolutions

Johnmcenroy

Active Member
Licensed User
Longtime User
What is the best way to use different images for various resolutions ?

According to Google 90% of all Android are:

Small ldpi ~10% (240x320) 0.75 2"-3.5" 120dpi
Normal mdpi ~16% (320x480) 1.0 3.6"-4.9" 160dpi (Status Bar - 25px/dip , ICS Software Buttons - 48px/dip)
Normal hdpi ~37% (480x800) 1.5 3.6"-4.9" 240dpi
Normal xhdpi ~25% (720x1280...HD) 2.0 3.6"-4.9" 320dpi
Large xxhdpi (1080x1920) 3.0 5" 480dpi (Galaxy S4)

Total: ~90%

Thanks
 
Last edited:

Johnmcenroy

Active Member
Licensed User
Longtime User
Thanks Erel. But I don't understand how with aspect ratio on devices with various resolution , Because if i make image 720x1280 it will be distorted on 320x480. And also must count Status Bar (25px) on 160 DPI Device and 48px Software Buttons on 160 DPI Device on ICS with software buttons. In app that i make for now aspect ratio without distortion is extremely important for one image. How I inderstand I must make an images for most popular resolutions in two formats - For Device with software buttons and without. So there will be 10 images. But how to load images - based on what - aspect ratio or resolution ? And what is the best way to do it - designer script or code ?
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Thanks Erel. But I don't understand how with aspect ratio on devices with various resolution , Because if i make image 720x1280 it will be distorted on 320x480. And also must count Status Bar (25px) on 160 DPI Device and 48px Software Buttons on 160 DPI Device on ICS with software buttons. In app that i make for now aspect ratio without distortion is extremely important for one image. How I inderstand I must make an images for most popular resolutions in two formats - For Device with software buttons and without. So there will be 10 images. But how to load images - based on what - aspect ratio or resolution ? And what is the best way to do it - designer script or code ?

Try this code, slightly modified from this post to support bicubic scaling.

B4X:
Sub ResizeImage(original As Bitmap, TargetX As Int, TargetY As Int) As Bitmap
    Dim origRatio As Float = original.Width / original.Height
    Dim targetRatio As Float = TargetX / TargetY
    Dim scale As Float
    
    If targetRatio > origRatio Then
        scale = TargetY / original.Height
    Else
        scale = TargetX / original.Width
    End If
    
    Dim c As Canvas
    Dim b As Bitmap
    b.InitializeMutable(TargetX, TargetY)
    c.Initialize2(b)
    'set the background
    c.DrawColor(Colors.LightGray)
    Dim r As Rect
    Dim w = original.Width * scale, h = original.Height * scale As Int
    r.Initialize(TargetX / 2 - w / 2, TargetY / 2 - h / 2, TargetX / 2 + w / 2, TargetY / 2+ h / 2)
    Dim ExtDraw As ABExtDrawing
    Dim paint As ABPaint
    paint.Initialize()
    paint.setFilterBitmap(True)
    paint.SetAntiAlias(True)
    ExtDraw.drawBitmap(C, original, Null, R, paint)
    return b
End Sub
 
Last edited:
Upvote 0

Johnmcenroy

Active Member
Licensed User
Longtime User
Thank you very much Yttrium . I also found that android scale images saving aspect ratio. So If my image for 160 MDPI 1.0 is 320x365 image for 240 HDPI 1.5 must be 480x548 , 320 XHDPI 2.0 - 640x730 and for 480 XXHDPI 3.0 - 960x1095. So it will be automatically correctly scaled maintaining aspect ratio. But how I understand it works if you check screen density and load certain image that is made for certain density (LDPI,MDPI and so on ).
 
Last edited:
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Thank you very much Yttrium . I also found that android scale images saving aspect ratio. So If my image for 160 MDPI 1.0 is 320x365 image for 240 HDPI 1.5 must be 480x548 , 320 XHDPI 2.0 - 640x730 and for 480 XXHDPI 3.0 - 960x1095. So it will be automatically correctly scaled maintaining aspect ratio. But how I understand it works if you check screen density and load certain image that is made for certain density (LDPI,MDPI and so on ).

Yes, and this make sense for buttons and such that have the same size in each layout/resolution (in dip, not actual pixels of course), but with regards to a splash screen, density shouldn't affect it.
 
Upvote 0
Top