Reputation: 123
I'm trying to set an xml file as a RelativeLayout
's background image. To do this, I have to convert the xml to a drawable.
This is what I've done...
RelativeLayout content = (RelativeLayout) findViewById(R.id.background);
// "content" is the xml file (which is currently displayed), that I want to use as the background image.
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
content.setDrawingCacheEnabled(false);
Drawable d = new BitmapDrawable(getResources(), bitmap);
// Now I'm changing layouts to the one that has the Relative Layout of which I want to add the xml/drawable background.
mvf.setDisplayedChild(0);
// The layout of which I want to add the xml/drawable background.
(findViewById(R.id.root)).setBackground(d);
These are the errors I'm getting.
W/Bitmap: Called getWidth() on a recycle()'d bitmap! This is undefined behavior!
W/Bitmap: Called getHeight() on a recycle()'d bitmap! This is undefined behavior!
W/Bitmap: Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
W/Bitmap: Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
W/Bitmap: Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@d5316ab
Upvotes: 3
Views: 8283
Reputation: 1
content.setDrawingCacheEnabled(false);
It should be called after R.id.root
is removed from its parent. This way will save memory.
Upvotes: 0
Reputation: 20346
Here bitmap = content.getDrawingCache()
you are getting not new bitmap but the same bitmap object, drawingCache, it's kind of pointer to the object. Somewhere in the future drawingCache is getting recycled and your bitmap object too.
I think you have to make copy of drawing cache. Something like
Bitmap bitmap = content.getDrawingCache().copy(bitmapConfig, false);
Upvotes: 1