我在 Android Studio 中创建了一个 Java 类文件,但我不知道如何从 mipmap 获取图像。我已尝试添加上下文,但它不起作用。
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ineza on 18/11/2016.
*/
public class DerpData {
private static final String[] titles = {
"Nothingness cannot be defined",
"The softest thing cannot be snapped",
"be like water, my friend."
};
private static final int[] icons = ;
public static List<LisItem> getListData() {
List<LisItem> data = new ArrayList<>();
// Repeat process 4 times, so that we have enough data to demonstrate a scrollable
// RecyclerView
for (int x = 0; x < 4; x++) {
//create ListItem with dummy data, then add them to our List
for (int i = 0; i < titles.length && i < icons.length; i++) {
LisItem item = new LisItem();
item.setImageResId(icons[i]);
item.setTittle(titles[i]);
data.add(item);
}
}
return data;
}
}
我使用了下面的代码块,但它不起作用:
public class DerpData extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
private static final int[] icons = Resources.getSystem().getIntArray(android.R.array.imgs)
}
在值 ihave xml 文件“图像”
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="imgs">
<item>@mipmap/computer</item>
<item>@mipmap/house</item>
<item>@mipmap/sunflower</item>
<item>@mipmap/dog</item>
</integer-array>
</resources>
最佳答案
您可以获得这样的 mipmap 图像:
Drawable drawable = ContextCompat.getDrawable(context,R.mipmap.ic_launcher);
或
imageView.setImageResource(R.mipmap.ic_launcher);
关于java - 如何使用 Java 类从资源 mipmap 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687100/