ulfben icon

BitmapPool

ulfben | PRO | 02/26/18 03:12:52 PM UTC | 0 ⭐ | 1367 👁️ | Never ⏰ | []
Java |

2.31 KB

|

None

|

0 👍

/

0 👎

import android.graphics.Bitmap;
import java.util.HashMap;
public class BitmapPool {
    private static final String TAG = "BitmapPool";
    private static final HashMap<String, Bitmap> mBitmaps = new HashMap<>();
    private BitmapPool(){super();}
 
    public static Bitmap createBitmap(final Game engine, final String sprite, float widthMeters, float heightMeters){
        final String key = BitmapPool.makeKey(sprite, widthMeters, heightMeters);
        Bitmap bmp = BitmapPool.getBitmap(key);
        if(bmp != null){
            return bmp;
        }
        try {
            bmp = BitmapUtils.loadScaledBitmap(engine.getContext(), sprite, (int)engine.worldToScreenX(widthMeters), (int)engine.worldToScreenY(heightMeters));
            BitmapPool.put(key, bmp);
        }catch(final Exception e){          
            throw AssertionError(e.toString);
        }  
        return bmp;
    }
 
    public static int size(){ return mBitmaps.size(); }
    public static String makeKey(final String name, final float width, final float height){
        return name+"_"+width+"_"+height;
    }
    public static void put(final String key, final Bitmap bmp){
        if(mBitmaps.containsKey(key)) {
            return;
        }
        mBitmaps.put(key, bmp);
    }
    public static boolean contains(final String key){
        return mBitmaps.containsKey(key);
    }
    public static boolean contains(final Bitmap bmp){ return mBitmaps.containsValue(bmp); }
    public static Bitmap getBitmap(final String key){
        return mBitmaps.get(key);
    }
    private static String getKey(final Bitmap bmp){
        if(bmp != null) {
            for (HashMap.Entry<String, Bitmap> entry : mBitmaps.entrySet()) {
                if (bmp == entry.getValue()) {
                    return entry.getKey();
                }
            }
        }
        return "";
    }
    private static void remove(final String key){
        Bitmap tmp = mBitmaps.get(key);
        if(tmp != null){
            mBitmaps.remove(key);
            tmp.recycle();
        }
    }
    public static void remove(Bitmap bmp){
        if(bmp == null){return;}
        remove(getKey(bmp));
    }
    public static void empty(){
        for (final HashMap.Entry<String, Bitmap> entry : mBitmaps.entrySet()) {
            entry.getValue().recycle();
        }
        mBitmaps.clear();
    }
}

Comments

  • Gyqizv icon
    04/02/26 06:18:26 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  • Yuizcko icon
    05/18/26 01:10:21 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400