Skip to content Skip to sidebar Skip to footer

Xamarin.andoid: Merge Imageview With Canvasview

I am using the FingerPaintCanvasView from this xamarin sample. I am working with 2 layers. The first layer is an ImageView i want to draw on. The second layer is the PaintCanvasVie

Solution 1:

Below is the code, I have add comments in it:

publicclassMainActivity : Activity
    {
        private Rect mSrcRect, mDestRect;
        protected override voidOnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            //your background picture ---markImageImageViewBitmapbackground= BitmapFactory.DecodeResource(Resources, Resource.Drawable.pause);
            //your foreground picture ---FingerPaintCanvasViewBitmapforeground= BitmapFactory.DecodeResource(Resources, Resource.Drawable.play);

            Paintp=newPaint();
            p.SetXfermode(newPorterDuffXfermode(PorterDuff.Mode.SrcOver));

            //use background to create a canvasBitmapworkingBitmap= Bitmap.CreateBitmap(background);
            BitmapmutableBitmap= workingBitmap.Copy(Bitmap.Config.Argb8888, true);
            Canvasc=newCanvas(mutableBitmap);

            intmBWith= background.Width;
            intmBHeight= background.Height;

            intmFWith= foreground.Width;
            intmFHeight= foreground.Height;
            mSrcRect = newRect(0, 0, mBWith, mBHeight);
            mDestRect = newRect(0, 0, mFWith, mFHeight);

            //draw foreground on the backaground, then they will be single bitmap
            c.DrawBitmap(foreground, mSrcRect, mDestRect, p);
            ImageViewimageView= FindViewById<ImageView>(Resource.Id.iv);
            imageView.SetImageBitmap(mutableBitmap);
        }
    }

And I have provide the demo on github.

Update:

Change Bitmap.Config.Argb4444 to Bitmap.Config.Argb8888.

Post a Comment for "Xamarin.andoid: Merge Imageview With Canvasview"