private void DatabaseVariable_Updated(object sender, Ao3TrackDatabase.VariableUpdatedEventArgs e) { Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { bool.TryParse(e.NewValue, out useBlur); if (useBlur) { if (IsAttachedToWindow) { if (rs == null) { rs = RenderScript.Create(Context); } if (rs != null) { script = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs)); } } } else { script?.Dispose(); script = null; sourceCanvas?.Dispose(); sourceCanvas = null; source?.Dispose(); source = null; blurred?.Dispose(); blurred = null; rs?.Dispose(); rs = null; } if (Element != null) { UpdateBackgroundColor(); } }); }
protected override void Dispose(bool disposing) { if (_isDisposed) { return; } _isDisposed = true; BitmapDrawable bitmapDrawable; if (disposing && Control != null && (bitmapDrawable = (Control.Drawable as BitmapDrawable)) != null) { Bitmap bitmap = bitmapDrawable.Bitmap; if (bitmap != null) { bitmap.Recycle(); bitmap.Dispose(); rs.Dispose(); script.Dispose(); } } base.Dispose(disposing); }
public static Bitmap CreateBlurredImageFromBitmap(Context context, int radius, Bitmap image) { // Load a clean bitmap and work from that. Bitmap originalBitmap = image; if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1) { try { Bitmap blurredBitmap; blurredBitmap = Bitmap.CreateBitmap(originalBitmap); RenderScript rs = RenderScript.Create(context); Allocation input = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script); Allocation output = Allocation.CreateTyped(rs, input.Type); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs)); script.SetInput(input); script.SetRadius(radius); script.ForEach(output); output.CopyTo(blurredBitmap); output.Dispose(); script.Dispose(); input.Dispose(); rs.Dispose(); return(blurredBitmap); } catch (Exception) { return(originalBitmap); } } else { return(originalBitmap); } }
protected override void Dispose(bool disposing) { if (_disposed) { _disposed = true; if (disposing) { _renderScript.Destroy(); _renderScript.Dispose(); _blurScript.Destroy(); _blurScript.Dispose(); if (_outAllocation != null) { _outAllocation.Destroy(); _outAllocation.Dispose(); _outAllocation = null; } } } base.Dispose(disposing); }