2016-11-18 247 views
3

我在Android Studio中創建了Java類文件,但我不知道如何從mipmap中獲取圖像。我試圖添加上下文,但它不起作用。如何使用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> 

回答

2

你可以得到紋理貼圖相似圖片:

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

OR

imageView.setImageResource(R.mipmap.ic_launcher);