Remote images and sizes for hdpi/mdpi/ldpi etc - android

So on device there's the drawable directories for hdpi, mdpi, ldpi etc...
Now lets say I want to download images from the internet to view in the app (on the fly).
Can I have the three different densities of image available for download? Is there some way I can check if the current device is hdpi/mdpi/ldpi and download the right resolution accordingly? Or is it much more simple than that?
I guess if I download a high res image onto a hdpi phone then it will assume its a mdpi image that just has larger dimensions than intended?
Thanks

This is how you get the density programatically
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}

Related

How to put images in drawable folder dynamically

I will get 5 different images from server that will be in xxxhdpi, xxhdpi, xhdpi, hdpi, mdpi.
The URL will be something like this.
http://www.abctest.com/image-xxxhdpi.jpg
http://www.abctest.com/image-hdpi.jpg
Is there any way i can put these images in drawable folder. So that each device will pickup the image according to its density. I googled but unable to find any way to achieve this.
Or If go to different approach, and get 1 large image from server, then on smaller devices it will create lagging.
Is there any way i can put these images in drawable folder.
Not at runtime. Resources and assets are read-only at runtime.
You can check density of the device by this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenDensity = metrics.densityDpi;
and choose image based on the density.
Edited
or you can also do this :
float density = getResources().getDisplayMetrics().density;
// return 0.75 if it's LDPI
// return 1.0 if it's MDPI
// return 1.5 if it's HDPI
// return 2.0 if it's XHDPI
// return 3.0 if it's XXHDPI
// return 4.0 if it's XXXHDPI

Optimizing image size for quality display on image view

I want to download image from my server and display it on an image view.
My main concern is to not damage the image quality on different devices (with different screen sizes, resolutions and densities)
I want to figure out exactly how to measure what image size i need to fit the image view keeping its quality
For the sake of the question,
Say i have an image view with 100dp * 100dp size what image resolution do i need to download in order to keep the quality for hdpi, xhdpi, xxhdpi and xxxhdpi devices?
I already know how to get the resolution and desisty of the device
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) m_context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
Log.d(Utils.TAG,"displaymetrics : Hieght: "+ height + " width: " + wwidth);
switch (m_context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
Log.d(Utils.TAG,"Low desity");
break;
case DisplayMetrics.DENSITY_MEDIUM:
Log.d(Utils.TAG,"medium desity");
break;
case DisplayMetrics.DENSITY_HIGH:
Log.d(Utils.TAG,"high desity");
break;
case DisplayMetrics.DENSITY_XHIGH:
Log.d(Utils.TAG,"xhigh desity");
break;
case DisplayMetrics.DENSITY_XXHIGH:
Log.d(Utils.TAG,"xxhigh desity");
break;
case DisplayMetrics.DENSITY_XXXHIGH:
Log.d(Utils.TAG,"xxxhigh desity");
break;
}
Low density (ldpi): 75x75
Medium density (mdpi): 100x100, baseline
High density (hdpi) 150x150
Extra high density (xhdpi) 200x200
Extra x2 high density (xxhdpi) 300x300
Extra x3 high density (xxxhdpi) 400x400
See Range of screens supported | Android Developers

Android drawable for Samsung Galaxy S4 and Nexus 10

