简体   繁体   English

Skia / SkiaSharp - 围绕自己的垂直轴旋转形状

[英]Skia / SkiaSharp - Rotating shapes around their own vertical axis

滑雪 / 滑雪

I generate the scene above using the OnDraw method below:我使用下面的 OnDraw 方法生成上面的场景:

  protected override void OnDraw(SKCanvas canvas, int width, int height)
    {
        int i = 0;
        int step = 0;
        List<SKRect> rects = new List<SKRect>();

        // get the 2D equivalent of the 3D matrix
        var rotationMatrix = rotationView.Matrix;

        // get the properties of the rectangle
        var length = Math.Min(width / 6, height / 6);

        canvas.Clear(EffectMedia.Colors.XamarinLightBlue);

        foreach (var n in numbers)
        {
            var rect = new SKRect(0 + step, 0, 100 + step, 100);
            rects.Add(rect);
            step += 120;
        }

        //var sideHoriz = rotationMatrix.MapPoint(new SKPoint(0, 1)).Y > 0;
        var sideVert = rotationMatrix.MapPoint(new SKPoint(1, 0)).X > 0;

        var paint = new SKPaint
        {
            Color = sideVert ? EffectMedia.Colors.XamarinPurple : EffectMedia.Colors.XamarinGreen,
            Style = SKPaintStyle.Fill,
            IsAntialias = true
        };

        // first do 2D translation to the center of the screen
        canvas.Translate((width - (120 * numbers.Count)) / 2, height / 2);

        // The following line is disabled because it makes the whole canvas rotate!
        // canvas.Concat(ref rotationMatrix);

        foreach (var n in numbers)
        {            
            canvas.RotateDegrees((float)-3);
            canvas.DrawRoundRect(rects[i], 30, 30, paint);

            var shadow = SKShader.CreateLinearGradient(
                    new SKPoint(0, 0), new SKPoint(0, length * 2),
                    new[] { paint.Color.WithAlpha(127), paint.Color.WithAlpha(0) },
                    null,
                    SKShaderTileMode.Clamp);

            var paintShadow = new SKPaint
            {
                Shader = shadow,
                Style = SKPaintStyle.Fill,
                IsAntialias = true,
                BlendMode = SKBlendMode.SoftLight
            };

            foreach (var r in rects)
            {
                r.Offset(0, 105);
                canvas.DrawRoundRect(r, 30, 30, paintShadow);
            }

            i++;
        }
    }

The idea is to make all those rounded boxes rotate (vertically) around their own axis.这个想法是让所有这些圆形框围绕它们自己的轴(垂直)旋转。

I tried using SKPath + Transform, saving&restoring the rotationMatrix and/or the canvas but I can't find a way to have 6 rotating boxes ( canvas.Concat(ref rotationMatrix); makes the whole canvas rotate [*]).我尝试使用 SKPath + Transform,保存并恢复旋转矩阵和/或 canvas 但我找不到拥有 6 个旋转框的方法( canvas.Concat(ref rotationMatrix); 使整个 ZFCC790C72A86190DE1B54 旋转 [9]DDC790C72A86190DE1B54

Do you have any hint on how that can be achieved?您对如何实现这一目标有任何暗示吗?

Note [*]: there's a call to rotationView.RotateYDegrees(5) every X milliseconds to update the rotationMatrix used by OnDraw.注意 [*]:每 X 毫秒调用一次 rotationView.RotateYDegrees(5) 以更新 OnDraw 使用的旋转矩阵。

This is what I'd like to achieve, any hints / directions would be really appreciated... :-)这就是我想要实现的目标,任何提示/方向都将不胜感激...... :-)

在此处输入图像描述

The following piece of code rotates those shapes around their Z-axis:以下代码围绕 Z 轴旋转这些形状:

canvas.Save();
canvas.RotateDegrees(degrees, rects[i].MidX, rects[i].MidY);
canvas.DrawRoundRect(rects[i], 30, 30, paint);
canvas.Restore();

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM