2016-08-05 27 views
1

xmlファイルをRelativeLayoutの背景イメージとして設定しようとしています。これを行うには、xmlをドロウアブルに変換する必要があります。drawableとしてXMLを使用しているときに「リサイクルビットマップを使用しようとしています」という例外が発生しました

これは私がやったです...

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); 

これらは私が取得していますエラーがあります。ここで

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 [email protected] 

答えて

0

bitmap = content.getDrawingCache()は、あなたはそれはオブジェクトへのポインタのようなものだ、新しいものではないが、同じビットマップビットマップオブジェクト、drawingCacheを得ています。将来のどこかでdrawCacheがリサイクルされ、ビットマップオブジェクトも取得されています。

私はあなたが描画キャッシュのコピーを作る必要があると思います。

Bitmap bitmap = content.getDrawingCache().copy(bitmapConfig, false); 

Bitmap.copy() documentation

0
content.setDrawingCacheEnabled(false); 

ような何かそれはR.id.root後に呼び出されるべきではその親から削除されます。この方法はメモリを節約します。

関連する問題