Ejemplo n.º 1
0
        public void Render(IDrawingContextImpl context)
        {
            var finalRenderSurface = context.CreateLayer(_destRect.Size);

            if (finalRenderSurface is null)
            {
                context.Clear(Colors.Aqua);
                return;
            }

            using (var renderSurfaceCtx = finalRenderSurface.CreateDrawingContext(null))
            {
                using (_lottieCanvas.CreateSession(_destRect.Size, finalRenderSurface,
                                                   new DrawingContext(renderSurfaceCtx)))
                {
                    _compositionLayer.Draw(_lottieCanvas, _matrix, 255);
                }
            }

            context.DrawBitmap(RefCountable.Create(finalRenderSurface),
                               1,
                               new Rect(new Point(), finalRenderSurface.PixelSize.ToSize(1)), _destRect);

            finalRenderSurface.Dispose();
        }
Ejemplo n.º 2
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        public override void Render(RenderTarget target)
        {
            lock (this)
            {
                if (_bitmapCanvas == null)
                {
                    return;
                }

                using (_bitmapCanvas.CreateSession(target.Size.Width, target.Size.Height, (DeviceContext)target))
                {
                    _bitmapCanvas.Clear(Color.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();
                        _bitmapCanvas.RestoreAll();
                    }
                }
            }
        }