예제 #1
0
        public void Draw(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("GradientFillContent.Draw");
            _path.Reset();
            for (var i = 0; i < _paths.Count; i++)
            {
                _path.AddPath(_paths[i].Path, parentMatrix);
            }

            _path.ComputeBounds(out _boundsRect);

            Shader shader;

            if (_type == GradientType.Linear)
            {
                shader = LinearGradient;
            }
            else
            {
                shader = RadialGradient;
            }
            _shaderMatrix.Set(parentMatrix);
            shader.LocalMatrix = _shaderMatrix;
            _paint.Shader      = shader;

            var alpha = (byte)(parentAlpha / 255f * _opacityAnimation.Value / 100f * 255);

            _paint.Alpha = alpha;

            canvas.DrawPath(_path, _paint);
            LottieLog.EndSection("GradientFillContent.Draw");
        }
예제 #2
0
        public CanvasCommandList GetCanvasImage(ICanvasResourceCreator resourceCreator, float scaleX, float scaleY)
        {
            lock (this)
            {
                var commandList = new CanvasCommandList(resourceCreator);
                using (var session = commandList.CreateDrawingSession())
                {
                    var width  = _composition.Bounds.Width * scaleX;
                    var height = _composition.Bounds.Height * scaleY;
                    if (_bitmapCanvas == null || _bitmapCanvas.Width < width || _bitmapCanvas.Height < height)
                    {
                        _bitmapCanvas?.Dispose();
                        _bitmapCanvas = new BitmapCanvas(width, height);
                    }

                    using (_bitmapCanvas.CreateSession(resourceCreator.Device, (float)width,
                                                       (float)height, session))
                    {
                        _bitmapCanvas.Clear(Colors.Transparent);
                        LottieLog.BeginSection("Drawable.Draw");
                        if (_compositionLayer == null)
                        {
                            return(null);
                        }

                        _matrix.Reset();
                        _matrix = MatrixExt.PreScale(_matrix, scaleX, scaleY);
                        _compositionLayer.Draw(_bitmapCanvas, _matrix, _alpha);
                        LottieLog.EndSection("Drawable.Draw");
                    }
                }

                return(commandList);
            }
        }
예제 #3
0
        public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("CompositionLayer.Draw");
            canvas.GetClipBounds(out _originalClipRect);
            RectExt.Set(ref _newClipRect, 0, 0, LayerModel.PreCompWidth, LayerModel.PreCompHeight);
            parentMatrix.MapRect(ref _newClipRect);

            for (var i = _layers.Count - 1; i >= 0; i--)
            {
                var nonEmptyClip = true;
                if (!_newClipRect.IsEmpty)
                {
                    nonEmptyClip = canvas.ClipRect(_newClipRect);
                }
                if (nonEmptyClip)
                {
                    var layer = _layers[i];
                    layer.Draw(canvas, parentMatrix, parentAlpha);
                }
            }
            if (!_originalClipRect.IsEmpty)
            {
                canvas.ClipReplaceRect(_originalClipRect);
            }
            LottieLog.EndSection("CompositionLayer.Draw");
        }
예제 #4
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        private void CanvasControlOnDraw(ICanvasAnimatedControl canvasControl, CanvasAnimatedDrawEventArgs args)
        {
            lock (this)
            {
                using (_bitmapCanvas.CreateSession(canvasControl.Device, canvasControl.Size.Width, canvasControl.Size.Height, args.DrawingSession))
                {
                    _bitmapCanvas.Clear(Colors.Transparent);
                    LottieLog.BeginSection("Drawable.Draw");
                    if (_compositionLayer == null)
                    {
                        return;
                    }

                    var   scale      = _scale;
                    float extraScale = 1f;

                    float maxScale = GetMaxScale(_bitmapCanvas);
                    if (scale > maxScale)
                    {
                        scale      = maxScale;
                        extraScale = _scale / scale;
                    }

                    if (extraScale > 1)
                    {
                        // This is a bit tricky...
                        // We can't draw on a canvas larger than ViewConfiguration.get(context).getScaledMaximumDrawingCacheSize()
                        // which works out to be roughly the size of the screen because Android can't generate a
                        // bitmap large enough to render to.
                        // As a result, we cap the scale such that it will never be wider/taller than the screen
                        // and then only render in the top left corner of the canvas. We then use extraScale
                        // to scale up the rest of the scale. However, since we rendered the animation to the top
                        // left corner, we need to scale up and translate the canvas to zoom in on the top left
                        // corner.
                        _bitmapCanvas.Save();
                        float halfWidth        = (float)_composition.Bounds.Width / 2f;
                        float halfHeight       = (float)_composition.Bounds.Height / 2f;
                        float scaledHalfWidth  = halfWidth * scale;
                        float scaledHalfHeight = halfHeight * scale;
                        _bitmapCanvas.Translate(
                            Scale * halfWidth - scaledHalfWidth,
                            Scale * halfHeight - scaledHalfHeight);
                        _bitmapCanvas.Scale(extraScale, extraScale, scaledHalfWidth, scaledHalfHeight);
                    }

                    _matrix.Reset();
                    _matrix = MatrixExt.PreScale(_matrix, scale, scale);
                    _compositionLayer.Draw(_bitmapCanvas, _matrix, _alpha);
                    LottieLog.EndSection("Drawable.Draw");

                    if (extraScale > 1)
                    {
                        _bitmapCanvas.Restore();
                    }
                }
            }
        }
