Code example for Bitmap

Methods: copyisRecycled

0
    public void draw(final Canvas canvas) {
        if (isCacheEnabled) {
            if (cache == null || cache.isRecycled()) {
                updateCache(); 
            } 
            if (cache != null && !cache.isRecycled()) {
                canvas.drawBitmap(cache, null, new Rect(0, 0, getWidth(), getHeight()), paint);
            } 
        } else { 
            super.draw(canvas);
        } 
    } 
 
    public void enableCache() { 
        if (!isCacheEnabled && android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            updateCache(); 
            isCacheEnabled = true;
        } 
    } 
 
    @Override 
    public void invalidate() { 
        if (!isCacheEnabled) {
            super.invalidate(); 
        } 
    } 
 
    @Override 
    protected void onDraw(final Canvas canvas) {
        if (!isCacheEnabled) {
            super.onDraw(canvas);
        } 
    } 
 
    @Override 
    public void setAlpha(final float alpha) {
        paint.setAlpha((int) (255 * alpha));
    } 
 
    private void updateCache() { 
        setDrawingCacheEnabled(true);
        final Bitmap current = getDrawingCache();
        if (current != null && !current.isRecycled()) {
            cache = current.copy(Bitmap.Config.RGB_565, false);
        } 
    } 
}