我正在开发许多图像的应用程序。 一些图像,我搬到了扩展的文件,因为应用程序的限制(50MB)。 我把我所有的图像转换成不同的文件夹一样绘制-xhdpi,绘制-MDPI等之前现在我编程确定哪些dpi是手机并相应地获取图像。 下面是代码的重要位:
public String getFolder()
{
String folder = "";
int dpi = res.getDisplayMetrics().densityDpi;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float scaleFactor = metrics.density;
float widthDp = widthPixels / scaleFactor;
float heightDp = heightPixels / scaleFactor;
float smallestWidth = Math.min(widthDp, heightDp);
if (smallestWidth > 720 && dpi == DisplayMetrics.DENSITY_MEDIUM) {
//Device is a 10" tablet
}
else if (dpi == DisplayMetrics.DENSITY_XHIGH||dpi == DisplayMetrics.DENSITY_TV) {
//Device is a 7" tablet
//xhdpi
folder = "drawable-xhdpi/";
}
else if((smallestWidth > 480 && dpi == DisplayMetrics.DENSITY_MEDIUM)|| dpi == DisplayMetrics.DENSITY_HIGH)
{
//hdpi
folder = "drawable-hdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_XXHIGH)
{
//xxhdpi
folder = "drawable-xxhdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_MEDIUM)
{
//mdpi
folder = "drawable-mdpi/";
}
else if(dpi == DisplayMetrics.DENSITY_LOW)
{
//ldpi
folder = "drawable-ldpi/";
}
return folder;
}
public BitmapDrawable getDrawable(Context ctx, String imageName){
String path = folder+imageName;
ZipResourceFile zip;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b = null;
try {
zip = APKExpansionSupport.getAPKExpansionZipFile(ctx, 9, -1);
InputStream is = zip.getInputStream(path);
b = BitmapFactory.decodeStream(is, null, bfo); }
catch (IOException e) {
e.printStackTrace();
}
return new BitmapDrawable(b);
}
所以,每当我需要一个像我打电话getDrawable像这样:
newItem.setBackgroundDrawable(storage.getDrawable(this,"red_stored.png"));
但问题是,现在当我做编程,图像看起来比我会把它变成绘制-xhdpi文件夹更小和Android authomatically从该文件夹捡起来。 我能做些什么来解决这个问题?