I have got a very good method for this situation works like a charm. You only need to create one hd image for you and than method works-out everything.
Here is the method:
/**
* Get DRAWABLE with original size screen resolution independent
* @param is Input stream of the drawable
* @param fileName File name of the drawable
* @param density Density value of the drawable
* @param context Current application context
* @return Drawable rearranged with its original values for all
* different types of resolutions.
*/
public static Drawable getDrawable(InputStream is, String fileName, int density, Context context) {
Options opts = new BitmapFactory.Options();
opts.inDensity = density;
opts.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
return Drawable.createFromResourceStream(context.getResources(), null, is, fileName, opts);
}
Here, you must prepare the inputstrem for your image file and set a density that is good for your screen at any usage area of yours. The smaller density is the lower quality, solve out by changing the value. Here are some examples on using the method:
1) Open an asset from the assets folder:
getDrawable(assetManager.open("image.png"), "any_title", 250, context)
2) Open a drawable from the drawables folder:
Here first, you must provide your inputstream with this method:
a) method:
/**
* Get InputStream from a drawable
* @param context Current application context
* @param drawableId Id of the file inside drawable folder
* @return InputStream of the given drawable
*/
public static ByteArrayInputStream getDrawableAsInputStream(Context context, int drawableId) {
Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(drawableId)).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
return new ByteArrayInputStream(imageInByte);
}
and the usage:
b) usage:
getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)
I hope it is useful.