Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.HashMap;
import android.content.Context;
import android.graphics.drawable.Drawable;

public class Main {
    private static HashMap<String, Drawable> iconCacheHashMap = new HashMap<String, Drawable>();

    public static Drawable getImgageDrawable(Context context, String iconStr) {
        int pos = iconStr.indexOf(".");
        if (pos != -1) {
            iconStr = iconStr.substring(0, pos);
        }
        if (!iconCacheHashMap.containsKey(iconStr)) {
            int res = context.getResources().getIdentifier(iconStr, "drawable", context.getPackageName());
            if (res == 0) {
                return null;
            } else {
                Drawable drawable = context.getResources().getDrawable(res);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                iconCacheHashMap.put(iconStr, drawable);
            }
        }
        return iconCacheHashMap.get(iconStr);
    }
}