android将FrameLayout转换为Drawable

时间:2013-03-05 11:47:34

标签: android bitmap android-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>

然后在代码中我尝试将它转换为Drawable,如下所示:

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

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

bitmapnull,为什么???

3 个答案:

答案 0 :(得分:1)

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

我认为它返回null,因为你的视图有维度(0,0)

答案 1 :(得分:0)

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

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

之后,当你提出要求时,它可能会有Cache构建。

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

答案 2 :(得分:0)

Bitmap b = null;
desktopFrameLayout.post(new Runnable() {
    desktopFrameLayout.setDrawingCacheEnabled(true);
    desktopFrameLayout.buildDrawingCache();     
    b = desktopFrameLayout.getDrawingCache();
});
相关问题