/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *   AndroidWorld Library, Copyright 2011 Bryan Chadwick                   *
 *                                                                         *
 *   FILE: ./android/image/FromResource.java                               *
 *                                                                         *
 *   This file is part of AndroidWorld.                                    *
 *                                                                         *
 *   AndroidWorld is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License           *
 *   as published by the Free Software Foundation, either version          *
 *   3 of the License, or (at your option) any later version.              *
 *                                                                         *
 *   AndroidWorld is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with AndroidWorld.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package android.image;

import java.util.Hashtable;
import android.graphics.*;
import android.content.res.Resources;

/** Represents an Image from an Android Project Resource.   */
public class FromResource extends FromFile{
    
    /** Create an Image from the given Resources and ID.  In an Eclipse 
     *    Android project, adding an image to the <tt>res/drawable-*</tt>
     *    directories causes resource IDs to be generated in the <tt>R</tt>
     *    class.  The application's {@link android.content.res.Resources Resources}
     *    instance can be obtained from the current {@link android.app.Activity Activity}
     *    using {@link android.app.Activity#getResources}.  Image resource IDs are cached
     *    so that images are only loaded once. */
    public FromResource(Resources res, int id){
        try{
            if(loadedRes.containsKey(id)){
                img = loadedRes.get(id);
            }else{
                img = BitmapFactory.decodeResource(res, id);
                loadedRes.put(id, img);
            }
            this.init(img);
        }catch(Exception e){
            throw new RuntimeException("Image Resource: \""+id+"\" Not Found!");
        }
    }    
    /** Store File Images, to avoid multiple loads */
    private static Hashtable<Integer,Bitmap> loadedRes = new Hashtable<Integer,Bitmap>();
}