Android Question Scale.GetScale - where I'm wrong?

LucaMs

Expert
Licensed User
Longtime User
B4X:
       x = 0
    y = 0
    For n = 0 To mQta - 1
        Dim imv As ImageView
        imv.Initialize("")
        imv.Bitmap = LoadBitmap(File.DirAssets, "Image72.png")
        ' Panel1 is part of layout, so it was scaled before, using Scale.ScaleAll(Activity, True)
        Panel1.AddView(imv, x, y, 85dip * Scale.GetScaleX , 85dip * Scale.GetScaleY)
        x = x + imv.Width
        If Panel1.Width < x + imv.Width Then
            x = 0
            y = y + imv.Height
        End If
    Next


The images overlap partially vertically. How so?
 

LucaMs

Expert
Licensed User
Longtime User
I changed a few things and run a test on an emulator.
Now, the images are too far apart horizontally.
I put some comments in the code with the features of the emulator and more.

Thank you, Klaus
 

Attachments

  • lm imv test.zip
    19 KB · Views: 195
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to add this line in the loop:
imv.Gravity = Gravity.FILL
In the designer the default gravity of an ImageView is Gravity.FILL but not when you add one in the code.
You should also change this line:
Panel1.AddView(imv, x, y, 85dip * Scale.GetScaleX , 85dip * Scale.GetScaleX)
You should use the same scale value for width and height otherwise the images will not remain square because of the difference of the X and Y scales.
Instead of multiplying each time with the scale in the loop you might define one variable before the loop and use this one in the loop for both, width and height.
Your layout variants are 320 x 533 x 1 and you declare them as 480 x 800 x 1.5 in the code ?
For my tests I changed the layout variants to 480 x 800 x 1.5 to be coherent with your code.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You need to add this line in the loop:
imv.Gravity = Gravity.FILL
In the designer the default gravity of an ImageView is Gravity.FILL but not when you add one in the code.
You should also change this line:
Panel1.AddView(imv, x, y, 85dip * Scale.GetScaleX , 85dip * Scale.GetScaleX)
You should use the same scale value for width and height otherwise the images will not remain square because of the difference of the X and Y scales.
Instead of multiplying each time with the scale in the loop you might define one variable before the loop and use this one in the loop for both, width and height.
Your layout variants are 320 x 533 x 1 and you declare them as 480 x 800 x 1.5 in the code ?
For my tests I changed the layout variants to 480 x 800 x 1.5 to be coherent with your code.



Gravitiy.Fill: yes, in the designer I can choose Center and so I must insert that Line, thanks ... the images seem strangely "filled".

I have full confidence in your experience and in your module, but I need to understand (the fish ;)).

For example, if the ratio between the width and height of my layout was 1:3, 200W x 600H x1 and my view was 100W x 200H x1 (1:2) using ScaleAll or ScaleView my view would not keep the relationship 1:2? It seems to me that this happens and that my layouts are correct, after ScaleAll (Activity, True). So I thought I should just use ScaleView in the same way on each view added by code.
Now, you said i had to use a same var for both dimensions, because the view is squared, but if it was a rectangle?

You wrote:
Your layout variants are 320 x 533 x 1 and you declare them as 480 x 800 x 1.5 in the code ?
For my tests I changed the layout variants to 480 x 800 x 1.5 to be coherent with your code.


Don't ask me why :) but...
I had trouble uploading my old layout (480x800) in the designer. More precisely, loading them and then using SetReferenceLayout (480, 800, 1.5) and ScaleAll I did not get the result I was hoping for.

I added a variant, for each old layout, indicating 480x800-1.5 but allowing the designer to normalize it and he did it to 320x533-1 (and I deleted the old 480... variant). Then, in the code I used SetReferenceLayout (480, 800, 1.5) and ScaleAll and everything seems ok.

[UPDATED] Not, my layouts are "adapted" ma the views has not the same ratio on all devices.
I'm doing it all wrong. I am launching survival white flag; I surrender


I thank you very much for your interest and for your answers, Klaus

P.S. I need an "on screen" ruler :D
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Gravitiy.Fill: yes, in the designer I can choose Center and so I must insert that Line,
You can also set CENTER in the code, I suggested FILL to show that the images are positioned in the right place.

Scale Module:
For example, if the ratio between the width and height of my layout was 1:3, 200W x 600H x1 and my view was 100W x 200H x1 (1:2) using ScaleAll or ScaleView my view would not keep the relationship 1:2?
You are comparing the wrong parameters.
If you set SetReferenceLayout and using ScaleAll or ScaleView every view is streched according to the horizontal and the vertical screen ratios. The same happens if you use the standard layout size and ScaleRate = 1.
As the height / width ratios of different devices are not the same ScaleX and ScaleY may not be the same !
In these cases the aspect ratio of all the views will be changed, but for ImageViews ScaleX is used internally for both directions to maintain the aspect ratio.
If you have an ImageView and you stretch it with two different scale ratios the image may be distorted.
That's the reason why I suggested you to use the same scale factor, but only for the images.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You can also set CENTER in the code, I suggested FILL to show that the images are positioned in the right place.

Scale Module:
You are comparing the wrong parameters.
If you set SetReferenceLayout and using ScaleAll or ScaleView every view is streched according to the horizontal and the vertical screen ratios. The same happens if you use the standard layout size and ScaleRate = 1.
As the height / width ratios of different devices are not the same ScaleX and ScaleY may not be the same !
In these cases the aspect ratio of all the views will be changed, but for ImageViews ScaleX is used internally for both directions to maintain the aspect ratio.
If you have an ImageView and you stretch it with two different scale ratios the image may be distorted.
That's the reason why I suggested you to use the same scale factor, but only for the images.


First of all, I thank you for your patience.

With regard to the "center", I expressed myself badly (as you know, the language).
I wanted to say that you were right to remind me the two options of the designer, Fill, Center, and then I have to set one of them also in the code. Even in the designer, I "always" put to Fill.

I can not seem to get what I want because of the following reasons (I'm pretty much thinking as I write, sorry):

1) my poor english
2) I do not associate with the right terms
3) ... etc

I wrote a very long text and then ... I deleted it.

I will not give you any more trouble. I'll study the situation well in a quieter time (for me).

I think that I will use everything in percentages, if I can not make better use of your module.

Thanks again for your help, Klaus
 
Upvote 0
Top