Handle 'match-parent' images - android

Let's say I have an ImageView like this:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/pic.png" />
I will have different sizes for pic.png in many folders:
drawable-mdpi: 100*100px
drawable-hdpi: 150*150px
drawable-xhdpi: 200*200px
drawable-xxhdpi: 300*300px
Now, if I have this ImageView
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/pic.png" />
What sizes should my images in drawable-* be? My ImageView will obviously be matching the screen bounds.
Thanks

You should create images of differents sizes and put them in its corresponding folders, drawable-mdpi, drawable-hdpi etc. Check the article of developer.android. You need to start at a scale, for example xxxhdpi, and from there scale down your images for densities mdpi, hdpi, xhdpi, xxhdpi. Based on the docs, you should follow this:
“So where do you begin when designing for multiple screens? One approach is to work in the base standard (normal size and MDPI) and scale it up or down for the other buckets. Another approach is to start with the device with the largest screen size, and then scale down and figure out the UI compromises you'll need to make on smaller screens.”

Related

Scaling image resources in Android and Picasso

There are so many material on densities, multiple screen support, so many questions on SO, but I still have not got it.
My goal is simple: display a bitmap as large as available space for ImageView. I need BitMap as I will do some operations on it.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/figureView" />
I will have some pictures to be used in the bitmap. I will place them in drawables directory. I will reference them with R.drawable.pictureX. I will use Picasso to load them and scale them:
Bitmap bitmap = Picasso.with(getContext()).load(resourceId).resize(w, h).get();
The unclear part for me is all those xxhdpi folders and Android heuristic to select the best.
When a documentation says that Android will automatically scale the image does it apply to my case? I do not want to scale already scaled pictured.
How many dpi variants shall I store in my case and where? Shall I have single file in no-dpi folder or shall I create picture variant for each dpi folder?
How can I determine a dimension for picture resource? It is easy for icons: for example 24x24 dpi and then multiple it with DPI formula. But I want to cover complete screen height. A chapter Configuration examples lists: 240x320 ldpi, 320x480 mdpi, 480x800 hdpi, 720x1280 mdpi, 800x1280 mdpi etc. There are no screen size qualifiers for resources.
Thanks for clarification.
I realized that in fact it is easy to find out Android behavior. I just need each DPI variant different so I can distinguish between them. I put a text with DPI name and pixel resolution inside each picture.
There is a sample GitHub Test DPI project. It has two ImageViews. The first is initialized from XML and Android does scaling. The second is a placeholder that I fill with a BitMap. I entered 200dp as its size and that was correct. Pixel size of MDPI = size in dp.
What I found is logical. Android does not know anything about my intentions. It just selects the best available resource for current DPI.
Nexus 5x uses XXHDPI by default, Samsung SIII Mini uses HDPI. If I delete their default DPI folders, they down-scale higher available variant: XXXHDPI and XHDPI. If I delete all variants except NODPI, this will be used.
And finally if I change the dimensions of ImageView dynamically loaded from source code to 300dp, then it will be scaled and look ugly. Android cannot know at decodeResource execution that I will scale it later and that there is a better resource available.
What remains unknown is how to find out dimensions of picture that fits the screen.
Java:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = false;
Bitmap figureBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_test, opt);
ImageView imageView=(ImageView) findViewById(R.id.testView);
imageView.setImageBitmap(figureBitmap);
Activity:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_test" />
<ImageView
android:id="#+id/testView"
android:layout_width="200dp"
android:layout_height="200dp" />
Update 30th august
Original picture: 230x541 px, loading with
Picasso.with(getContext()).load(R.drawable.amalka).into(target)
stored in nodpi, loaded as 230x541 px Bitmap
stored in xxhdpi, loaded as 230x541 px Bitmap
1.Android will automatically scale image in case when you provided image for one screen dencity and there is no such image for another one. e.g. if you add your image only in drawable-xxxhdpi folder, Android will generate images for xxhdpi, xhdpi screen dencities etc.
2.You can save your image in one folder drawable-xxxhdpi and let Android make the downscale, if you don't like the result you can create images for different dencities on your own.
name dencity scale
mdpi 160dpi x1
hdpi 240dpi x1.5
xhdpi 320dpi x2
xxhdpi 480dpi x3
xxxhdpi 640dpi x4
3.You can define dimensions of your images based on size of you ImageView and dpi scale. For example if your ImageView has width 80dp and height 40dp then your image for drawable-mdpi folder should have size 80x40px because mdpi is a baseline dencity, for drawable-hdpi then you need to have image 120x60px (scale is 1.5) etc. For drawable-xxxhdpi your image will be 320x160px.
If you want your image to fit all the screen on all devices you can use parameter android:scaleType="centerCrop", it will make the image take all the space but little part of image can be hidden depending on screen aspect ratio. You can also try to specify image resorces based on shortest dimension of the available screen area, e.g. drawable-sw720dp. Read more about this here.

