900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java mdpi_如何使用drawable兼容所有屏幕尺寸(idpi mdpi hdpi xhdpi xxhdpi)

java mdpi_如何使用drawable兼容所有屏幕尺寸(idpi mdpi hdpi xhdpi xxhdpi)

时间:2019-12-01 02:06:08

相关推荐

java mdpi_如何使用drawable兼容所有屏幕尺寸(idpi mdpi hdpi xhdpi xxhdpi)

我有一个非常好的方法,这种情况就像一个魅力 . 你只需要为你创建一个高清图像,而不是方法可以解决所有问题 .

这是方法:

/**

* 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);

}

在这里,您必须为图像文件准备inputstrem,并在您的任何使用区域设置适合您屏幕的密度 . 密度越小,质量越低,通过改变值来解决 . 以下是使用该方法的一些示例:

1)从资产文件夹中打开资产:

getDrawable(assetManager.open("image.png"), "any_title", 250, context)

2)从drawables文件夹中打开一个drawable:首先,你必须使用这个方法提供你的输入流: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();

press(pressFormat.PNG, 100, stream);

byte[] imageInByte = stream.toByteArray();

return new ByteArrayInputStream(imageInByte);

}

和用法:b)用法:

getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)

我希望它有用 .

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。