Exemple #1
0
        private void OnPaintSurfaceDisabled(SKPaintSurfaceEventArgs e)
        {
            if (Source == null)
            {
                if (_bitmapDisabled == null || _centerDisabled == null)
                {
                    var result = BuildBitmap("NPImage.Resources.disabled.9.png");
                    _bitmapDisabled = result.bitmap;
                    _centerDisabled = result.center;
                }
            }
            else
            {
                if (_bitmapDisabled == null || _centerDisabled == null)
                {
                    OnPaintSurfaceDefault(e);
                    return;
                }
            }

            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.DrawBitmapNinePatch(_bitmapDisabled, _centerDisabled, new SKRect(0, 0, e.Info.Width, e.Info.Height));
        }
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            SKRectI centerRect = new SKRectI(100, 100, 400, 400);

            canvas.DrawBitmapNinePatch(FiveByFiveBitmap, centerRect, info.Rect);
        }
Exemple #3
0
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.White);

            using (var stream = new SKManagedStream(SampleMedia.Images.NinePatch))
                using (var bitmap = SKBitmap.Decode(stream))
                {
                    var patchCenter = new SKRectI(33, 33, 256 - 33, 256 - 33);

                    // 2x3 for portrait, or 3x2 for landscape
                    var land   = width > height;
                    var min    = land ? Math.Min(width / 3f, height / 2f) : Math.Min(width / 2f, height / 3f);
                    var wide   = SKRect.Inflate(SKRect.Create(0, land ? min : (min * 2f), min * 2f, min), -20, -20);
                    var tall   = SKRect.Inflate(SKRect.Create(land ? (min * 2f) : min, 0, min, min * 2f), -20, -20);
                    var square = SKRect.Inflate(SKRect.Create(0, 0, min, min), -20, -20);
                    var text   = SKRect.Create(land ? min : 0, land ? 0 : min, min, min / 5f);
                    text.Offset(text.Width / 2f, text.Height * 1.5f);
                    text.Right = text.Left;

                    // draw the bitmaps
                    canvas.DrawBitmapNinePatch(bitmap, patchCenter, square);
                    canvas.DrawBitmapNinePatch(bitmap, patchCenter, tall);
                    canvas.DrawBitmapNinePatch(bitmap, patchCenter, wide);

                    // describe what we see
                    using (var paint = new SKPaint())
                    {
                        paint.IsAntialias = true;
                        paint.TextAlign   = SKTextAlign.Center;
                        paint.TextSize    = text.Height * 0.75f;

                        canvas.DrawText("The corners", text.Left, text.Top, paint);
                        text.Offset(0, text.Height);
                        canvas.DrawText("should always", text.Left, text.Top, paint);
                        text.Offset(0, text.Height);
                        canvas.DrawText("be square", text.Left, text.Top, paint);
                    }
                }
        }
Exemple #4
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            if (_bitmap == null || _center == null)
            {
                var result = BuildBitmap("NPImage.Resources.default.9.png");
                _bitmap = result.bitmap;
                _center = result.center;
            }

            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.DrawBitmapNinePatch(_bitmap, _center, new SKRect(0, 0, e.Info.Width, e.Info.Height));
        }
Exemple #5
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();
            canvas.DrawBitmapNinePatch(buttonPressed ? bitmapPressed : bitmapReleased,
                                       centerRect, info.Rect);

            SKRect textBounds = new SKRect();

            textPaint.MeasureText(Text, ref textBounds);
            float xText = info.Width / 2 - textBounds.MidX;
            float yText = info.Height / 2 - textBounds.MidY;

            canvas.DrawText(Text, xText, yText, textPaint);
        }
Exemple #6
0
        private void SKCanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            System.Diagnostics.Trace.WriteLine($"SKCanvasView_PaintSurface {e.Info.Width}/{e.Info.Height} {resourceBitmap?.Width}/{resourceBitmap?.Height}");

            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear();

            if (resourceBitmap != null)
            {
                //canvas.DrawBitmap(resourceBitmap, new SKRect(0, 0, e.Info.Width, e.Info.Height));

                System.Diagnostics.Debug.WriteLine($"外周を切り取る");
                var croppedWidth  = resourceBitmap.Width - 2;
                var croppedHeight = resourceBitmap.Height - 2;
                var croppedBitmap = new SKBitmap(croppedWidth, croppedHeight);
                var croppedDst    = new SKRect(0, 0, croppedWidth, croppedHeight);
                var croppedSrc    = new SKRect(1, 1, resourceBitmap.Width - 1, resourceBitmap.Height - 1);
                using (var croppedCanvas = new SKCanvas(croppedBitmap))
                {
                    croppedCanvas.DrawBitmap(resourceBitmap, croppedSrc, croppedDst);
                }

                System.Diagnostics.Debug.WriteLine($"中央のX座標を調べる");
                var centerXs = new List <int>();
                for (int i = 0; i < resourceBitmap.Width; i++)
                {
                    var color = resourceBitmap.GetPixel(i, 0);
                    System.Diagnostics.Debug.WriteLine($"{i} {color.Alpha} {color.Red} {color.Green} {color.Blue} {color.Alpha != 0}");
                    if (i != 0 && color.Alpha != 0)
                    {
                        centerXs.Add(i - 1);
                    }
                }

                System.Diagnostics.Debug.WriteLine($"中央のY座標を調べる");
                var centerYs = new List <int>();
                for (int i = 0; i < resourceBitmap.Height; i++)
                {
                    var color = resourceBitmap.GetPixel(0, i);
                    System.Diagnostics.Debug.WriteLine($"{i} {color.Alpha} {color.Red} {color.Green} {color.Blue} {color.Alpha != 0}");
                    if (i != 0 && color.Alpha != 0)
                    {
                        centerYs.Add(i - 1);
                    }
                }

                System.Diagnostics.Debug.WriteLine($"中央の座標を決定する");
                int left   = (centerXs.Count > 0) ? centerXs.Min() : 0;
                int top    = (centerYs.Count > 0) ? centerYs.Min() : 0;
                int right  = (centerXs.Count > 0) ? centerXs.Max() : 0;
                int bottom = (centerYs.Count > 0) ? centerYs.Max() : 0;
                System.Diagnostics.Debug.WriteLine($"{left} {top} {right} {bottom}");

                // 調整
                if (left == right)
                {
                    right = left + 1;
                }
                if (top == bottom)
                {
                    bottom = top + 1;
                }

                System.Diagnostics.Debug.WriteLine($"切り取った画像を貼る");
                var dst    = new SKRect(0, 0, e.Info.Width, e.Info.Height);
                var center = new SKRectI(left, top, right, bottom);
                canvas.DrawBitmapNinePatch(croppedBitmap, center, dst);
            }
        }