コード例 #1
0
        public Bitmap BlurImage(Bitmap image)
        {
            if (image == null)
                return null;

            image = RGB565toARGB888(image);
            if (!configured)
            {
                input = Allocation.CreateFromBitmap(rs, image);
                output = Allocation.CreateTyped(rs, input.Type);
                script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                script.SetRadius(BLUR_RADIUS);
                configured = true;
            }
            else
            {
                input.CopyFrom(image);
            }

            script.SetInput(input);
            script.ForEach(output);
            output.CopyTo(image);

            return image;
        }
コード例 #2
0
        public void Show()
        {
            var obj  = (Activity)Xamarin.Forms.Forms.Context;
            var root = obj.Window.DecorView.FindViewById(Resource.Id.Content);

            root.DrawingCacheEnabled = true;
            var b = Bitmap.CreateBitmap(root.GetDrawingCache(true));

            root.DrawingCacheEnabled = false;

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(b);

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

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, b, 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, Renderscripts.Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(25);

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

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

            dialog = new Dialog(obj, Resource.Style.ThemeNoTitleBar);
            Drawable d = new BitmapDrawable(blurredBitmap);

            dialog.Window.SetBackgroundDrawable(d);
            dialog.Show();
        }
コード例 #3
0
        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();

            if (useBlur)
            {
                rs = RenderScript.Create(Context);

                if (rs != null)
                {
                    script = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs));
                }

                if (Element != null)
                {
                    UpdateBackgroundColor();
                }
            }
        }
コード例 #4
0
ファイル: ArtistItemView.cs プロジェクト: phaufe/Similardio
 Bitmap BlurImage(Bitmap input)
 {
     try {
         var rsScript = RenderScript.Create(Context);
         var alloc    = Allocation.CreateFromBitmap(rsScript, input);
         var blur     = ScriptIntrinsicBlur.Create(rsScript, alloc.Element);
         blur.SetRadius(12);
         blur.SetInput(alloc);
         var result   = Bitmap.CreateBitmap(input.Width, input.Height, input.GetConfig());
         var outAlloc = Allocation.CreateFromBitmap(rsScript, result);
         blur.ForEach(outAlloc);
         outAlloc.CopyTo(result);
         rsScript.Destroy();
         return(result);
     } catch (Exception e) {
         Log.Error("Blurrer", "Error while trying to blur, fallbacking. " + e.ToString());
         return(input);
     }
 }
コード例 #5
0
        public Bitmap CreateBlurredImageoffline(Context contexto, int radius, int resid)
        {
            // Load a clean bitmap and work from that.
            WallpaperManager wm = WallpaperManager.GetInstance(this);
            Drawable         d  = wm.Drawable;
            Bitmap           originalBitmap;

            originalBitmap = ((BitmapDrawable)d).Bitmap;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1)
            {
                // Create another bitmap that will hold the results of the filter.
                Bitmap blurredBitmap;
                blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

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

                // Allocate memory for Renderscript to work with
                Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, 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, Element.U8_4(rs));
                script.SetInput(input);

                // Set the blur radius
                script.SetRadius(radius);

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

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

                return(blurredBitmap);
            }
            else
            {
                return(originalBitmap);
            }
        }
コード例 #6
0
        private void CreateBlurredImage(int radius)
        {
            // Load a clean bitmap and work from that.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            if (blurredBmp != null)
            {
                blurredBmp.Recycle();
                blurredBmp.Dispose();
                blurredBmp = null;
            }

            // Create another bitmap that will hold the results of the filter.
            blurredBmp = Bitmap.CreateBitmap(originalBitmap);

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

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, Allocation.UsageScript);
            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, Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Start Renderscript working.
            script.ForEach(output);

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

            input.Destroy();
            input.Dispose();

            output.Destroy();
            output.Dispose();
        }
コード例 #7
0
        public Bitmap FnBlurEffectToImage(Bitmap image)
        {
            if (image == null)
            {
                return(null);
            }

            Bitmap       outputBitmap = Bitmap.CreateBitmap(image);
            RenderScript renderScript = RenderScript.Create(this);
            Allocation   tmpIn        = Allocation.CreateFromBitmap(renderScript, image);
            Allocation   tmpOut       = Allocation.CreateFromBitmap(renderScript, outputBitmap);
            //Intrinsic Gausian blur filter
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            theIntrinsic.SetRadius(10f);
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach(tmpOut);
            tmpOut.CopyTo(outputBitmap);
            return(outputBitmap);
        }
