2013-01-12 79 views
1

我明白,Android的选择基于它们的位置绘制(-ldpi,-mdpi等)。我应该如何组织我的图像,如果我有两组drawables,一个用于mdpi和ldpi,另一个用于hdpi和xhdpi?如何正确组织android drawable资源?

+0

有你试图把一组在MDPI和另一在hdpi上? –

+0

@ renam.antunes:这是行不通的。如果设备是xhdpi,并且在'/ drawable-xhdpi'文件夹中找不到drawable,则Android将搜索默认的'/ drawable'文件夹,并在next-lower-dpi文件夹中搜索_not_:“如果没有匹配的资源可用,系统将使用默认资源并根据需要调整其大小以匹配当前屏幕大小和密度“from [here](http://developer.android.com/guide/practices/screens_support.html#support) – saschoar

回答

0

现在,我没碰到过任何的可能性来 dpi的文件夹。 但你还是有选择:

虚拟绘制:

把你的图片到你的正常/drawable文件夹并命名他们像image1_low.pngimage1_high.png。现在把“虚拟绘制的”文件image1_dummy.xml到您的/drawable-ldpi/drawable-mdpi文件夹,包含这样的:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/image1_low" /> 

这同样适用于在/drawable-hdpi/drawable-xhdpi文件夹,但现在@drawable/image1_high,而不是@drawable/image1_low

现在,你可以参考一下虚拟的名字,例如通过

getResources().getDrawable(R.drawable.image1_dummy);

在你的代码以获得所需的绘制。

的程序化:

你并不局限于使用文件夹,还有一个方法的程序化(学分https://stackoverflow.com/a/5323437/1140682):

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
switch(metrics.densityDpi){ 
    case DisplayMetrics.DENSITY_XHIGH: 
     // assign your high-res version 
     break; 
    case DisplayMetrics.DENSITY_HIGH: 
     // assign your high-res version 
     break; 
    default: 
     // assign your low-res version 
     break; 
}