私は、この状況が魔法のように機能するための非常に優れた方法を持っています。hd イメージを 1 つ作成するだけで、すべての方法で解決できます。
メソッドは次のとおりです。
/**
* 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);
}
ここでは、画像ファイルの入力ストリームを準備し、画面のどの使用領域でも適切な密度を設定する必要があります。密度が小さいほど品質が低くなります。値を変更して解決します。メソッドの使用例を次に示します。
1) アセット フォルダーからアセットを開きます。
getDrawable(assetManager.open("image.png"), "any_title", 250, context)
2) drawables フォルダーからドローアブルを開きます: ここではまず、入力ストリームに次のメソッドを提供する必要があります: a) メソッド:
/**
* 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);
}
および使用法: b) 使用法:
getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)
役に立つことを願っています。