コード例 #8
0
        private Bitmap CreateBlurredImage(int radius, Bitmap originalBitmap)
        {
            Bitmap blurredBitmap;

            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            var rs     = RenderScript.Create(Forms.Context);
            var input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            var output = Allocation.CreateTyped(rs, input.Type);

            var script = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs));

            script.SetInput(input);
            script.SetRadius(radius);
            script.ForEach(output);

            output.CopyTo(blurredBitmap);

            return(blurredBitmap);
        }
コード例 #9
0
        public static Bitmap Blur(Context ctx, Bitmap image)
        {
            int width  = (int)Math.Round(image.Width * BITMAP_SCALE);
            int height = (int)Math.Round(image.Height * BITMAP_SCALE);

            Bitmap inputBitmap  = Bitmap.CreateScaledBitmap(image, width, height, false);
            Bitmap outputBitmap = Bitmap.CreateBitmap(inputBitmap);

            var rs = RenderScript.Create(ctx);
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
            Allocation          tmpIn        = Allocation.CreateFromBitmap(rs, inputBitmap);
            Allocation          tmpOut       = Allocation.CreateFromBitmap(rs, outputBitmap);

            theIntrinsic.SetRadius(BLUR_RADIUS);
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach(tmpOut);
            tmpOut.CopyTo(outputBitmap);

            return(outputBitmap);
        }
コード例 #10
0
        protected override void OnDetachedFromWindow()
        {
            script?.Dispose();
            script = null;
            sourceCanvas?.Dispose();
            sourceCanvas = null;
            source?.Dispose();
            source = null;
            blurred?.Dispose();
            blurred = null;
            rs?.Dispose();
            rs = null;

            if (Element != null)
            {
                UpdateBackgroundColor();
            }

            base.OnDetachedFromWindow();
        }
コード例 #11
0
ファイル: MainActivity.cs プロジェクト: MIliev11/Samples
        Bitmap CreateBlurredImage(int radius)
        {
            // Load a clean bitmap to work from.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            // Load up an instance of the specific script that we want to use.
            // An Element is similar to a C type. The second parameter, Element.U8_4,
            // tells the Allocation is made up of 4 fields of 8 unsigned bits.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            // Create an Allocation for the kernel inputs.
            Allocation input = Allocation.CreateFromBitmap(renderScript, originalBitmap,
                                                           Allocation.MipmapControl.MipmapFull,
                                                           AllocationUsage.Script);

            // Assign the input Allocation to the script.
            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Finally we need to create an output allocation to hold the output of the Renderscript.
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);

            // Next, run the script. This will run the script over each Element in the Allocation, and copy it's
            // output to the allocation we just created for this purpose.
            script.ForEach(output);

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

            // Cleanup.
            output.Destroy();
            input.Destroy();
            script.Destroy();

            return(blurredBitmap);
        }
コード例 #12
0
        protected override Android.Graphics.Bitmap Transform(Android.Graphics.Bitmap source)
        {
            Bitmap outBitmap = Bitmap.CreateBitmap(source.Width, source.Height, Bitmap.Config.Argb8888);
            Canvas canvas    = new Canvas(outBitmap);

            canvas.DrawBitmap(source, 0, 0, null);

            RenderScript        rs           = RenderScript.Create(mContext);
            Allocation          overlayAlloc = Allocation.CreateFromBitmap(rs, outBitmap);
            ScriptIntrinsicBlur blur         = ScriptIntrinsicBlur.Create(rs, overlayAlloc.Element);

            blur.SetInput(overlayAlloc);
            blur.SetRadius(mRadius);
            blur.ForEach(overlayAlloc);
            overlayAlloc.CopyTo(outBitmap);

            source.Recycle();
            rs.Destroy();

            return(outBitmap);
        }
コード例 #13
0
        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();
                }
            });
        }