Android Resize images according to the screen size of the device

I hope someone can help me.
I have days looking for a way to Resize images in android according to the screen size of the device.
Sometimes the picture is fine but when used in a slightly larger image comes to be very small and not resized or adapted to the screen size.
For there I saw that for every image that wanted to join the project, was to add image in different sizes, Example: 80x80, 160x160,220x220,320xx320 (each has the same name.) and that helped resized.
Or did so but not me. sometimes the image is totally distorted.
This is the code I use for ImageView
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inicio"
android:background="#drawable/inicio1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:layout_below="#+id/imageView"
/>
that I can do to make the image take an appropriate size according to the size of the screen.
Thank You
do this
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inicio"
android:background="#drawable/inicio1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:scaleType:"fitXY"
android:layout_below="#+id/imageView"
/>
You can use different sized images in different folders.Put same image with different sizes i.e small size image for ldpi folder,slightly bigger for mdpi.You can refer the documentation here
hdpi,ldpi,mdpi,xhdpi and xxhdpi
Have you put the images into their respective sizefolders? Like Android tells you here: http://developer.android.com/guide/practices/screens_support.html
Basically in the drawable folder you should have another set of folders with the names ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi. In these folders you should put your image with the same name but different image size. Look at the link for the different sizes suggested.

Image button size for multiple screens

I am having trouble with android layouts, I want to make app that will support multiple screens, tablets included.
But I am having problem with button sizes.
How do I set pictures to fit multiple screen sizes?
I have pictures in ressources for Xdpi Mdpi ldpi, but it seems that eclipse is not using them.
Regards
My button looks like this:
<ImageButton
android:id="#+id/sound4"
android:layout_width="160dp"
android:layout_height="81dp"
android:layout_alignLeft="#+id/sound2"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/sound3"
android:background="#drawable/famas"
android:onClick="onClick" />
1)please you can use 9-patch image .so this image will stretch automatically
on your device height and width.
use this link to create 9-pathc image online..
draw9patch.com
or you can use other option:
2)button height and width set in all dimension file in values folder for all devices.
for ex.values-xhdpi,values-hdpi,values-mdpi...etc
You need to do this:--
create different types of drawables according to your multiple devices.
like this:-
drawable-hdpi,
drawable-mdpi,
drawable-ldpi,
drawable-xhdpi,
drawable-xxhdpi,
drawable-sw600dp
drawable-large-hdpi,etc.
You need to use the values folder for this case.make different dimension file and set it according to your device specification.
Like this:--
values-mdpi,
values-ldpi,
values-hdpi,
values-xxdpi,
values-xlarge,etc.

Trouble grasping the dp unit in Android

