【问题标题】:Saving bitmap from layout that contains views从包含视图的布局中保存位图
【发布时间】:2014-09-19 13:09:18
【问题描述】:

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

我希望实现与这些示例类似的东西,Create image from view/screen in AndroidConvert frame layout into image and save it

我遇到的问题与 GetDrawingCache() 和 DrawingCacheEnabled() 等方法有关。 似乎我无法在布局上调用它们。

这里是错误

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

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

我目前正在使用 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>

【问题讨论】:

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


【解决方案1】:

您可以像这样将布局制作为位图

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2020-07-01
    • 2013-09-22
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多