コード例 #14
0
ファイル: BitmapExtension.cs プロジェクト: xamarinhub/SKOR.UI
        public static Bitmap Blur(this Bitmap image, Context context, float blurRadius = 25)
        {
            Bitmap outputBitmap = null;

            if (image != null)
            {
                if (blurRadius == 0)
                {
                    return(image);
                }

                if (blurRadius < 1)
                {
                    blurRadius = 1;
                }

                if (blurRadius > 25)
                {
                    blurRadius = 25;
                }

                BLUR_RADIUS = blurRadius;

                int width  = (int)Math.Round(image.Width * BITMAP_SCALE);
                int height = (int)Math.Round(image.Width * BITMAP_SCALE);

                Bitmap inputBitmap = Bitmap.CreateScaledBitmap(image, width, height, false);
                outputBitmap = Bitmap.CreateBitmap(inputBitmap);

                RenderScript        rs           = RenderScript.Create(context);
                ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                Allocation          tmpIn        = Allocation.CreateFromBitmap(rs, inputBitmap);
                Allocation          tmpOut       = Allocation.CreateFromBitmap(rs, outputBitmap);
                theIntrinsic.SetRadius(BLUR_RADIUS);
                theIntrinsic.SetInput(tmpIn);
                theIntrinsic.ForEach(tmpOut);
                tmpOut.CopyTo(outputBitmap);
            }
            return(outputBitmap);
        }
コード例 #15
0
        public Bitmap Blur(string key, Bitmap bitmap, Color color)
        {
            if (bitmap == null)
            {
                return(null);
            }
            var red       = Color.GetRedComponent(color);
            var blue      = Color.GetBlueComponent(color);
            var green     = Color.GetGreenComponent(color);
            var blurColor = new Color(red, green, blue, 66);

            var px  = 70f * ((float)metrics.DensityDpi / 160f);
            var pxs = Math.Round(px);

            var ratio = pxs / bitmap.Height;

            var height = (ratio * bitmap.Height);
            var y      = bitmap.Height - height;


            var portionToBlur = Bitmap.CreateBitmap(bitmap, 0, (int)y, bitmap.Width,
                                                    (int)height);
            var blurredBitmap = portionToBlur.Copy(Bitmap.Config.Argb8888, true);
            var renderScript  = RenderScript.Create(context);
            var theIntrinsic  = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));
            var tmpIn         = Allocation.CreateFromBitmap(renderScript, portionToBlur);
            var tmpOut        = Allocation.CreateFromBitmap(renderScript, blurredBitmap);

            theIntrinsic.SetRadius(25f);
            theIntrinsic.SetInput(tmpIn);
            theIntrinsic.ForEach(tmpOut);
            tmpOut.CopyTo(blurredBitmap);
            new Canvas(blurredBitmap).DrawColor(blurColor);
            var newBitmap = bitmap.Copy(Bitmap.Config.Argb8888, true);
            var canvas    = new Canvas(newBitmap);

            canvas.DrawBitmap(blurredBitmap, 0, (int)y, new Paint());
            return(newBitmap);
        }
コード例 #16
0
        public Drawable Difuminar(Drawable papelTapiz)
        {
            //Fondo de escritorio provista por el Argumento que se pasa en <papelTapiz>
            Bitmap originalBitmap = ((BitmapDrawable)papelTapiz).Bitmap;
            // Un bitmap null que almacenará la imagen difuminada.
            Bitmap blurredBitmap;

            //Asignar a este bitmap la imagen original para trabajar con ella.
            blurredBitmap = Bitmap.CreateBitmap(originalBitmap);
            //Crear la instancia de RenderScript que hará el trabajo
            RenderScript rs = RenderScript.Create(Application.Context);
            //Alocar memoria para que RenderScript trabaje.
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, 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, Element.U8_4(rs));

            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(25);

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

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

            //Scale the bitmap:
            Bitmap blurredBitMapResized = Bitmap.CreateScaledBitmap(blurredBitmap, 70, 80, false);

            Drawable papelTapizDifuminado = new BitmapDrawable(blurredBitMapResized);

            originalBitmap       = null;
            blurredBitmap        = null;
            blurredBitMapResized = null;
            return(papelTapizDifuminado);
        }