예제 #5
0
        private static void Draw(CanvasDevice device, BitmapCanvas bitmapCanvas, CompositionLayer compositionLayer, Rect bounds, float scale, byte alpha, Matrix3X3 matrix, double width, double height, CanvasDrawingSession canvasDrawingSession)
        {
            using (bitmapCanvas.CreateSession(device, width, height, canvasDrawingSession))
            {
                bitmapCanvas.Clear(Colors.Transparent);
                LottieLog.BeginSection("Drawable.Draw");
                if (compositionLayer == null)
                {
                    return;
                }

                var   localScale = scale;
                float extraScale = 1f;

                float maxScale = GetMaxScale(bitmapCanvas, bounds);
                if (localScale > maxScale)
                {
                    localScale = maxScale;
                    extraScale = scale / localScale;
                }

                if (extraScale > 1)
                {
                    // This is a bit tricky...
                    // We can't draw on a canvas larger than ViewConfiguration.get(context).getScaledMaximumDrawingCacheSize()
                    // which works out to be roughly the size of the screen because Android can't generate a
                    // bitmap large enough to render to.
                    // As a result, we cap the scale such that it will never be wider/taller than the screen
                    // and then only render in the top left corner of the canvas. We then use extraScale
                    // to scale up the rest of the scale. However, since we rendered the animation to the top
                    // left corner, we need to scale up and translate the canvas to zoom in on the top left
                    // corner.
                    bitmapCanvas.Save();
                    float halfWidth        = (float)bounds.Width / 2f;
                    float halfHeight       = (float)bounds.Height / 2f;
                    float scaledHalfWidth  = halfWidth * localScale;
                    float scaledHalfHeight = halfHeight * localScale;
                    bitmapCanvas.Translate(
                        scale * halfWidth - scaledHalfWidth,
                        scale * halfHeight - scaledHalfHeight);
                    bitmapCanvas.Scale(extraScale, extraScale, scaledHalfWidth, scaledHalfHeight);
                }

                matrix.Reset();
                matrix = MatrixExt.PreScale(matrix, localScale, localScale);
                compositionLayer.Draw(bitmapCanvas, matrix, alpha);
                LottieLog.EndSection("Drawable.Draw");

                if (extraScale > 1)
                {
                    bitmapCanvas.Restore();
                }
            }
        }
예제 #6
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        private void CanvasControlOnDraw(ICanvasAnimatedControl canvasControl, CanvasAnimatedDrawEventArgs args)
        {
            lock (this)
            {
                using (_bitmapCanvas.CreateSession(canvasControl.Device, canvasControl.Size.Width, canvasControl.Size.Height, args.DrawingSession))
                {
                    _bitmapCanvas.Clear(Colors.Transparent);
                    LottieLog.BeginSection("Drawable.Draw");
                    if (_compositionLayer == null)
                    {
                        return;
                    }
                    var   scale         = _scale;
                    float extraScale    = 1f;
                    var   hasExtraScale = false;
                    float maxScale      = GetMaxScale(_bitmapCanvas);
                    if (_compositionLayer.HasMatte() || _compositionLayer.HasMasks())
                    {
                        // Since we can only scale up the animation so much before masks and mattes get clipped, we
                        // may have to scale the canvas to fake the rest. This isn't a problem for software rendering
                        // but hardware accelerated scaling is rasterized so it will appear pixelated.
                        extraScale = scale / maxScale;
                        scale      = Math.Min(scale, maxScale);
                        // This check fixes some floating point rounding issues.
                        hasExtraScale = extraScale > 1.001f;
                    }

                    if (hasExtraScale)
                    {
                        _bitmapCanvas.Save();
                        // This is extraScale ^2 because what happens is when the scale increases, the intrinsic size
                        // of the view increases. That causes the drawable to keep growing even though we are only
                        // rendering to the size of the view in the top left quarter, leaving the rest blank.
                        // The first scale by extraScale scales up the canvas so that we are back at the original
                        // size. The second extraScale is what actually has the scaling effect.
                        float extraScaleSquared = extraScale * extraScale;
                        int   px = (int)(_composition.Bounds.Width * scale / 2f);
                        int   py = (int)(_composition.Bounds.Height * scale / 2f);
                        _bitmapCanvas.Scale(extraScaleSquared, extraScaleSquared, px, py);
                    }

                    _matrix.Reset();
                    _matrix = MatrixExt.PreScale(_matrix, scale, scale);
                    _compositionLayer.Draw(_bitmapCanvas, _matrix, _alpha);
                    if (hasExtraScale)
                    {
                        _bitmapCanvas.Restore();
                    }
                    LottieLog.EndSection("Drawable.Draw");
                }
            }
        }
