Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 像位图一样保存视图,我只得到黑屏_Java_Android_View_Bitmap_Makefile - Fatal编程技术网

Java 像位图一样保存视图,我只得到黑屏

Java 像位图一样保存视图,我只得到黑屏,java,android,view,bitmap,makefile,Java,Android,View,Bitmap,Makefile,我有一个绘图应用程序,我有保存绘制的图片的方法。但我只有一个黑色位图图片与保存按钮。问题在哪里 这是我的xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/all" android:layout_width="match_parent" android:la

我有一个绘图应用程序,我有保存绘制的图片的方法。但我只有一个黑色位图图片与保存按钮。问题在哪里

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".Circle" 
   >

<org.vkedco.mobappdev.draw_touch_drive_00001.krouzky
    android:id="@+id/pntr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:tag="Painter" />



 <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/button4"
    android:layout_marginTop="50dp"
    android:text="value"
    android:textColor="@color/white" />


<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Add" />


<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Delete" />

  <Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Save"
    android:layout_marginTop="45dp"
    android:layout_alignLeft="@+id/button3" />


  <Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Clear"
    android:layout_marginTop="45dp"
    android:layout_alignRight="@+id/button3" />

 <LinearLayout 
     android:orientation="vertical"
     android:layout_height="match_parent"
      android:id="@+id/linearLayout"
     android:layout_width="match_parent" />
保存方法

    public  void save(View v)
    {
        Log.v("log_tag", "Width: " + v.getWidth());
        Log.v("log_tag", "Height: " + v.getHeight());
        if(mBitmap == null)
        {
            mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.RGB_565);
        }
        Canvas canvas = new Canvas(mBitmap);
        try
        {

            FileOutputStream mFileOutStream = new FileOutputStream(mypath);

            v.draw(canvas);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
            mFileOutStream.flush();
            mFileOutStream.close();


        }
        catch(Exception e)
        {
            Log.v("log_tag", e.toString());
        }
    }
我的问题已经解决了,但我有一个小细节。当我更改xml时,我的应用程序会停止运行。对这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".Circle" >

<org.vkedco.mobappdev.draw_touch_drive_00001.krouzky
    android:id="@+id/pntr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:tag="Painter" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Add" />


<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Delete" />

  <Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Save"
    android:layout_marginTop="45dp"
    android:layout_alignLeft="@+id/button3" />


  <Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Clear"
    android:layout_marginTop="45dp"
    android:layout_alignRight="@+id/button3" />

<LinearLayout 
     android:orientation="vertical"
     android:layout_height="match_parent"
      android:id="@+id/linearLayout"
     android:layout_width="match_parent" >

</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button6"
    android:layout_marginTop="21dp"
    android:text="value"
    android:textColor="@color/blue" />

您可以使用


在它之前,你应该打电话

实际上,您正在传递方法
save(v)
中的按钮btn6的视图(v)-传递线性布局对象,该对象是
mcontent
mView
,然后将是
save(mcontent)
,这将解决您的问题

你也可以试试这个方法-

在方法中传递父布局或视图-

 Bitmap file = save(mcontent);

 Bitmap save(View v)
   {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
   }
试试这个: -查找父视图

RelativeLayout holder = (RelativeLayout) findViewById(R.id.all);
-并调用
loadBitmapFromView(holder)


我创建了一个扩展GroupView的cutom组件,并使用此方法将此布局保存为位图

    public Bitmap createBitmap() {
    //Log.d("Pin", "Image W ["+this.getWidth()+"] x H ["+this.getHeight()+"]");
    Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);       
    Canvas c = new Canvas(b);

    this.draw(c);

    return b;
}

希望对你有帮助。如果你想保存你的版面,你应该用你想保存的整个版面来更改它

非常感谢你,可能会重复Wow。这么小的重要细节。谢谢,还有一件小事。有谁能告诉我,为什么我的申请会失败。如果我将xml文件中的TextView移动到LinearLayout下(用于在TextView中保存带有文本的绘制图像)?
public static Bitmap loadBitmapFromView(View v) {
    DisplayMetrics dm = getResources().getDisplayMetrics(); 
    v.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    ((RelativeLayout) v).setGravity(Gravity.CENTER);
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}
    public Bitmap createBitmap() {
    //Log.d("Pin", "Image W ["+this.getWidth()+"] x H ["+this.getHeight()+"]");
    Bitmap b = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);       
    Canvas c = new Canvas(b);

    this.draw(c);

    return b;
}