コード例 #17
0
ファイル: BlurredImage.cs プロジェクト: planath/MasterDetail
        /// <summary>
        /// transforms a bitmap and returns it blurred
        /// </summary>
        /// <param name="context">application context</param>
        /// <param name="smallBitmap">bitmap without blur</param>
        /// <param name="radius">blur radius must be 0 < r <= 25</param>
        /// <returns></returns>
        private Bitmap BlurRenderScript(Context context, Bitmap smallBitmap, int radius)
        {
            radius = Math.Min(radius, 25);
            radius = Math.Max(radius, 1);

            smallBitmap = RgbToArgb(smallBitmap);
            var bitmap = Bitmap.CreateBitmap(smallBitmap.Width, smallBitmap.Height, Bitmap.Config.Argb8888);

            var renderScript = RenderScript.Create(context);
            var blurInput    = Allocation.CreateFromBitmap(renderScript, smallBitmap);
            var blurOutput   = Allocation.CreateFromBitmap(renderScript, bitmap);

            var blur = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            blur.SetInput(blurInput);
            blur.SetRadius(radius);
            blur.ForEach(blurOutput);

            blurOutput.CopyTo(bitmap);
            renderScript.Destroy();

            return(bitmap);
        }
コード例 #18
0
        public static Bitmap BlurBitmap(Bitmap smallBitmap, int radius, Context context)
        {
            Bitmap bitmap = Bitmap.CreateBitmap(
                smallBitmap.Width, smallBitmap.Height,
                Bitmap.Config.Argb8888);

            RenderScript renderScript = RenderScript.Create(context);

            Allocation blurInput  = Allocation.CreateFromBitmap(renderScript, smallBitmap);
            Allocation blurOutput = Allocation.CreateFromBitmap(renderScript, bitmap);

            ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.Create(renderScript,
                                                                  Element.U8_4(renderScript));

            blur.SetInput(blurInput);
            blur.SetRadius(radius); // radius must be 0 < r <= 25
            blur.ForEach(blurOutput);

            blurOutput.CopyTo(bitmap);
            renderScript.Destroy();

            return(bitmap);
        }
コード例 #19
0
        public bool Prepare(Context context, Bitmap buffer, float radius)
        {
            if (_mRenderScript == null)
            {
                try
                {
                    _mRenderScript = RenderScript.Create(context);
                    _mBlurScript   = ScriptIntrinsicBlur.Create(_mRenderScript, Element.U8_4(_mRenderScript));
                }
                catch (Android.Renderscripts.RSRuntimeException e)
                {
#pragma warning disable CS0162
                    if (DEBUG)
                    {
                        throw e;
                    }
                    else
                    {
                        // In release mode, just ignore
                        Release();
                        return(false);
                    }
#pragma warning restore CS0162
                }
            }

            _mBlurScript.SetRadius(radius);

            _mBlurInput = Allocation.CreateFromBitmap(
                _mRenderScript,
                buffer,
                Allocation.MipmapControl.MipmapNone,
                AllocationUsage.Script);
            _mBlurOutput = Allocation.CreateTyped(_mRenderScript, _mBlurInput.Type);

            return(true);
        }
コード例 #20
0
        void BlurImage(int radius)
        {
            // Disable the event handler and the Seekbar to prevent this from
            // happening multiple times.
            _seekbar.ProgressChanged -= BlurImageHandler;
            _seekbar.Enabled          = false;

            _imageView.SetImageDrawable(null);

            Task.Run(() =>
            {
                // Load a clean bitmap and work from that.
                var sentBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

                var bitmap = sentBitmap.Copy(sentBitmap.GetConfig(), true);
                var rs     = RenderScript.Create(this);
                var input  = Allocation.CreateFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MipmapNone, Allocation.UsageScript);
                var output = Allocation.CreateTyped(rs, input.Type);
                var script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                script.SetRadius(radius);
                script.SetInput(input);
                script.ForEach(output);
                output.CopyTo(bitmap);
                // clean up renderscript resources
                rs.Destroy();
                input.Destroy();
                output.Destroy();
                script.Destroy();

                RunOnUiThread(() =>
                {
                    _imageView.SetImageBitmap(bitmap);
                    _seekbar.ProgressChanged += BlurImageHandler;
                    _seekbar.Enabled          = true;
                });
            });
        }
コード例 #21
0
        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);
            }
        }
