Blur An Image In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. Blur an Image is used to seek bar to control the Image Blur.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below, are required to be followed in order to create Blur an Image app in Xamarin, using Visual Studio 2015.

Step 1

Go to File --> New --> Project, or click (Ctrl+Shift+N). The project needs to be clicked after opening all the types of projects in Visual Studio.

Project

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Project

Step 3

Next, go to the Solution Explorer. Select Resource --> Layout --> double click to open Main.axml page. Now, if you want to design the app through coding, select Source panel, else select Designer panel.

Project

Step 4

After opening the Main.axml file, let's open the main page designer.

Project

Next, delete the default "hello world" button. Go to the Source panel and delete the XAML code. The same way, go to the MainActivity.cs page and delete the C# button action code.

Step 5

Now, go to the toolbox window and scroll until you see the LinearLayout(Horizontal) control. Drag and drop it on your main page design section.

Project

Step 6

Next, drag and drop the TextView.

Project

Step 7

Drag and drop the Seekbar.

Project

Step 8

Next, drag and drop the ImageView.

Project

Step 9

Now, go to the properties window. You need to edit the TextView Id Value and Text Value (EX:android:id="@+id/textView1" android:text="@string/blur_radius_text").

Project

Step 10

Next, edit the SeekBar Id value(EX:android:id="@+id/seekBar1").

Project

Step 11

In this step, add one image. Go to Solution Explorer --> Resource --> Drawable --> Right click --> Add -->Existing (Shift+Alt+A).

Project

Step 12

In this step, select the image you need and click Add.

Project

Step 13

Now, go to the Properties window. You need to edit the ImageView Id Value and src Value here (EX: android:id="@+id/originalImageView" android:src="@drawable/dog_and_monkeys").

Project

Step 14

In this step, go to the Main.axml page source panel. Note, the ImageView Id value and source value and TextView and SeekBar Id Values.

Project

Main.axml

  1. <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="12dp" android:paddingStart="6dp" android:paddingEnd="6dp">  
  2.     <TextView android:text="@string/blur_radius_text" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" />  
  3.     <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBar1" android:max="25" />  
  4. </LinearLayout>  
  5. <ImageView android:src="@drawable/dog_and_monkeys" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/originalImageView" />  
Step 15

In this step, open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.

Project

Step 16

After opening the String.xml file, write the following XML code.

String.xml

Project

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="blur_image_text">Blur!</string>  
  4.     <string name="app_name">Blur Example</string>  
  5.     <string name="dialogtitle">Blur Image</string>  
  6.     <string name="dialogmessage">Working</string>  
  7.     <string name="blur_radius_text">Blur Radius:</string>  
  8. </resources>  
Step 17

Next, go to the MainActivity.cs page and write the following two namespaces.
  1. using System.Threading.Tasks;  
  2. using Android.Graphics;  
  3. using Android.Renderscripts;  
  4. and also create the variables.  
  5. static readonly string PROGRESS_DIALOG_TAG = "progress_dialog";  
  6. ImageView imageView;  
  7. SeekBar seekBar;  
  8. RenderScript renderScript;  
Project

Step 18

Now, go to the MainActivity.cs page and write the following code.

MainActivity.cs 
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource    
  4.     SetContentView(Resource.Layout.Main);  
  5.     imageView = FindViewById < ImageView > (Resource.Id.originalImageView);  
  6.     seekBar = FindViewById < SeekBar > (Resource.Id.seekBar1);  
  7.     seekBar.StopTrackingTouch += BlurImageHandler;  
  8.     renderScript = RenderScript.Create(this);  
  9. }  
  10. protected override void OnDestroy() {  
  11.     renderScript.Destroy();  
  12.     base.OnDestroy();  
  13. }  
  14. void BlurImageHandler(object sender, SeekBar.StopTrackingTouchEventArgs e) {  
  15.     int radius = e.SeekBar.Progress;  
  16.     if (radius == 0) {  
  17.         imageView.SetImageResource(Resource.Drawable.dog_and_monkeys);  
  18.     } else {  
  19.         DisplayBlurredImage(radius);  
  20.     }  
  21. }  
  22. Bitmap CreateBlurredImage(int radius) {  
  23.     Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);  
  24.     Bitmap blurredBitmap = Bitmap.CreateBitmap(originalBitmap);  
  25.     ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));  
  26.     Allocation input = Allocation.CreateFromBitmap(renderScript, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);  
  27.     script.SetInput(input);  
  28.     script.SetRadius(radius);  
  29.     Allocation output = Allocation.CreateTyped(renderScript, input.Type);  
  30.     script.ForEach(output);  
  31.     output.CopyTo(blurredBitmap);  
  32.     output.Destroy();  
  33.     input.Destroy();  
  34.     script.Destroy();  
  35.     return blurredBitmap;  
  36. }  
  37. void DismissIndeterminateProgressDialog() {  
  38.     Fragment frag = FragmentManager.FindFragmentByTag(PROGRESS_DIALOG_TAG);  
  39.     if (frag != null) {  
  40.         ((DialogFragment) frag).Dismiss();  
  41.     }  
  42. }  
  43. void ShowIndeterminateProgressDialog() {  
  44.     MyProgressDialog progressDialog = FragmentManager.FindFragmentByTag(PROGRESS_DIALOG_TAG) as MyProgressDialog;  
  45.     if (progressDialog == null) {  
  46.         var tx = FragmentManager.BeginTransaction();  
  47.         progressDialog = new MyProgressDialog();  
  48.         progressDialog.Show(tx, PROGRESS_DIALOG_TAG);  
  49.     }  
  50. }  
  51. void DisplayBlurredImage(int radius) {  
  52.     seekBar.StopTrackingTouch -= BlurImageHandler;  
  53.     seekBar.Enabled = false;  
  54.     ShowIndeterminateProgressDialog();  
  55.     Task.Run(() => {  
  56.         Bitmap bmp = CreateBlurredImage(radius);  
  57.         return bmp;  
  58.     }).ContinueWith(task => {  
  59.         Bitmap bmp = task.Result;  
  60.         imageView.SetImageBitmap(bmp);  
  61.         seekBar.StopTrackingTouch += BlurImageHandler;  
  62.         seekBar.Enabled = true;  
  63.         DismissIndeterminateProgressDialog();  
  64.     }, TaskScheduler.FromCurrentSynchronizationContext());  
  65. }  
  66. }  
  67. public class MyProgressDialog: DialogFragment {  
  68.         public override Dialog OnCreateDialog(Bundle savedInstanceState) {  
  69.             ProgressDialog dialog = new ProgressDialog(Activity);  
  70.             dialog.SetTitle(Resource.String.dialogtitle);  
  71.             dialog.SetMessage(GetString(Resource.String.dialogmessage));  
  72.             dialog.Indeterminate = true;  
  73.             dialog.SetCancelable(false);  
  74.             return dialog;  
  75.         }  
Project

Step 19

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)).

Click the Run option.

Project

Output

After a few seconds, the app will start running on your phone.

Project
 
Use the Seek bar to blur the image.

Project

Summary

So, this was the process of blurring an image in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all