How to prepare android screen independent graphic - specific solution - android

Is it possible to use common hdpi folder for all screen densities? I have quite a lot images. If I make specific copies to folders drawable-hdpi, drawable-ldpi, drawable-xhdpi, ... but it takes huge data (backgrounds, bitmaps).
Is it possible to set only one drawable folder for all devices and then rescale according to a specific device programmatically?
I think about this code to get display size of the scree:
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
Then I will get display density of the device, something like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
density = metrics.density; // 1 - 1,5 - 2 .....
The I will recalculate size of imageview with density:
ImageView logo = (ImageView)findViewById(R.id.logo);
LinearLayout.LayoutParams logo1 = (LinearLayout.LayoutParams) logo.getLayoutParams();
logo1.width = (int)(logo.getWidth()*density);
logo1.height = (int)(logo.getHeight()*density);
logo1.leftMargin=(int)(logo1.leftMargin*density); // for margin
logo1.topMargin=(int)(logo1.topMargin*density); // for margin
logo1.rightMargin=(int)(logo1.rightMargin*density); // for margin
logo1.bottomMargin=(int)(logo1.bottomMargin*density); // for margin
My main problem is I need to have all proportions of graphic same on all devices. It means I must recalculate imageViews accroding to the screen size.
Is this a right way to get density independent screen? How does android work on other devices if only hdpi folder contains files. Does it take files from this folder? Can I set one common drawable folder to all densities?

I would strongly (strongly) advise against doing this. However, if you want the system to rescale your image assets, design them for mdpi (the baseline density) and put them in drawable/.
That said, you need at least mdpi and hdpi to get reasonable scaling (since hdpi is 1.5x mdpi, scaling algorithms produce worse results than for the other conversions from mdpi).
Make sure you've read and understood Providing Resources and Supporting Multiple Screens before you start dealing with resources.
P.S. The layout solution is wrong for a few reasons (e.g., setting margins instead of size) but it's also the completely wrong thing to do. Don't do it!

Related

am I supposed to make images larger for tablets, or same size as handset?

I am trying to wrap my head around the idea of scaling images which is all new to me. I finally understand for icon launcher you want something like 48x48 for mdpi and 72x72 for hdpi. For this, I have no problem using google's provided tool. I have an image I downloaded from the net that is 178x283 pixels, 96 dpi. When my (4inch 480x800hdpi) phone emulator views this, the image is pretty large, taking about half the screen. On my (10inch1280x800mdpi)phone, it looks smaller, actually the exact same as the original. This is not what I want right? how do I want the images to be scaled? do I want them the same size as the original, or smaller on a small phone and bigger on a big phone. I assume after this step I create the correct qualifiers and do the math done in this thread to resize the images ? Supporting multiple screens on Android. I forgot to mention what does android OS do automatically, because I figured if I have one image in drawable, then it will automatically make the smaller phone have a small image and the bigger phone have a big image, but that is not happening(given that I don't hard code values).
EDIT: decided to use this http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html
EDIT2: I have gotten the images and layouts how I want them now, but my 10 inch won't use the correct layout folder. I have this
layout
layout-land
layout-sw720dp
layout-sw720dp-land
it only works when i name them layout-xlarge, which is deprecated, what gives
edit:is it because sw qualifier is only for 3.3+ and I am targeting 2.3+?
Do you want the images to scale? Or do you want them to stay a particular size.
If you want them to scale, you can use something like this in your layout
android:adjustViewBounds="true"
Then set your size however you want with either layout_width or maxWidth (and length respectively).
Then scale how you want to:
android:scaleType="centerInside"
If the images are pixelated, then you need to add larger images for each screen size under your res folder.
Create a different layout for 10 inches tablet and another for 7 inches tablet then use this code below:
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
final int height = dm.heightPixels;
final int width = dm.widthPixels;
if ((height == 1280) && (width == 800)) {
setContentView(R.layout.10inchLayout);
}else if ((heig
ht == 1024) && (width == 600)) {
setContentView(R.layout.7inchLayout);
}
else {
setContentView(R.layout.PhoneinchLayout);
}
Hope it helps you.

How to resize the bitmap image according to mdpi,hdpi and screen size of the mobile

I am using image say 85x85 px (putting this image in drawable-mdpi)
and if i am displaying this image of 85x85 px on [320x480]mdpi screen size device it looks good,
but while displaying this image on [480x800]mdpi device it looks very small.
I wants to know how can i resize this image of (85x85 px) so that it works fine for the device having screen width and height of 480x800, mdpi.
You have to set the image size in the xml in dp. This post is really helpful.
Consider MDPI image(let say 85x85) as baseline Create images as follow
FOLDER ImageSize Percentage
LDPI 64x64 75% of baseline
MDPI 85x85 100% BASELINE
HDPI 127x127 150% of baseline
XHDPI 170x170 200% of baseline
The better solution for this is to produce LDPI,MDPI,HDPI,XHDPI,XXHDPI sized images and place it inside the corresponding folders.
Put the image in /drawable, and define the size of the image in your XML as 85dp, it will then be scaled for LDPI/HDPI/XHDPI/XXHDPI and god forbid XXXHDPI.
However, the better solution would to produce HDPI(128px)/XHDPI(170px)/XXHDPI(255px) version of the graphics and put them in /drawable-hdpi, /drawable-xhdpi, /drawable-xxhdpi respectively. This way you can provide the best experience for your users.
[Edit]
"dp" is Android's way of defining physical size of an UI objects, so a button of 48x48dp will have roughly the same physical dimension even when run on screen sizes between 240x320 to 1080x1920, for more information check Android developer site's Supporting Multiple Screens.
[Edit 2]
For images on canvas you can use this to calculate the scaled size of the image:
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int newSize = (int)(85 * (dm.densityDpi / 160f));