コード例 #22
0
ファイル: Player.cs プロジェクト: pictos/Opus
        public async void RefreshPlayer()
        {
            while (MainActivity.instance == null || MusicPlayer.CurrentID() == -1)
            {
                await Task.Delay(100);
            }

            Song current = await MusicPlayer.GetItem();

            if (current == null || (current.IsYt && current.Album == null))
            {
                return;
            }

            FrameLayout smallPlayer = MainActivity.instance.FindViewById <FrameLayout>(Resource.Id.smallPlayer);

            smallPlayer.FindViewById <TextView>(Resource.Id.spTitle).Text  = current.Title;
            smallPlayer.FindViewById <TextView>(Resource.Id.spArtist).Text = current.Artist;
            smallPlayer.FindViewById <ImageView>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Pause);
            ImageView art = smallPlayer.FindViewById <ImageView>(Resource.Id.spArt);

            if (!current.IsYt)
            {
                var songCover       = Android.Net.Uri.Parse("content://media/external/audio/albumart");
                var nextAlbumArtUri = ContentUris.WithAppendedId(songCover, current.AlbumArt);

                Picasso.With(Application.Context).Load(nextAlbumArtUri).Placeholder(Resource.Drawable.noAlbum).Resize(400, 400).CenterCrop().Into(art);
            }
            else
            {
                Picasso.With(Application.Context).Load(current.Album).Placeholder(Resource.Drawable.noAlbum).Transform(new RemoveBlackBorder(true)).Into(art);
            }

            TextView title  = MainActivity.instance.FindViewById <TextView>(Resource.Id.playerTitle);
            TextView artist = MainActivity.instance.FindViewById <TextView>(Resource.Id.playerArtist);

            albumArt = MainActivity.instance.FindViewById <ImageView>(Resource.Id.playerAlbum);
            SpannableString titleText = new SpannableString(current.Title);

            titleText.SetSpan(new BackgroundColorSpan(Color.ParseColor("#BF000000")), 0, current.Title.Length, SpanTypes.InclusiveInclusive);
            title.TextFormatted = titleText;
            SpannableString artistText = new SpannableString(current.Artist);

            artistText.SetSpan(new BackgroundColorSpan(Color.ParseColor("#BF000000")), 0, current.Artist.Length, SpanTypes.InclusiveInclusive);
            artist.TextFormatted = artistText;

            if (!errorState)
            {
                if (MusicPlayer.isRunning)
                {
                    MainActivity.instance.FindViewById <ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.Pause);
                    smallPlayer.FindViewById <ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Pause);
                }
                else
                {
                    MainActivity.instance.FindViewById <ImageButton>(Resource.Id.playButton).SetImageResource(Resource.Drawable.Play);
                    smallPlayer.FindViewById <ImageButton>(Resource.Id.spPlay).SetImageResource(Resource.Drawable.Play);
                }
            }

            Bitmap drawable = null;

            if (current.AlbumArt == -1)
            {
                await Task.Run(() =>
                {
                    try
                    {
                        drawable = Picasso.With(Application.Context).Load(current.Album).Error(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.noAlbum).Transform(new RemoveBlackBorder(true)).Get();
                    }
                    catch
                    {
                        drawable = Picasso.With(Application.Context).Load(Resource.Drawable.noAlbum).Get();
                    }
                });
            }
            else
            {
                Android.Net.Uri songCover = Android.Net.Uri.Parse("content://media/external/audio/albumart");
                Android.Net.Uri iconURI   = ContentUris.WithAppendedId(songCover, current.AlbumArt);

                await Task.Run(() =>
                {
                    try
                    {
                        drawable = Picasso.With(Application.Context).Load(iconURI).Error(Resource.Drawable.noAlbum).Placeholder(Resource.Drawable.noAlbum).NetworkPolicy(NetworkPolicy.Offline).Get();
                    }
                    catch
                    {
                        drawable = Picasso.With(Application.Context).Load(Resource.Drawable.noAlbum).Get();
                    }
                });
            }

            albumArt.SetImageBitmap(drawable);
            Palette.From(drawable).MaximumColorCount(28).Generate(this);

            if (await SongManager.IsFavorite(current))
            {
                MainActivity.instance?.FindViewById <ImageButton>(Resource.Id.fav)?.SetImageResource(Resource.Drawable.Unfav);
            }
            else
            {
                MainActivity.instance?.FindViewById <ImageButton>(Resource.Id.fav)?.SetImageResource(Resource.Drawable.Fav);
            }


            if (albumArt.Width > 0)
            {
                try
                {
                    //k is the coeficient to convert ImageView's size to Bitmap's size
                    //We want to take the lower coeficient because if we take the higher, we will have a final bitmap larger than the initial one and we can't create pixels
                    float k      = Math.Min((float)drawable.Height / albumArt.Height, (float)drawable.Width / albumArt.Width);
                    int   width  = (int)(albumArt.Width * k);
                    int   height = (int)(albumArt.Height * k);

                    int dX = (int)((drawable.Width - width) * 0.5f);
                    int dY = (int)((drawable.Height - height) * 0.5f);

                    Console.WriteLine("&Drawable Info: Width: " + drawable.Width + " Height: " + drawable.Height);
                    Console.WriteLine("&AlbumArt Info: Width: " + albumArt.Width + " Height: " + albumArt.Height);
                    Console.WriteLine("&Blur Creation: Width: " + width + " Height: " + height + " dX: " + dX + " dY: " + dY);
                    //The width of the view in pixel (we'll multiply this by 0.75f because the drawer has a width of 75%)
                    Bitmap blured = Bitmap.CreateBitmap(drawable, dX, dY, (int)(width * 0.75f), height);
                    Console.WriteLine("&BLured bitmap created");

                    RenderScript        rs      = RenderScript.Create(MainActivity.instance);
                    Allocation          input   = Allocation.CreateFromBitmap(rs, blured);
                    Allocation          output  = Allocation.CreateTyped(rs, input.Type);
                    ScriptIntrinsicBlur blurrer = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                    blurrer.SetRadius(13);
                    blurrer.SetInput(input);
                    blurrer.ForEach(output);

                    output.CopyTo(blured);
                    MainActivity.instance.FindViewById <ImageView>(Resource.Id.queueBackground).SetImageBitmap(blured);
                    Console.WriteLine("&Bitmap set to image view");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("&Queue background error: " + ex.Message);
                }
            }

            if (bar != null)
            {
                if (spBar == null)
                {
                    spBar = MainActivity.instance.FindViewById <ProgressBar>(Resource.Id.spProgress);
                }

                if (current.IsLiveStream)
                {
                    bar.Max        = 1;
                    bar.Progress   = 1;
                    spBar.Max      = 1;
                    spBar.Progress = 1;
                    timer.Text     = "🔴 LIVE";
                }
                else
                {
                    int duration = await MusicPlayer.Duration();

                    bar.Max        = duration;
                    timer.Text     = string.Format("{0} | {1}", DurationToTimer((int)MusicPlayer.CurrentPosition), DurationToTimer(duration));
                    spBar.Max      = duration;
                    spBar.Progress = (int)MusicPlayer.CurrentPosition;

                    handler.PostDelayed(UpdateSeekBar, 1000);

                    int LoadedMax = (int)await MusicPlayer.LoadDuration();

                    bar.Max    = LoadedMax;
                    spBar.Max  = LoadedMax;
                    timer.Text = string.Format("{0} | {1}", DurationToTimer((int)MusicPlayer.CurrentPosition), DurationToTimer(LoadedMax));
                }
            }
        }
