java - 如何使用 Java 类从资源 mipmap 获取图像

标签 java android image load android-resources

我在 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/

相关文章:

javascript - 尝试使用javascript创建一个函数来放大图像

ImageMagick:此图像格式没有解码委托(delegate) `' @ error/constitute.c/ReadImage/504

java - Apache SocketFactory 与 java.net SocketFactory 兼容吗?

java - 当项目名称与工作区名称相同时,Eclipse 中的 "The project: swing which is referenced by the classpath, does not exist"

java - 如何制作像 Nexus one 天气/新闻应用这样的标签?

java - 如何在 android 的 firebase 数据库中跳过子项

html - 如何让我的文字高于我的 img <div>?

java - 使用 Jmap 在 OutOfMemoryError 上转储 Java 堆文件

java - 使用 spring-hibernate 的 2 个不同的数据库连接

android - 长按按钮上的上下文菜单?