Simple question :
When you import a picture, does android studio automatically scale it proportionally to the right device or what do I have to do to achieve this?
I think the scaling for each device size is done in the layout. Images are generally automatically scaled to the size of the imageView (depending on how you set the imageView), so it's the size of the imageView that you should be concerned about. If you make your layouts relatively, then it they will scale automatically by device size.
Is this what you are looking for?
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
ViewGroup.LayoutParams p = yourImage.getLayoutParams();
p.height = height*500/1040;
p.width = width*300/600;
yourImage.setLayoutParams(p);
You need something like this. I get the ratios I need everything to be from my personal device. That is, my screen is 600x1040 and the image looks good there at 300x500.
Related
I am developing an application in android studio which is displayed correctly on 1080x1920 phones(and lower) but for phones like mine(s8) the app gets messed up and i can't find a way to fix the app's ui for every screen size. So, i was wondering if there is a way to restrict the app's screen size to 1080x1920(with let's say black bars at the top & bottom of the screen) even if a phone's dimensions are bigger than that(this is automatically done in games that are not compatible with s8). Do you guys know a way of achieving this effect?
It's possible, you can set your parent View to exact size given the aspect ratio you want (1080x1920 scales down to 9x16). For this you'd have to programmatically call the View (or create it in code) and set width and height in pixels with LayoutParams:
yourView.setLayoutParams(new LayoutParams(width, height));
for the weight and height you'd want the actual screen dimensions, which you can get from the WindowManager:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int x = point.x;
int y = point.y;
Here you get the x and y, which hold your actual screen size (in pixels). Then, you decide on which one should be reduced (in order to maintain 9x16 aspect ratio) and set your parent View's LayoutParams as mentioned above.
Finally, your set your content View as your parent View:
setContentView(yourView);
That's about it. Hope that helps.
EDIT: Since you've mentioned games, I can tell you that there (if your use Android SDK) you have your SurfaceView as a parent View, the procedure is same.
As seen in this image:
I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens:
Is the screen size different from the getDefaultDisplay() method? How do I fix this?
I think your shape is seen by the system as a 9patch. It use the first and last lines/columns in order to know how to resize the image in order to display it consistently.
That's why there is a 2px difference in width and height. I think you can correct it by modifying the png and removing the first and last lines/columns, or just use another way to draw the image. Maybe using another format (jpg ? bmp ?) for the image could do the job. I'm not sure.
you can do that with calculation to fit all screen devices based on width and height of the device screen:
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen
I have a game where enemies are spawned from a library at random. Their size and speed are set to work on specific dimensions. However, these specific standards won't work well on phones with small screens. Is there a way to change speed, size depending on a phone's dimensions?
you can get Screen size with following code:
if (android.os.Build.VERSION.SDK_INT >= 13)
{
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
}
more info, and change any thing with this information
For detecting the environment, check out the System Capabilities class (screenResolution, etc)
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabilities.html
and for Flash settings, stageWidth and stageHeight may help:
(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html).
However, if you're targeting both small and large screens, you may want to think about two (or more) sets of FLA files, since you can't set stage size on-the-fly, and otherwise you'd be scaling graphics, bitmaps, etc. to fit on either.
You can use Config Constants (ActionScript settings) to signal to the code which environment you're compiling.
I have a RelativeLayout that has a number of controls in it. On some screens, there might not be enough room for this layout as well as the other screen elements. In this case, I'd like to use an alternate layout that excludes some controls and has less padding.
How can I implement this in a performant manner? The smaller layout should only be loaded if the larger layout definitely won't fit. I don't want to use the various screen size qualifiers because whether the layout will fit is a function of the width and height of the screen and the additional visible controls and that seems like it will get too complicated.
I Personally think you should use the new screen size qualifiers. It's how Android is designed and I think you should follow those rules. It will very probably also make the whole development process a lot easier!
But if you insist on doing it yourself, the only way I can think of to "hardcode" it, would be something like this:
Step 1: obtain the screen size
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
or if you're targeting a lower API level:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Step 2: pick an alternative layout depending on the screen size
if(width < value_x_small && height < value_y_small){
setContentView(R.layout.main_small);
} else if(width > value_x_small && height > value_y_small){
setContentView(R.layout.main_large);
} else {
setContentView(R.layout.main_larged);
}
This code allows you to select your own resources depending on the screen size in px. Its probably better to convert the px values to DP (DPI) and use those to make your decisions.You might also want to add a check on the current screen orientation.
You can implement your controls as separate Fragments and position them depending on screen size. Here is more info about Fragments and their Design Philosophy .