コード例 #23
0
 public RenderScriptBlur(Context context)
 {
     _renderScript = RenderScript.Create(context);
     _blurScript   = ScriptIntrinsicBlur.Create(_renderScript, Element.U8_4(_renderScript));
 }
コード例 #24
0
 public BlurredImageRenderer(Context context) : base(context)
 {
     AutoPackage = false;
     rs          = RenderScript.Create(Context);
     script      = ScriptIntrinsicBlur.Create(rs, Android.Renderscripts.Element.U8_4(rs));
 }
コード例 #25
0
        protected override void OnDraw(Canvas canvas)
        {
            canvas.Save();

            try
            {
                if (rs != null)
                {
                    var mainLayoutRenderer = WebViewPage.Current.MainLayoutRenderer;
                    var sourceView         = mainLayoutRenderer.View;

                    if (lastSourceView != sourceView)
                    {
                        if (lastSourceView != null)
                        {
                            lastSourceView.ViewTreeObserver.PreDraw -= ViewTreeObserver_PreDraw;
                        }

                        sourceView.ViewTreeObserver.PreDraw += ViewTreeObserver_PreDraw;
                        lastSourceView = sourceView;
                    }

                    // Create bitmaps if needed
                    if ((sourceView.Width + 50) != source?.Width || (sourceView.Height + 50) != source?.Height)
                    {
                        sourceCanvas?.Dispose();
                        source?.Dispose();
                        blurred?.Dispose();

                        source = Bitmap.CreateBitmap(sourceView.Width + 50, sourceView.Height + 50, Bitmap.Config.Argb8888);
                        source.EraseColor(Ao3TrackReader.Resources.Colors.Alt.High.ToAndroid());


                        blurred      = Bitmap.CreateBitmap(sourceView.Width + 50, sourceView.Height + 50, Bitmap.Config.Argb8888);
                        sourceCanvas = new Canvas(source);
                    }


                    int[] sourceLoc = new int[2];
                    sourceView.GetLocationInWindow(sourceLoc);

                    int[] destLoc = new int[2];
                    GetLocationInWindow(destLoc);

                    var xOffset = sourceLoc[0] - sourceView.TranslationX - 25 - destLoc[0] + TranslationX;
                    var yOffset = sourceLoc[1] - sourceView.TranslationY - 25 - destLoc[1] + TranslationY;

                    sourceCanvas.Save();
                    sourceCanvas.ClipRect((int)-xOffset, (int)-yOffset, (int)-xOffset + Width + 50, (int)-yOffset + Height + 50);
                    sourceCanvas.Translate(25, 25);
                    sourceView.Draw(sourceCanvas);
                    sourceCanvas.Restore();

                    // Allocate memory for Renderscript to work with
                    Allocation input  = Allocation.CreateFromBitmap(rs, source, 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.
                    script.SetInput(input);

                    // Set the blur radius
                    script.SetRadius(25);


                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                    {
                        var lo = new Script.LaunchOptions();
                        lo.SetX((int)-xOffset, (int)-xOffset + Width);
                        lo.SetY((int)-yOffset, (int)-yOffset + Height);
                        script.ForEach(output, lo);
                    }
                    else
                    {
                        script.ForEach(output);
                    }

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

                    input.Dispose();
                    output.Dispose();

                    canvas.ClipRect(0, 0, Width, Height);
                    canvas.Translate(xOffset, yOffset);
                    canvas.DrawBitmap(blurred, 0, 0, null);
                    canvas.DrawColor(tint);
                }
            }
            catch
            {
                script?.Dispose();
                script = null;
                sourceCanvas?.Dispose();
                sourceCanvas = null;
                source?.Dispose();
                source = null;
                blurred?.Dispose();
                blurred = null;
                rs?.Dispose();
                rs = null;

                useBlur = false;
                App.Database.GetVariableEvents("PaneViewRenderer.useBlur").Updated -= DatabaseVariable_Updated;

                UpdateBackgroundColor();
            }
            canvas.Restore();

            base.OnDraw(canvas);
        }
コード例 #26
0
 private void InitializeRenderScript(Context context)
 {
     _renderScript = RenderScript.Create(context);
     _blurScript   = ScriptIntrinsicBlur.Create(_renderScript, Element.U8_4(_renderScript));
 }
コード例 #27
0
ファイル: BlurringView.cs プロジェクト: takigava/Glass
 private void initializeRenderScript(Context context)
 {
     mRenderScript = RenderScript.Create(context);
     mBlurScript   = ScriptIntrinsicBlur.Create(mRenderScript, Element.U8_4(mRenderScript));
 }
コード例 #28
0
        private Bitmap CreateBitmap(ShadeInfo shadeInfo)
        {
#if DEBUG
            var stopWatch = new Stopwatch();
            stopWatch.Start();
#endif
            var shadow = Bitmap.CreateBitmap(
                shadeInfo.Width,
                shadeInfo.Height,
                Bitmap.Config.Argb8888);

            InternalLogger.Debug(LogTag, () => $"CreateBitmap( shadeInfo: {shadeInfo} )");
            RectF rect = new RectF(
                ShadeInfo.Padding,
                ShadeInfo.Padding,
                shadeInfo.Width - ShadeInfo.Padding,
                shadeInfo.Height - ShadeInfo.Padding);

            using var bitmapCanvas = new Canvas(shadow);
            using var paint        = new Paint { Color = shadeInfo.Color };
            bitmapCanvas.DrawRoundRect(
                rect,
                _cornerRadius,
                _cornerRadius,
                paint);

            if (shadeInfo.BlurRadius < 1)
            {
                return(shadow);
            }

            const int MaxBlur    = 25;
            float     blurAmount = shadeInfo.BlurRadius > MaxRadius ? MaxRadius : shadeInfo.BlurRadius;
            while (blurAmount > 0)
            {
                Allocation input = Allocation.CreateFromBitmap(
                    _renderScript,
                    shadow,
                    Allocation.MipmapControl.MipmapNone,
                    AllocationUsage.Script);
                Allocation          output = Allocation.CreateTyped(_renderScript, input.Type);
                ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(_renderScript, Element.U8_4(_renderScript));

                float blurRadius;
                if (blurAmount > MaxBlur)
                {
                    blurRadius  = MaxBlur;
                    blurAmount -= MaxBlur;
                }
                else
                {
                    blurRadius = blurAmount;
                    blurAmount = 0;
                }

                script.SetRadius(blurRadius);
                script.SetInput(input);
                script.ForEach(output);
                output.CopyTo(shadow);
            }
#if DEBUG
            LogPerf(LogTag, stopWatch);
#endif
            return(shadow);
        }
コード例 #29
0
        protected bool Prepare()
        {
            if (mBlurRadius == 0)
            {
                Release();
                return(false);
            }

            float downsampleFactor = mDownsampleFactor;
            float radius           = mBlurRadius / downsampleFactor;

            if (radius > 25)
            {
                downsampleFactor = downsampleFactor * radius / 25;
                radius           = 25;
            }

            if (mDirty || mRenderScript == null)
            {
                if (mRenderScript == null)
                {
                    try
                    {
                        mRenderScript = RenderScript.Create(Context);
                        mBlurScript   = ScriptIntrinsicBlur.Create(mRenderScript, Element.U8_4(mRenderScript));
                    }
                    catch (RSRuntimeException e)
                    {
#if DEBUG
                        if (e.Message != null && e.Message.StartsWith("Error loading RS jni library: java.lang.UnsatisfiedLinkError:"))
                        {
                            throw new Exception("Error loading RS jni library, Upgrade buildToolsVersion=\"24.0.2\" or higher may solve this issue");
                        }
                        throw;
#else
                        // In release mode, just ignore
                        ReleaseScript();
                        return(false);
#endif
                    }
                }

                mBlurScript.SetRadius(radius);
                mDirty = false;
            }

            int width  = Width;
            int height = Height;

            int scaledWidth  = Math.Max(1, (int)(width / downsampleFactor));
            int scaledHeight = Math.Max(1, (int)(height / downsampleFactor));

            if (mBlurringCanvas == null || mBlurredBitmap == null ||
                mBlurredBitmap.Width != scaledWidth ||
                mBlurredBitmap.Height != scaledHeight)
            {
                ReleaseBitmap();

                var r = false;
                try
                {
                    mBitmapToBlur = Bitmap.CreateBitmap(scaledWidth, scaledHeight, Bitmap.Config.Argb8888);
                    if (mBitmapToBlur == null)
                    {
                        return(false);
                    }

                    mBlurringCanvas = new Canvas(mBitmapToBlur);

                    mBlurInput  = Allocation.CreateFromBitmap(mRenderScript, mBitmapToBlur, Allocation.MipmapControl.MipmapNone, AllocationUsage.Script);
                    mBlurOutput = Allocation.CreateTyped(mRenderScript, mBlurInput.Type);

                    mBlurredBitmap = Bitmap.CreateBitmap(scaledWidth, scaledHeight, Bitmap.Config.Argb8888);
                    if (mBlurredBitmap == null)
                    {
                        return(false);
                    }

                    r = true;
                }
                catch (Java.Lang.OutOfMemoryError)
                {
                    // Bitmap.createBitmap() may cause OOM error
                    // Simply ignore and fallback
                }

                if (!r)
                {
                    ReleaseBitmap();
                    return(false);
                }
            }

            return(true);
        }