Android Density and ImageView - android

ImageViews do not seem to scale but buttons do when I apply the following code
var myDisplayMetrics = Resources.DisplayMetrics;
myDisplayMetrics.DensityDpi = DisplayMetricsDensity.Low;
myDisplayMetrics.SetTo(myDisplayMetrics);
This came about because I noticed the ImageButtons were larger on the screen than the pixels size delivered. I investigated this and found a way to calculated the DP.
On hardware which gives DisplayMetrics as density=1.5, width=1920, height=1200, scaledDensity=1.83, xdpi=225.777, ydpi=225.77701 and this apparently gives a DP of 1280 x 800 hence the unexpected size
I applied the code above and the ImageButton appeared at the right size however the animation in the ImageView was still too big..
What is the standard way of fixing this? I am using Xamarin.

Related

ScaleType center in Android appears to scale image

My device has a screen resolution of 600 x 800. I created a bitmap 600 x 600 and display it in an ImageView where ScaleType is set to "center". I also have my activity set to take up the entire screen. When the image is displayed, the image shows its correction ratio but appears larger than it should. The width appears larger with the edges cut off. According to the Android docs, setting the ScaleType to "center" is not suppose to cause any scaling. So why doesn't the image fit my width exactly?
Check out this answer.
You seem to have fallen in the When it will not work scenario described there.

variable screen resolution sprite not scaling unity

I am trying to make my 1st game in unity. I need to support different resolutions of android and iOS. The problem is when i place the background image for any one res ex. 1536x2048,It doesn't displays properly for 640x960. The screen size is varying, but sprites and bg sprite is not scaling as per res.
I am not clear how to acheive it. But, it must be a basic thing as there are so many games on android and all phones have different res. and dpi.
There should be some formula or we just need to set orthographic.size for this. I am using this :
// to calc size
Camera.main.orthographicSize = Screen.currentResolution.height / (2*70); //
Camera.main.orthographicSize = Screen.currentResolution.height / 2;
Debug.Log("Camera size :" + Camera.main.orthographicSize);
Debug.Log("Width : " + Screen.currentResolution.width + " height : " + Screen.currentResolution.height);
// this doesnt give me width and height as per resolution of screen for ex. for 1536x2048 -> i get, Width : 1366 height : 768
i found this as similar question
http://forum.unity3d.com/threads/orthographic-screen-size.113641/ but it didnt help, whereas problem is nearly same.
If somebody can share how to get correct screen resolution by code and how to set width of bg sprite and other sprite, it can solve the problem OR
if there is some setting which takes the default scene and scales automatically for various resolution. it will help.
Unity does not scale like that. Instead, we design assets to suit our needs. You are using a formula but i don't know what you expect from it.
here's some info: Orthographic size is height of view port. Taking your example, for height of 2048, camera size is : 2048/2/100=10.24. This will be maintained irrespective of device screen size. Only difference being it will be less detailed on smaller devices, kind of like a zoom out effect. Now for width,i'm not sure for portrait, but let's say 9:16 is the widest. So if you design the bg for this aspect ratio, the bg will be cropped on smaller devices, shown full on 9:16 devices. To design for this aspect ratio, your bg needs to be 9/16*10.24*100=576 px. (Again i'm not sure which aspect ratio is widest in portrait mode). Hope that helps

Match different screen size

