From a02fb1308cb520acd445212530770d9ed4ae2875 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Mon, 5 Oct 2015 14:31:30 +0300 Subject: [PATCH] Adapted to drawing context changes --- .../Rendering/DrawingContext.cs | 56 +++++++++++-------- .../Perspex.Android/Rendering/PerspexView.cs | 4 +- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Android/Perspex.Android/Rendering/DrawingContext.cs b/src/Android/Perspex.Android/Rendering/DrawingContext.cs index cf4fa28457..7f0c3c7dc3 100644 --- a/src/Android/Perspex.Android/Rendering/DrawingContext.cs +++ b/src/Android/Perspex.Android/Rendering/DrawingContext.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reactive.Disposables; using Android.Graphics; @@ -12,11 +13,22 @@ using ATextAlign = Android.Graphics.Paint.Align; namespace Perspex.Android.Rendering { - public class DrawingContext : IDrawingContext + public class DrawingContext : IDrawingContextImpl { private static readonly BrushImpl FallbackBrush = new SolidColorBrushImpl(new SolidColorBrush(Colors.Magenta)); - private float _currentOpacity = 1.0f; + private double _currentOpacity = 1.0f; + + private double CurrentOpacity + { + get { return _currentOpacity; } + set + { + _currentOpacity = value; + _nativebrush.Alpha = (int) (_currentOpacity*255); + } + } + private TextPaint _nativebrush; public Canvas Canvas; @@ -32,10 +44,17 @@ namespace Perspex.Android.Rendering _nativebrush = null; } - public Matrix CurrentTransform + Matrix _currentTransform = Matrix.Identity; + public Matrix Transform { - get { return Canvas.Matrix.ToPerspex(); } - set { Canvas.Matrix = value.ToAndroidGraphics(); } + get { return _currentTransform; } + set + { + if(_currentTransform == value) + return; + _currentTransform = value; + Canvas.Matrix = value.ToAndroidGraphics(); + } } public void DrawImage(IBitmap source, double opacity, Rect sourceRect, Rect destRect) @@ -125,33 +144,24 @@ namespace Perspex.Android.Rendering } } - public IDisposable PushClip(Rect clip) + public void PushClip(Rect clip) { Canvas.Save(); Canvas.ClipBounds.Set(clip.ToAndroidGraphics()); - return Disposable.Create(() => Canvas.Restore()); + } - public IDisposable PushOpacity(double opacity) - { - var previous = _currentOpacity; - _currentOpacity = (float) opacity; - _nativebrush.Alpha = (int) _currentOpacity; + public void PopClip() => Canvas.Restore(); - return Disposable.Create(() => - { - _currentOpacity = previous; - _nativebrush.Alpha = (int) _currentOpacity; - }); + Stack _opacityStack = new Stack(); + public void PushOpacity(double opacity) + { + _opacityStack.Push(_currentOpacity); + CurrentOpacity = CurrentOpacity*opacity; } - public IDisposable PushTransform(Matrix matrix) - { - var oldMatrix = CurrentTransform; - CurrentTransform = oldMatrix*matrix; + public void PopOpacity() => CurrentOpacity = _opacityStack.Pop(); - return Disposable.Create(() => { CurrentTransform = oldMatrix; }); - } private Path RoundRectPath(Rect rc, float radius) { diff --git a/src/Android/Perspex.Android/Rendering/PerspexView.cs b/src/Android/Perspex.Android/Rendering/PerspexView.cs index 1ff1794f4c..659047e631 100644 --- a/src/Android/Perspex.Android/Rendering/PerspexView.cs +++ b/src/Android/Perspex.Android/Rendering/PerspexView.cs @@ -35,9 +35,9 @@ namespace Perspex.Android.Rendering IntPtr IPlatformHandle.Handle => Handle; public string HandleDescriptor => "Perspex View"; - public IDrawingContext CreateDrawingContext() + public Media.DrawingContext CreateDrawingContext() { - return new DrawingContext(); + return new Media.DrawingContext(new DrawingContext()); } public void Resize(int width, int height)