Finding the right size of image/image view

hackhack

Active Member
Licensed User
Longtime User
I'd like to figure out how to add graphics so they look right.


Say I have a picture on the PC which is 1768x1420 and it looks normal then.

How would I go about finding the right size for an imageview to keep the picture looking right and not get distorted?

Yeah, I suppose I could try to do it by looking at it, but it would be nice if there was a way to calculate it.
 

Widget

Well-Known Member
Licensed User
Longtime User
Have you tried accessing:

ImageView1.bitmap.width and ImageView1.bitmap.height?

ImageView1.Width = ImageView1.Bitmap.Width * ScaleFact
ImageView1.Height = ImageView1.Bitmap.Height * ScaleFact

Where ScaleFact is the scaling factor you'll need for large images.

You can of course set ImageView1.Gravity=Gravity.NoGravity so it doesn't resize the image for small images.

Widget
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Have you tried accessing:

ImageView1.bitmap.width and ImageView1.bitmap.height?

Eh no, but wouldn't it just be the same size as in the designer?

ImageView1.Width = ImageView1.Bitmap.Width * ScaleFact
ImageView1.Height = ImageView1.Bitmap.Height * ScaleFact

Where ScaleFact is the scaling factor you'll need for large images.

Its more the image i need scaling, but how do i find the factor?
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Try this:

New_Height = Old_Height * New_Width / Old_Width

New_Width = Old_Width * New_Height / Old_Height
 
Last edited:
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Guess I can't explain myself well enough.

I'm trying to design a layout with imageview which shouldn't change size later, so i have to try and find a size which will make the pictures look like on the PC.
When i say calculate i mean on a calculator manually when its designed, not when its running.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
i mean on a calculator manually when its designed, not when its running.

???

You can use the Formulas with your calculator, too.


If you know the original Height and Width of the Picture and want it display on your phone with a given height, type

Original_Width * Height_Phone / Original_Height

in your calculator, to get Width_Phone.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I'd like to figure out how to add graphics so they look right. Say I have a picture on the PC which is 1768x1420 and it looks normal then. How would I go about finding the right size for an imageview to keep the picture looking right and not get distorted?

Have you tried reducing the picture on your PC down to a size which will fit on an Android device? If you do that, not only will you see what the dimensions will be, but you'll see how the picture looks shrunk down that much. For example, to reduce it to fit on a phone with 480x320 resolution, the picture may be so small that you may lose all the detail and even picture clarity.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Guess I can't explain myself well enough.

I'm trying to design a layout with imageview which shouldn't change size later, so i have to try and find a size which will make the pictures look like on the PC.
When i say calculate i mean on a calculator manually when its designed, not when its running.

Are you looking for the aspect ratio? i.e. Width/Height
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Its more the image i need scaling, but how do i find the factor?

You can load the image into a Bitmap and use Bitmap.Height and Bitmap.Width to get the original dimensions of the image, then assign that to the ImageView.

B4X:
dim bm as bitmap, Wid, Ht as Int
bm.initialize(dir,filename)     'If bitmap is large, then use InitializeSample()
Wid = bm.width
Ht   = bm.height

if bm.width > ImageView1.Width or bm.height > ImageView1.Height then
  ImageView1.Width   =   ' Scale size of ImageView1 using Wid and Ht
  ImageView1.Height  =   ' Scale size of ImageView1 using Wid and Ht
  ImageView1.Gravity = Gravity.Fill
else
  ImageView1.Gravity = Gravity.NoFill    'Bitmap fits inside the ImageView
  ImageView1.WIdth  = Wid
  ImageView1.Height = Ht
end if

ImageView1.SetBackgroundImage(bm)

This might give you a few ideas (use MaxImageWid and MaxImageHt instead of ImageView1.Width). Be careful loading images that are too large. I recommend using InitializeSample but of course that requires you specify max dimensions. I don't know if this keeps the aspect ratio.

Widget
 
Upvote 0
Top