0

I cannot seem to save a layout and its view components as a bitmap.

I am looking to implement something similar to these example, Create image from view/screen in Android , Convert frame layout into image and save it

The problem I am having is in regards to methods like GetDrawingCache(), and DrawingCacheEnabled(). It appears as if I cannot call them on a layout.

Here are the ERRORS

  1. Error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement (CS0201) (APPNAME)

  2. Error CS1501: No overload for method 'GetDrawingCache' takes 0 arguments (CS1501) (APPNAME)

I am currently coding in Monodroid/Xamarin. So the methods will seem a little different, but maybe someone can provide some insight.

ImageActivity

Here I am adding the views programmatically to a layout.

protected override void OnCreate (Bundle savedInstanceState)
{
    base.OnCreate (savedInstanceState);
    image = new ImageView (this);
    //image = FindViewById<ImageView> (Resource.Id.imageView1);
    //this.image.SetScaleType (ImageView.ScaleType.CenterInside);
    //this.image.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
    SetContentView (Resource.Layout.Image);
    m_Layout = FindViewById<FrameLayout> (Resource.Id.editable_layout);
    m_Layout.AlwaysDrawnWithCacheEnabled;

    this.path = (savedInstanceState ?? Intent.Extras).GetString ("path");
    Title = System.IO.Path.GetFileName (this.path);

    Cleanup ();
    DecodeBitmapAsync (path, 400, 400).ContinueWith (t => {
        image = FindViewById<ImageView> (Resource.Id.imageView1);
        image.SetImageBitmap (this.bitmap = t.Result);
    }, TaskScheduler.FromCurrentSynchronizationContext ());

    btnAddImage1 = FindViewById<ImageButton> (Resource.Id.button1);
    btnAddImage2 = FindViewById<ImageButton> (Resource.Id.button2);
    btnAddImage2 = FindViewById<ImageButton> (Resource.Id.imageButton2);

    btnAddImage1.Click += (sender, e) => { 
        CustomImageView bitmapView = new CustomImageView (this, Resource.Drawable.big85);   
        m_Layout.AddView (bitmapView);
    };

    btnAddImage2.Click += (sender, e) => { 
        CustomImageView bitmapView = new CustomImageView (this, Resource.Drawable.curled5); 
        m_Layout.AddView (bitmapView);

    };
    btnConfirm.Click += (sender, e) => { 
        createBitmap();
    };

}

public void createBitmap(){
    m_Layout.DrawingCacheEnabled;
    m_Layout.BuildDrawingCache ();
    Bitmap m_Bitmap = Bitmap.CreateBitmap(m_Layout.GetDrawingCache());
}

XML for ImageActivity

<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:background="@color/gray_35"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/root_layout">
    <LinearLayout
        p1:orientation="horizontal"
        p1:minWidth="25px"
        p1:background="@drawable/red_noise_background"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="50dp"
        p1:id="@+id/linearLayoutActionBar">
        <ImageButton
            p1:src="@drawable/ic_navigation_previous_item"
            p1:layout_width="wrap_content"
            p1:layout_height="fill_parent"
            p1:background="@color/transparent"
            p1:id="@+id/imageButton1" />
        <LinearLayout
            p1:layout_width="2dp"
            p1:layout_height="fill_parent"
            p1:background="@color/pixagramdark_red" />
        <TextView
            p1:text="Pixagram"
            p1:textColor="@color/white"
            p1:layout_weight="1"
            p1:textSize="24sp"
            p1:gravity="center"
            p1:layout_width="fill_parent"
            p1:layout_height="fill_parent"
            p1:id="@+id/textView1" />
        <LinearLayout
            p1:layout_width="2dp"
            p1:layout_height="fill_parent"
            p1:background="@color/pixagramdark_red" />
        <ImageButton
            p1:background="@color/transparent"
            p1:src="@drawable/ic_navigation_accept"
            p1:layout_width="wrap_content"
            p1:layout_height="fill_parent"
            p1:id="@+id/imageButton2" />
    </LinearLayout>
    <LinearLayout
        p1:layout_width="fill_parent"
        p1:layout_height="2dp"
        p1:background="@color/pixagramdark_red" />
    <FrameLayout
        p1:layout_weight="1"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/editable_layout">
        <ImageView
            p1:src="@android:drawable/ic_menu_gallery"
            p1:layout_width="match_parent"
            p1:layout_height="match_parent"
            p1:id="@+id/imageView1" />
    </FrameLayout>
    <LinearLayout
        p1:layout_width="fill_parent"
        p1:layout_height="2dp"
        p1:background="@color/pixagramdark_red" />
    <LinearLayout
        p1:orientation="vertical"
        p1:minWidth="25px"
        p1:background="@drawable/red_noise_background"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="75dp"
        p1:id="@+id/linearLayout1">
        <HorizontalScrollView
            p1:layout_gravity="bottom"
            p1:gravity="bottom"
            p1:layout_width="fill_parent"
            p1:layout_height="fill_parent"
            p1:id="@+id/horizontalScrollView1">
            <LinearLayout
                p1:orientation="horizontal"
                p1:layout_width="fill_parent"
                p1:layout_height="fill_parent"
                p1:id="@+id/linearLayout1">
                <ImageButton
                    p1:text="Button"
                    p1:layout_weight="1"
                    p1:layout_width="75dp"
                    p1:background="@color/transparent"
                    p1:layout_height="fill_parent"
                    p1:src="@drawable/big85"
                    p1:id="@+id/button1" />
                <ImageButton
                    p1:text="Button"
                    p1:background="@color/transparent"
                    p1:layout_weight="1"
                    p1:src="@drawable/curled5"
                    p1:layout_width="75dp"
                    p1:layout_height="fill_parent"
                    p1:id="@+id/button2" />
                <ImageButton
                    p1:text="Button"
                    p1:background="@color/transparent"
                    p1:layout_weight="1"
                    p1:layout_width="75dp"
                    p1:src="@drawable/curve21"
                    p1:layout_height="fill_parent"
                    p1:id="@+id/button3" />
                <ImageButton
                    p1:text="Button"
                    p1:layout_weight="1"
                    p1:src="@drawable/facial1"
                    p1:background="@color/transparent"
                    p1:layout_width="75dp"
                    p1:layout_height="fill_parent"
                    p1:id="@+id/button4" />
                <ImageButton
                    p1:text="Button"
                    p1:layout_weight="1"
                    p1:layout_width="75dp"
                    p1:src="@drawable/horseman1"
                    p1:background="@color/transparent"
                    p1:layout_height="fill_parent"
                    p1:id="@+id/button5" />
                <ImageButton
                    p1:text="Button"
                    p1:src="@drawable/thin20"
                    p1:layout_weight="1"
                    p1:background="@color/transparent"
                    p1:layout_width="75dp"
                    p1:layout_height="fill_parent"
                    p1:id="@+id/button6" />
            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</LinearLayout>
Community
  • 1
  • 1
Coova
  • 1,818
  • 5
  • 36
  • 63
  • Probably this link can help you: http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android – Ciro Rizzo Sep 19 '14 at 13:16

1 Answers1

0

you can make a Layout as an bitmap like this

final LinearLayout li = (LinearLayout) grid.findViewById(R.id.lin);
li.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

li.layout(0, 0, li.getMeasuredWidth(), li.getMeasuredHeight());

li.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(li.getDrawingCache());
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
vishal jangid
  • 2,967
  • 16
  • 22