assets
(不是可绘制文件夹)文件夹中的子目录加载可绘制的绘制吗?
Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)
由于资源,它可以正确缩放可绘制的缩放...
import android.content.Context;
import android.graphics.drawable.Drawable;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by bartburg on 4-11-2015.
*/
public class AssetsReader {
public static Drawable getDrawableFromAssets(Context context, String url){
Drawable drawable = null;
InputStream inputStream = null;
try {
inputStream = context.getAssets().open(url);
drawable = Drawable.createFromStream(inputStream, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return drawable;
}
}
i我正在一个回收库适配器中工作,发现
david's s的答案对我不起作用(由于某种原因,无论我进口什么而尚未解决)我发现这对我有用(Kotlin Code)
public static Drawable getDrawableFromAssetFolder(String fullPath, Activity ctx) {
Drawable d =null;
try {
d = Drawable.createFromStream(ctx.getAssets().open(fullPath), null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
这有助于达到正确的密度
在此版本中,如果您在可绘制的文件夹中制作一个子文件夹,则无法在XML文件中使用它,当您使用Android:src.时,它将无法识别。
查看此线程:可绘制的可绘制目录包含子目录?