Android 如何在线性布局上绘制(手指绘制)后将其保存为图像

Android 如何在线性布局上绘制(手指绘制)后将其保存为图像,android,android-drawable,Android,Android Drawable,我是android新手,正在创建一个小应用程序,我想从gallery中获取一个图像,然后在上面绘制(使用手指画)后保存它 我遇到的问题是在绘图之后,当我试图保存图像时,它只保存黑色背景的绘图。从库中拍摄的图像在保存的图像中不可见 在图像上绘制并保存后: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

我是android新手,正在创建一个小应用程序,我想从gallery中获取一个图像,然后在上面绘制(使用手指画)后保存它

我遇到的问题是在绘图之后,当我试图保存图像时,它只保存黑色背景的绘图。从库中拍摄的图像在保存的图像中不可见

在图像上绘制并保存后

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFCCCCCC"
 android:orientation="vertical"
 tools:context=".ImageActivity" >

 <!-- layout for displaying save button -->
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/my_save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
 </LinearLayout>

 <!-- layout that displays image taken from gallery and allows to finger paint-->
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:id="@+id/my_view_drawing_pad1"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/my_view_drawing_pad"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
 </LinearLayout>
</LinearLayout>

保存的实际图像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFCCCCCC"
 android:orientation="vertical"
 tools:context=".ImageActivity" >

 <!-- layout for displaying save button -->
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/my_save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
 </LinearLayout>

 <!-- layout that displays image taken from gallery and allows to finger paint-->
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:id="@+id/my_view_drawing_pad1"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/my_view_drawing_pad"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
 </LinearLayout>
</LinearLayout>

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFCCCCCC"
 android:orientation="vertical"
 tools:context=".ImageActivity" >

 <!-- layout for displaying save button -->
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >
    <ImageButton
        android:id="@+id/my_save_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="@string/save"
        android:src="@drawable/save" />
 </LinearLayout>

 <!-- layout that displays image taken from gallery and allows to finger paint-->
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:id="@+id/my_view_drawing_pad1"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/my_view_drawing_pad"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
 </LinearLayout>
</LinearLayout>

你能告诉我,我错过了什么吗。为什么我的图像以黑色背景保存?

拍摄根布局的屏幕截图,将其转换为位图图像,并按如下方式保存:

 LinearLayout v = (LinearLayout) findViewById(R.id.root_layout); 

    v.setDrawingCacheEnabled(true);

    v.buildDrawingCache(true);

    saveBm = Bitmap.createBitmap(v.getDrawingCache());

    v.setDrawingCacheEnabled(false);

将此位图图像保存到任何需要的位置。

谢谢。请添加LinearLayout vi=(LinearLayout)findViewById(R.id.my\u view\u drawing\u pad1);在你答案的顶部。这可能会帮助像我这样的新手。