I have an image that is the background of one activity in my app.
For the Samsung Galaxy S4 this image should be 1920x1080 and placed at the xxhdpi folder.
For the Nexus 10 this image should be 2560x1600 and placed at the xhdpi folder.
It's nonsense to place an image bigger in the xhpdi folder than an image placed at the xxhdpi folder.
And because that I believe that I misunderstood something.
Can someone explain what I misunderstood?
--edit--
For all the answer questioning if the nexus10 is really xhdpi and the S4 xxhdpi:
The answer of prijupaul is good, but I don't have any Nexus 10 or Galaxy S4 to test. I discovered the resolution trying to create AVDs for both, in the device configuration creation it says what configuration it one will be.
The resource classes 'hdpi', 'xhdpi' and 'xxhdpi' have nothing to do with resolution (width and height in plain pixels) but everything to do with density (number of pixels per inch of screen).
The S4 has a lower resolution but can have a higher density because it screen is smaller.
I think it is quite well explained in the documentation (developer.android.com). Check that for more details.
You should then use another qualifier than xhdpi if resolution does really matter. You can use for example drawable-sw720dp (targets 10" tablets).
My personal opinion though is that you should just have a version for each aspect ratio in the biggest size. Then compute that aspect ratio at runtime and pick the best image from assets.
Isnt S4 is xhdpi? Did you verify using
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int density = dm.densityDpi;
switch(density)
{
case DisplayMetrics.DENSITY_LOW:
Toast.makeText(context, "ldpi", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_MEDIUM:
Toast.makeText(context, "mdpi", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_HIGH:
Toast.makeText(context, "hdpi", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XHIGH:
Toast.makeText(context, "xhdpi", Toast.LENGTH_SHORT).show();
break;
case DisplayMetrics.DENSITY_XXHIGH:
Toast.makeText(context, "xxhdpi", Toast.LENGTH_SHORT).show();
break;
}
This does not answer the question. But, This could will be useful to double check it again.
I think it's about to 'PPI'. Galaxy s4 has 441 ppi but Nexus 10 has 300 ppi.
Are you sure image for nexus10 should be in xhdpi folder?
Nexus10 has 300ppi,which is different to dip.
Visit Android XXHDPI resources
It should be like this,
s4:xhdpi
nexus10:xxhdpi

How to change image according resolution?

I am working on an android application. i have used different layout folder. Like layout,layout-large,layout-xlarge. So that it can adjust for all resolutions. But i am setting images dynamically in activity. How can i change them according to screen resolution? How too big image will replace smaller if we change resolution?
Android works with density buckets, which go from l(low)dpi to xx(extra extra)h(high)dpi.
You want to create different versions of your images in folders as
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
and drawable-xxhdpi if you want to support the Nexus 10.
That's kind of loose from the layout-large folders, which enable you to define different layouts for different sizes.
2 pretty different things, which you can read much more about at
screen practices in Android.
=======
Edit; Seems this wasn't exactly the question.
If you're doing it 'the right way' the Android system will choose the correct image for you, even when adding them dynamically (you can still call R.drawable.my_image from java code).
If for some reason you do have to choose, you can simply check for the current density with something like (a little outdated);
public String getDensity() {
String density;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// will either be DENSITY_LOW (120) , DENSITY_MEDIUM (160) or
// DENSITY_HIGH (240)
switch (dm.densityDpi) {
case 120:
density = "ldpi";
break;
case 160:
density = "mdpi";
break;
case 240:
density = "hdpi";
break;
// use hdpi as default, because flurry shows this will be suited for
// most of our users.
default:
density = "hdpi";
break;
}
return density;
}

dpi for downloadable images

its simple my doubt.
The app have 4 directories(ldpi, mdpi, hdpi, xhdpi) with same images but different dpi . For mdpi is 160dpi, the one im using atm.
What happens with the images i download from the web? i have a news reader and that means each new have one image at least. And i want to have a decent image for all the smartphones.
Do i need different type of images or just download the same for all?
Can i know which resolution is best for each particular smartphone?
I couldn't find the "official" way to handle this so i dont know if its important dpi for images that we plan to download.
Regards!
ldpi means ~120dpi,mdpi means ~160dpi, hdpi means ~240dpi. xhdpi means ~320dpi.
you can follow 3:4:6:8 ratio for ldpi:mdpi:hdpi:xhdpi for resources.
Now you can have different images for all the resolution it is better and there must be no problem. If you cant have different images for all then have a image for xhdpi and runtime you can resize the image depending on the screen resolution.
YOu can use below sample code to know the screen resolution at runtime::
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch (metrics.densityDpi) {
case 120:
//your stuff
break;
case 160:
////your stuff
break;
case 240:
///your suff
break;
case 320:
//your stuff
break;
}
sample code to resize the bitmap::
Bitmap mIcon_val = your bitmap;
Matrix matrix = new Matrix();
matrix.postScale((float)newwidth/mIcon_val.getWidth(), (float)newheight/mIcon_val.getHeight());
Bitmap resizedBitmap = Bitmap.createBitmap(mIcon_val, 0, 0, mIcon_val.getWidth(), mIcon_val.getHeight(), matrix, true);
now you have the new bitmap new width and height you can use it.
If you want to construct a great app, you need to manage every image to fit the right smartphone, it wont look nice if you download the same image for all.
For more info go:
Supporting Multiple Screens

Categories

Resources