So, I have googled a lot, but still cant quiet grasp the concept. I have 3 folders : xhdpi, lhdpi and mhdpi. I do understand the conversion and downscaling to different dip and screen densities. Android selects resources for the right screen type when the application runs.
But, how do I start? I made a background for my application in Photoshop. The background was defined in 720x1080px and exported as an .png file. I put the .png in the xhdpi folder. Everything worked out fine on my Sony Xperia Z, but when a friend loaded it on his Galaxy 3 the background was "to-small" and did not fit his screen. I assume this is because the bacground was to small.
But how large do I need to make it? What px-sizes should xhdpi drawable resources be to fit every single xhdpi screen?
Would a good approach be to start with the largest size, xhdpi? And then convert the drawables down to mdpi and ldpi later on? If so, I need a starter size in px so I can create the background for the application in Photoshop.
Here is how I use the background : (Note, i changed my height to fill_parent it fixed the "not filling the whole screen problem")
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_xhdpi" >
</RelativeLayout>
To make PX sizes scale appropriately you will need to scale using the following ratios:
2X for XDPI
1.5X for HDPI
1X for MDPI (baseline
.75X for LDPI
When you are specifying sizes in your code (or XML), use the DP parameter to specify the value (or SP if you are specifying fonts).
for example: android:layout_marginLeft="12dp"
Here is the docs: http://developer.android.com/guide/practices/screens_support.html, you will notice this image, and a lot of other details describing this.
You can find in this table a briefing of different devices screen densities and resolutions; according to it, Galaxy SIII is xhdpi with a resolution of 1280 X 720. Besides, you could want considerate to follow compatibility mode guide to avoid rendering issues in multiple screens.

How can I get my layout to look the same across multiple screen sizes and densities?

This is my code:
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="50dp"
android:textColor="#873670" />
<TextView
android:id="#+id/questionList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#110987" />
<RadioGroup
android:id="#+id/rgAns"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbOpt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="usrAnsrChoice" />
<RadioButton
android:id="#+id/rbOpt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="usrAnsrChoice" />
<RadioButton
android:id="#+id/rbOpt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="usrAnsrChoice" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="40dp"
android:orientation="horizontal" >
<Button
android:id="#+id/prevQstn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quizPrvBtnLbl" />
<Button
android:id="#+id/endTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quizEndTstBtnLbl" />
<Button
android:id="#+id/nextQstn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quizNxtBtnLbl" />
<TextView
android:id="#+id/inc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="100dp"
android:textColor="#ffffff" />
</LinearLayout>
I want this screen to be adjusted automatically for 4", 5", and 2" screens. I also want to fully support mdpi and ldpi screens and have everything look the same on a 7" screen as it would on a 4" screen. What changes must I make to my XML to support these screen sizes and densities?
Right now my layout looks fine on small screens but not so much on larger screens. Also, I don't want my images to be stretched or my text to be misaligned.
To design a layout that works well on several different types of screens you should create layout files for each screen size. The old way of doing this is similar to what Danny outlined in his answer, but not quite accurate. The old way of supporting multiple screen sizes is as follows:
layout-xlarge: xlarge screens are at least 960dp x 720dp
layout-large: large screens are at least 640dp x 480dp
layout-normal: normal screens are at least 470dp x 320dp
layout-small: small screens are at least 426dp x 320dp
This way of defining layouts for different screen sizes is still supported but it is now suggested that this method be replaced by targeting minimum screen widths. The reason for targeting screen widths instead of generic size "buckets" is that it was sort of ambiguous when determining which bucket a screen actually fit into and there were some devices that were reporting the wrong bucket. By targeting screen widths, the device can no longer incorrectly report its size.
Screen width folders would be setup like this:
layout-sw320dp: 320dp -> a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
layout-sw480dp: 480dp -> a tweener tablet like the Dell Streak (480x800 mdpi).
layout-sw600dp: 600dp -> a 7" tablet (600x1024 mdpi).
layout-sw720dp: 720dp -> a 10" tablet (720x1280 mdpi, 800x1280 mdpi, etc).
For your drawable resources, instead of targeting the screen size, you would want to target the screen density. The basic idea with this is that you create all your resources targeting the mdpi density bucket and scale your drawables for different densities. So in this case you'd have the following drawable folders for your image resources:
drawable-ldpi: Low density screens
drawable-mdpi: Normal or medium density screens
drawable-hdpi: High density screens
drawable-xhdpi: Very high density screens
A better explanation can be found on the Android Developer Reference and is excerpted here (density bucket names added by me):
Alternative drawables
Almost every application should have alternative drawable resources
for different screen densities, because almost every application has a
launcher icon and that icon should look good on all screen densities.
Likewise, if you include other bitmap drawables in your application
(such as for menu icons or other graphics in your application), you
should provide alternative versions or each one, for different
densities.
Note: You only need to provide density-specific drawables for bitmap
files (.png, .jpg, or .gif) and Nine-Path files (.9.png). If you use
XML files to define shapes, colors, or other drawable resources, you
should put one copy in the default drawable directory (drawable/).
To create alternative bitmap drawables for different densities, you
should follow the 3:4:6:8 scaling ratio between the four generalized
densities. For example, if you have a bitmap drawable that's 48x48
pixels for medium-density screen (the size for a launcher icon), all
the different sizes should be:
36x36 for low-density (ldpi)
48x48 for medium-density (mdpi)
72x72 for high-density (hdpi)
96x96 for extra high-density (xhdpi)
For more information about designing
icons, see the Icon Design Guidelines, which includes size information
for various bitmap drawables, such as launcher icons, menu icons,
status bar icons, tab icons, and more.
A complete reference on supporting multiple screen sizes effectively can be found on the Android Developer Site.
You should use RelativeLayout as the root view. Then you can specify relations between UI elements as above, below etc. It is much easier to design for multiple displays using RelativeLayout
To design the layout neatly with good UI.
you create the folders in drawable
1. layout-large for the hdpi devices like google nexus
2. layout-xlarge for the xhdpi devices like touchpads
3. layout-small for the small devices less than medium devices
And align them the fields and elements where you want set the font size based on the layouts.
To appear good UI.

Categories

Resources