2013-03-05 48 views
1

我有這樣的FrameLayout在我的LinearLayout:機器人轉換的FrameLayout來繪製

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/desktopsFramelayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="none" 
     android:src="@drawable/accept" /> 

</FrameLayout> 

然後在代碼中,我試圖將其轉換在繪製對象是這樣的:

FrameLayout desktopFrameLayout = (FrameLayout)findViewById(R.id.desktopsFramelayout); 

     desktopFrameLayout.setDrawingCacheEnabled(true); 
     desktopFrameLayout.buildDrawingCache();  
     Bitmap bitmap = desktopFrameLayout.getDrawingCache(); 

bitmapnull,爲什麼? ?

回答

0

可能是因爲您剛剛啓用了DrawingCache,但FrameLayout沒有任何時間來構建它。

嘗試在xml中分配drawingCacheEnabled並調用desktopFrameLayout.getDrawingCache();

之後它可能會在您請求時生成緩存。

如果不在生命週期的後期嘗試它。

0
Bitmap b = null; 
desktopFrameLayout.post(new Runnable() { 
    desktopFrameLayout.setDrawingCacheEnabled(true); 
    desktopFrameLayout.buildDrawingCache();  
    b = desktopFrameLayout.getDrawingCache(); 
}); 
它返回null