Showing pictures in Android app - Resolution?

I've a Photo gallery in my app. The set of pictures are stored in the drawable folder. I'm making use of ViewPager when the user swipes through the images.. What is the width and height in pixels and dps for xhdpi, hdpi, mdpi??
I know that the Android documentation says px = dp*dpi/160.. but I'm still confused about what should be the pictures download pixels so that it fits in all screen sizes??
There isn't one magic size that guarantees that it fits all screens perfectly. There are more than one screen size that fall under each density.
You should look at making your layout scale well on as many devices as possible. In your case it sounds like you shouldn't be focusing on an a specific pixel size, but rather how to display correctly on the common screens and gracefully display on less common ones. That being said I would do the following:
I'd look at the dashboard here. I'd make layouts targeting first targeting hdpi with a normal screen size(50.1% of the market) followed by xhdpi(25.1%) with a normal screen size, followed by mdpi(11%) normal screen size. Check table 3 on this page for common screen sizes for those values. Since you will be most likely using an image view make sure to check out the scale type attribute to help handle when the image view isn't at
As a side note(maybe useful later)if you are having difficulties translating sizes between densities and raw pixel values use this calculator.
Use following function which give you height and width of current display
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceHeightPix = displaymetrics.heightPixels;
deviceWidthPix = displaymetrics.widthPixels;
based on this, you can pass data on server to fetch relative images.

Android align ImageViews in different resolutions/densities

I have received a PSD layout from my client, and I it contains some fixed background images, and below those images there should be some Buttons, TextViews, etc.
The problem is, that for example the Game Over screen has a background, then there should be image with "You win" or "You lose", above the background in specific offset from top left corner.
If I design the interface for 480x800 phones, and specify the offset in pixels, everything's OK. But if I deploy it into 320x480 phone, it of course doesn't fit, because pixel offsets are different now and the background is scaled.
Using dip units doesn't help, because the smaller phones have physically smaller screens too.
here is a sketch of what I'd like to do. All and it should look similar on phones from 240x320 do 540x960. Is it possible to somehow do this in single XML layout file or do I have to hand-code offsets for every resolution that comes to my mind?
The main problem is that on the picture, the red frame around "you win" is a place where it fits into the orange background, because that's the way the graphics is designed. And when the app is run on phone with smaller resolution, I don't exactly know how to align the "you win" picture on the orange one. .
create different size images from provided PSD for different screen size layout, and create different layout xml file for different screen size, in different layout file you can provide reference of images which fit for layout.
For better aprroach you should go here and also this
#Axarydax if you want want your application to support different screen sizes you will have design them .
I had the same issue
placed images in drawable-hdpi for high density device , in drawable-ldpi for low density device and in drawable-mdpi for medium density device and then
what i did that i created three diffrent layout folders
like for 320x480
layout folder
for screen size 240x320
layout-port-320x240
for screen size 480x800
layout-port-480x800
and created the respective layout with same name
Once you have done compilation for all
android will automatically will pick the respective layout
for that i created emulators of the resolution and then adjusted the layouts as per my requirement
I managed the multi-resolution by getting offset of screen size.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
getDensityName(displaymetrics.density);
int dens=displaymetrics.densityDpi;
double wi=(double)width;
double hi=(double)height;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y)/(double)dens;
if(screenInches<3.7)
{
offset = 0.7f;
}
else if(screenInches<4.2)
{
offset = 1.1f;
}
else if(screenInches<5.0)
{
offset = 1.5f;
}
else if(screenInches<6.0)
{
offset = 1.7f;
}
else
{
offset = 2f;
}
I hope this may help others.Thanks!

Separate graphics for different resolutions, android

I need drawable resources for different resolutions, such as 320x480, 800x480, 854x480, 800x600
1024x600, 1024x768, 1024x800. It is a game and the scaling of bitmaps is inacceptable, coz they are very sensitive to it. dpi dependent folders and combination with screen sizes(which are deprecated but no other way to set drawable for large screens with same resolution and differend dpi) are not enaugh. How to distinguish graphics for 1024x600 and 1024x768 for example?
It is so sad to stop use mechanism of auto picking resources and switching to manual loading from assets.
any ideas?
I'm usually using 4 resource folders for drawables:
drawable-mdpi for 320x480
drawable-hdpi for 480x800 and 480x854
drawable-large-hdpi for 600x1024, 768x1024 and so on
drawable-xlarge-mdpi for 800x1280
These are just enough in my mind. Also, you don't need to worry about different drawable resources for, in example, devices with 800x480 and 854x480 screen sizes: you can specify an offset on the edges of your screen equal to 27 pixels and center your game on the screen. Hope this helps.
If you want a pixel perfect fit you could always load the background at runtime via a simple method:
private void setBackgroundImage() {
DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
// TODO: Conditional Structure to apply the right drawable
// ressource to be applied in the background
}
After you've acquired the resolution of the display you can apply the appropriate background via the setBackgroundDrawable method.

Categories

Resources