Is it possible to load my drawables for hdpi, mdpi and ldpi from given URLs at runtime and use them ?
If not, how can I overcome the denisity problem ?
Is it possible to load my drawables for hdpi, mdpi and ldpi from given URLs at runtime and use them ?
If not, how can I overcome the denisity problem ?
Based on screen density you can set the image URL,
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
url = "your_ldpi_url";
break;
case DisplayMetrics.DENSITY_MEDIUM:
url = "your_mdpi_url";
break;
case DisplayMetrics.DENSITY_HIGH:
url = "your_hdpi_url";
break;
}
And you can fetch that image and use,
try {
InputStream is = (InputStream) this.fetch(url);
image = Drawable.createFromStream(is,"src");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It's impossible. You can't do this after you have built project. Use this:
res/drawable-ldpi/my_icon.png // bitmap for low density
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
If you really, really want to, you can get the screen density at runtime and download a bitmap depending on the result. However, this is an extraordinarily inefficient way to support multiple screen densities, and should be avoided.
If you need to appropriately resize an image that you have to download (rather than one that will be present on every run), take a look at the BitmapDrawable class.