my problem is quite simple, i never did this before but now i'm forced to search for a solution.
working with layout-normal
Let's say i have a image background set to match_parent, with 3 different points on it, on that points i should place buttons, i added the buttons and set some margin dp's till i meet the points. for example: layout_marginTop="50dp"
on the preview screen it looks ok, but when i start some emulators from the same size category "layout-normal"
3.7inch; 4.0inch; 4.65inch ... it looks different. it looks ok only on the device size i got the preview
What should i do? what can i do?
i tryed to fix the image, created a linear layout, set the image as background, set the size of the screen (let's say 400x250) and added the buttons in the layout.
but now when i run it in different emulators i get empty space and the image does not fit the entire screen.
Hrrrrrrr! what can i do?
Get your ImageView Size with a onPreDrawListener, or if it fill the Screen you can get it with getDisplaySize
You know the points in your image where to place your Buttons:
orginal image 480x800 -> button1 left:XX px top:xx px
new size 720x1280 -> button1 left: YY px top: YY px
with this info you can calculate where to place the Button on real device with an other Resolution than 480x800 (using the rule of three)
then set your margins programaticly
actualy i found out that android documentation gives you other posibilities in layouts arranging.
-- layout-normal; layout-small; layout-large; layout-xlarge
-- layout-sw300dp; layout-sw360dp; layout-sw450dp; layout-sw600dp;
layout-sw800dp; where sw means smallest available width required by your layout resources
-- layout-w300dp; layout-w360dp; layout-w450dp; layout-w600dp; layout-w800dp; where w means the minimum width required for the layout
-- layout-h200dp; layout-h260dp; ... where h means the minimum height required for the layout
i used to give them the smallest available width.

Why my 720 px image got distortion in my 720 px galaxy S3

My question is just in the title.
I try to put an image to my layout, i tried this with a FrameLayout's background with wrap_content width and height and also tried with imageView with all of the possible fitScale properties.
I just cant see why my 720 px width image is HALF on the width of my phone's screen which is a samsung galaxy s3 with 720 px width....
My real question:
What is the best way to ensure that my pics in applicaion wont get any distortion?
First of all, you need to think in screen densities and dip values, not pixels. That means that you are dealing with a "virtual screen" where 1 dip (density independent pixel) represents a different amount of real screen pixels depending on the device.
For drawables you have the following categories for getting crisp results on any screen:
ldpi (Scale factor 0.75)
mdpi (Default / baseline: scale factor 1.0)
hdpi (1.5)
xhdpi (2.0)
Create different sized versions of your bitmaps based on these scale factors to get crisp results on any screen. You should start from xhdpi as highest resolution to avoid quality loss because of upscaling.
Put these different versions into their respective drawable folders (res/drawable-ldpi, res/drawable-mdpi...). These different versions of one and the same bitmap must bear the same file name of course, otherwise that won't work.
Second of all you shouldn't make strong assumptions about the device screen height and width. You need to layout your views and graphics so they dynamically make use of the whole screen not knowing the exact screen resolution. However, you can make weak assumptions about these screen resolutions based on the configuration qualifiers
small
normal
large
xlarge
Try to avoid fixed sizes for your views based on any assumptions. Always avoid "px" values in layout XML files. Use "dip" instead.
Read the documentation Supporting Multiple Screens for more information.
What you want to achieve sounds as if you want your image to take up the full width of the screen.
If you set the image as a background of any view (FrameLayout for example) then the displayed clip of the image depends on the size of that view.
If that view has layout_width / layout_height set to wrap_content then the size of it depends on the dimensions and arrangement of its child view(s).
In case of FrameLayout it depends on the size of that single child of FrameLayout.
If you're using ImageView the image will be scaled to fit into the size of the ImageView by default. The size of the ImageView depends on its layout_width / layout_height values. If you set these values to match_parent and if the ImageView has enough room "to grow" you should be able to see your image stretch all over the screen.
The "room to grow" depends on the ImageView's context in the layout. Does it have neighbors that take up room? Is the parent view too small (because of wrap_content for example)? Look for these possible problems.

How do I get around clipping issues with my app in smaller phones?

I have a basic linear layout, with image view that display photos, textviews that show text etc...and when I test my whole app on my android 2, there is never any part of the screen that doesn't show. But when I test it in the QVGA emulator, half the view is off the screen. Now, some of my text is defined to a certain size.
I never use px (only dip) in any of my xml. I use fill_parent, wrap_content where it is needed and never fix a height or length. Could it be with my text, I use say, textSize = 22dip? I couldn't find documentation on why that would not work in all sizes.
Could it be that some apps need to be programmed to those screen sizes? Or am I missing something?
Thanks!
DIP actually doesn't help in fluid layout design. DIP tries to make sure things will looks the same size across different screen density. (imagine the concept of Point in typography)
Without your code and/or screenshot of what happen, it is hard to judge what happens to your program, but 22dip text size is possibly one of the reason which:
22dip text in HDPI will become: 22 * 240 / 160 = 33 pixel
22dip text in LDPI will become: 22 * 120 / 160 = 16.5 pixel
If your setting on QVGA is HDPI, then it is actually 33 pixel tall for a letter, which consumes more than 1/10 of the screen height.
By the way, your 16:9 wide screen (vertical) may fit everything in one screen without scroll, while QVGA is only 3:2 screen, even though everything is in proportion, something would be hidden away for sure.

Categories

Resources