How To Get Image From Drawable-xhdpi
Solution 1:
you can not set like R.drawable-hdpi.tree
. only R.drawable.tree
is created for every drawable tree images. if you use R.drawable.tree
then system will choose automatically depending on the screen density.
now if you want different image for diffeent resolution you can use different images to show in your app.
Solution 2:
The system should do this automatically for you.
So let's say you have a image, image.png
, made specifically for each density.
Your code shouldn't need to know the actual density in order to operate. It should just focus on assigning the conceptual image to the image view.
So in the project's res folder:
<YourProjectHome>/res/
There are folders:
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Just put the image specific to each density in the corresponding folder, and name them exactly the same. In this example it would be just image.png
Then in your code you would just do
treepic.setImageResource(R.drawable.image);
There are some cases where yes you would like to specify the image to be of a specific density. There is rather new call, getDrawableForDensity()
This one will look up a specific version of a drawable for the given density. However for the common case it is overkill. It's only really necessary if you were to be downsampling. I.e. You don't have a hdpi version of something but like you have an xxxhdpi version and want to scale it down to look semi ok.
Solution 3:
you can use unique names for the equal pictures in folders: R.drawable-hdpi.tree_hdpi R.drawable-mdpi.tree_mdpi etc.
Solution 4:
Everything about pix density in your screen is managed by Android. you don't need to set or locate the images. android will choose the image correct image for you
you can use R.drawable.tree
to choose the image automatically for you
- /drawable-ldpi For low density screens
- /drawable-mdpi For medium density screens
- /drawable-hdpi For high resolution screens
- /drawable-xhdpi For extra high resolution screens
so just place all the different density image into appropriate folder with same name.
and call them by R.drawable.tree
Android will choose correct one depends on the screen density for you
/drawable should be reserved for assets that you don't either care which device or for xml drawable assets
if you need more info about screen support resolutions goto this link
Post a Comment for "How To Get Image From Drawable-xhdpi"