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

时间:2016-11-18 22:47:31

标签: java android image load android-resources

我在Android Studio中创建了一个Java类文件,但我不知道如何从mipmap获取图像。我试图添加Context但它不起作用。

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)
}

在值中有xml文件&#39;图像&#39;

 <?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>

1 个答案:

答案 0 :(得分:5)

你可以得到这样的mipmap图像:

Drawable drawable = ContextCompat.getDrawable(context,R.mipmap.ic_launcher);

OR

imageView.setImageResource(R.mipmap.ic_launcher);