从包含视图的布局中保存位图

时间:2014-09-19 13:09:19

标签: android android-layout bitmap xamarin xamarin.android

我似乎无法将布局及其视图组件保存为位图。

我希望实现与这些示例Create image from view/screen in Android类似的内容, Convert frame layout into image and save it

我遇到的问题是GetDrawingCache()和DrawingCacheEnabled()等方法。 看起来好像我不能在布局上调用它们。

以下是错误

  1. 错误CS0201:只能将赋值,调用,递增,递减,等待和新对象表达式用作语句(CS0201)(APPNAME)

  2. 错误CS1501:方法'GetDrawingCache'没有重载需要0个参数(CS1501)(APPNAME)

  3. 我目前正在使用Monodroid / Xamarin进行编码。所以方法看起来会有所不同,但也许有人可以提供一些见解。

    ImageActivity

    这里我将以编程方式将视图添加到布局中。

    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());
    }
    

    用于ImageActivity的XML

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

1 个答案:

答案 0 :(得分:0)

您可以将布局设为像这样的位图

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());