예제 #7
0
        public virtual void Draw(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("FillContent.Draw");
            _paint.Color = _colorAnimation.Value;
            var alpha = (byte)(parentAlpha / 255f * _opacityAnimation.Value / 100f * 255);

            _paint.Alpha = alpha;

            _path.Reset();
            for (var i = 0; i < _paths.Count; i++)
            {
                _path.AddPath(_paths[i].Path, parentMatrix);
            }

            canvas.DrawPath(_path, _paint);
            LottieLog.EndSection("FillContent.Draw");
        }
예제 #8
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        public void Draw(BitmapCanvas canvas)
        {
            LottieLog.BeginSection("Drawable.Draw");
            if (_compositionLayer == null)
            {
                return;
            }
            var scale = _scale;

            if (_compositionLayer.HasMatte())
            {
                scale = Math.Min(_scale, GetMaxScale(canvas));
            }

            _matrix.Reset();
            _matrix = MatrixExt.PreScale(_matrix, scale, scale);
            _compositionLayer.Draw(canvas, _matrix, _alpha);
            LottieLog.EndSection("Drawable.Draw");
        }
예제 #9
0
        internal static void ApplyTrimPathIfNeeded(Path path, float startValue, float endValue, float offsetValue)
        {
            LottieLog.BeginSection("applyTrimPathIfNeeded");
            PathMeasure.SetPath(path);

            var length = PathMeasure.Length;

            if (startValue == 1f && endValue == 0f)
            {
                LottieLog.EndSection("applyTrimPathIfNeeded");
                return;
            }
            if (length == 0f || Math.Abs(endValue - startValue - 1) < .01)
            {
                LottieLog.EndSection("applyTrimPathIfNeeded");
                return;
            }
            var start    = length * startValue;
            var end      = length * endValue;
            var newStart = Math.Min(start, end);
            var newEnd   = Math.Max(start, end);

            var offset = offsetValue * length;

            newStart += offset;
            newEnd   += offset;

            // If the trim path has rotated around the path, we need to shift it back.
            if (newStart >= length && newEnd >= length)
            {
                newStart = MiscUtils.FloorMod(newStart, length);
                newEnd   = MiscUtils.FloorMod(newEnd, length);
            }

            if (newStart < 0)
            {
                newStart = MiscUtils.FloorMod(newStart, length);
            }
            if (newEnd < 0)
            {
                newEnd = MiscUtils.FloorMod(newEnd, length);
            }

            // If the start and end are equals, return an empty path.
            if (newStart == newEnd)
            {
                path.Reset();
                LottieLog.EndSection("applyTrimPathIfNeeded");
                return;
            }

            if (newStart >= newEnd)
            {
                newStart -= length;
            }

            _tempPath.Reset();
            PathMeasure.GetSegment(newStart, newEnd, ref _tempPath, true);

            if (newEnd > length)
            {
                _tempPath2.Reset();
                PathMeasure.GetSegment(0, newEnd % length, ref _tempPath2, true);
                _tempPath.AddPath(_tempPath2);
            }
            else if (newStart < 0)
            {
                _tempPath2.Reset();
                PathMeasure.GetSegment(length + newStart, length, ref _tempPath2, true);
                _tempPath.AddPath(_tempPath2);
            }
            path.Set(_tempPath);
            LottieLog.EndSection("applyTrimPathIfNeeded");
        }