Esempio n. 1
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();
                    }
                }
            }
        }
        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();
                }
            }
        }
Esempio n. 3
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");
                }
            }
        }
Esempio n. 4
0
        public override void DrawLayer(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            var bitmap = Bitmap;

            if (bitmap == null)
            {
                return;
            }
            _paint.Alpha = parentAlpha;
            canvas.Save();
            canvas.Concat(parentMatrix);
            RectExt.Set(ref _src, 0, 0, PixelWidth, PixelHeight);
            RectExt.Set(ref _dst, 0, 0, (int)(PixelWidth * _density), (int)(PixelHeight * _density));
            canvas.DrawBitmap(bitmap, _src, _dst, _paint);
            canvas.Restore();
        }
Esempio n. 5
0
        public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            canvas.Save();
            if (!_lottieDrawable.UseTextGlyphs())
            {
                canvas.SetMatrix(parentMatrix);
            }
            var documentData = _textAnimation.Value;

            if (!_composition.Fonts.TryGetValue(documentData.FontName, out var font))
            {
                // Something is wrong.
                return;
            }

            _fillPaint.Color   = _colorAnimation?.Value ?? documentData.Color;
            _strokePaint.Color = _strokeAnimation?.Value ?? documentData.StrokeColor;
            if (_strokeWidthAnimation?.Value != null)
            {
                _strokePaint.StrokeWidth = _strokeWidthAnimation.Value.Value;
            }
            else
            {
                var parentScale = Utils.GetScale(parentMatrix);
                _strokePaint.StrokeWidth = documentData.StrokeWidth * _composition.DpScale * parentScale;
            }

            if (_lottieDrawable.UseTextGlyphs())
            {
                DrawTextGlyphs(documentData, parentMatrix, font, canvas);
            }
            else
            {
                DrawTextWithFont(documentData, font, parentMatrix, canvas);
            }

            canvas.Restore();
        }