How to blur imageview in android

后端 未结 10 1991
南笙
南笙 2020-12-05 10:54

I have an imageview and i set Image Resources programmatically like this:

int resourceId = getResources().getIdentifier(\"imagename\", \"drawable\", \"mypack         


        
10条回答
  •  Happy的楠姐
    2020-12-05 11:45

    private Bitmap CreateBlurredImage (int radius)
    {
       // Load a clean bitmap and work from that
        Bitmap originalBitmap=
        BitmapFactory.DecodeResource(Resources,Resource.Drawable.dog_and_monkeys);
    
    // 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 (this);
    
    // 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;
    

    }

    protected override void OnCreate (Bundle bundle)
    {
    base.OnCreate (bundle);
    
    SetContentView (Resource.Layout.Main);
    _imageView = FindViewById (Resource.Id.originalImageView);
    
    _seekbar = FindViewById (Resource.Id.seekBar1);
    _seekbar.StopTrackingTouch += BlurImageHandler;
    

    }

    private void BlurImageHandler (object sender, SeekBar.StopTrackingTouchEventArgs e)
    {
    int radius = e.SeekBar.Progress;
    if (radius == 0) {
        // We don't want to blur, so just load the un-altered image.
        _imageView.SetImageResource (Resource.Drawable.dog_and_monkeys);
    } else {
        DisplayBlurredImage (radius);
    }
    

    }

    private void DisplayBlurredImage (int radius)
    {
    _seekbar.StopTrackingTouch -= BlurImageHandler;
    _seekbar.Enabled = false;
    
    ShowIndeterminateProgressDialog ();
    
    Task.Factory.StartNew (() => {
        Bitmap bmp = CreateBlurredImage (radius);
        return bmp;
    })
    .ContinueWith (task => {
        Bitmap bmp = task.Result;
        _imageView.SetImageBitmap (bmp);
        _seekbar.StopTrackingTouch += BlurImageHandler;
        _seekbar.Enabled = true;
        DismissIndeterminateProgressDialog ();
    }, TaskScheduler.FromCurrentSynchronizationContext ());
    

    }

    
    
        
        
    
    

    click here deatiled code example

提交回复
热议问题