Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Android ImageRenderer

$
0
0

Hi,

I have the below code. I tried many way of doing it but basically it's like SetImageBitmap doesn't change the image once it's been displayed not matter if it's called again. I mean the first display works but when I change the BlurRadius I can see that SetBitmap is called but it has no effect on screen. What am I missing?
Note: CreateBlurredImage comes from Xamarin's documentation and GetImageFromImageSource from https://github.com/TheRealAdamKemp/BlurredImageTest/blob/master/Droid/BlurredImageRenderer.cs

public class BlurredImage : ImageRenderer
    {
        ImageSource BaseSource;
        Bitmap BaseImage;
        Dictionary<double, Bitmap> BlurredImages = new Dictionary<double, Bitmap>();

        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            BaseSource = Element.Source;
            var img = new ImageView(Forms.Context,null);
            SetNativeControl(img);
            SetBlurImage();          
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == XamarinCommon.Views.BlurredImage.BlurRadiusProperty.PropertyName)
                SetBlurImage();
        }

        private async Task SetBlurImage()
        {
            var rcv = Element as XamarinCommon.Views.BlurredImage;
            if(BaseImage == null)
                BaseImage = await GetImageFromImageSource(BaseSource);
            if (rcv.BlurRadius == 0 && !BlurredImages.ContainsKey(0))
                BlurredImages.Add(0, BaseImage);
            if (rcv.BlurRadius != 0 && !BlurredImages.ContainsKey(rcv.BlurRadius))
                BlurredImages.Add(rcv.BlurRadius, CreateBlurredImage(BaseImage, rcv.BlurRadius));

            Control.SetImageBitmap(BlurredImages[rcv.BlurRadius]);
        }

        private Bitmap CreateBlurredImage(Bitmap BaseImage, double radius)
        {
            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap;
            blurredBitmap = Bitmap.CreateBitmap(BaseImage);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(Forms.Context);

            // Allocate memory for Renderscript to work with
            Allocation input = Allocation.CreateFromBitmap(rs, BaseImage, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // Load up an instance of the specific script that we want to use.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs));
            script.SetInput(input);

            // Set the blur radius
            script.SetRadius((float)(10.0 * radius));

            // Start the ScriptIntrinisicBlur
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            return blurredBitmap;
        }

        private async Task<Bitmap> GetImageFromImageSource(ImageSource imageSource)
        {
            IImageSourceHandler handler;
            if (imageSource is FileImageSource)
                handler = new FileImageSourceHandler();
            else if (imageSource is StreamImageSource)
                handler = new StreamImagesourceHandler(); // sic
            else if (imageSource is UriImageSource)
                handler = new ImageLoaderSourceHandler(); // sic
            else
                throw new NotImplementedException();
            return await handler.LoadImageAsync(imageSource, Forms.Context);
        }

    }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>