1

我的 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>

然后在代码中我尝试像这样在 Drawable 中转换它:

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

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

bitmapnull为什么???

4

3 回答 3

1

查看此链接并阅读 nininho 的答案: Android View.getDrawingCache return null, only null

我认为它返回 null 因为您的视图具有尺寸 (0,0)

于 2013-03-05T11:53:25.273 回答
0

可能是因为您刚刚启用了 DrawingCache,但 FrameLayout 还没有时间构建它。

尝试在 xml 中分配 drawingCacheEnabled 并调用 desktopFrameLayout.getDrawingCache();

之后,当您要求它时,它可能会构建缓存。

如果不在生命周期的后期尝试。

于 2013-03-05T11:53:17.467 回答
0
Bitmap b = null;
desktopFrameLayout.post(new Runnable() {
    desktopFrameLayout.setDrawingCacheEnabled(true);
    desktopFrameLayout.buildDrawingCache();     
    b = desktopFrameLayout.getDrawingCache();
});
于 2013-03-05T11:57:41.377 回答