title: Android Blur View (Blur background behind the view)
body: <p>I am trying to make the bottom part of an Image blur for the view on top it like in the image.</p>
  
  <p>I tried blurring it using Rendenscript but I am not able to blur only the part behind the view. :(</p>
  
  <p>I have seen many libraries but almost all of them blur the entire image, but not a part of it.</p>
  
  <p>Also, an important part is that I am using this inside a ViewPager and hence needs to be fast and dynamic something like <a href="https://github.com/nicklockwood/FXBlurView" rel="noreferrer">this in IOS</a> which redraws itself the moment image behind it changes.</p>
  
  <p>Any help is appreciated. Thanks for stopping by!</p>
  
  <p>My xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:gravity="center"
      android:orientation="vertical" &gt;
  
      &lt;ImageView
          android:id="@+id/image"
          android:src="@drawable/broadstairs"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scaleType="centerCrop"
          android:layout_centerInParent="true"/&gt;
  
      &lt;TextView
          android:id="@+id/text"
          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:text="Hello World"
          android:gravity="center"
          android:layout_alignParentBottom="true"
          android:textColor="@android:color/white"
          android:textStyle="bold"
          android:textSize="36sp"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My code:</p>
  
  <p>BlurBuilder.java</p>
  
  <pre><code>public class BlurBuilder {
      private static final float BITMAP_SCALE = 0.1f;
      private static final float BLUR_RADIUS = 7.5f;
  
      public static Bitmap blur(Context context, Bitmap image) {
          int width = Math.round(image.getWidth() * BITMAP_SCALE);
          int height = Math.round(image.getHeight() * BITMAP_SCALE);
  
          Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
          Bitmap 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;
      }
  
      @SuppressLint("NewApi")
      public static void blur(final Context context, final Bitmap bitmap, final View view) {
  
          new AsyncTask&lt;Void, Void, Bitmap&gt;() {
  
              @Override
              protected Bitmap doInBackground(Void... params) {
                  Paint paint = new Paint();
                  paint.setFilterBitmap(true);
                  Bitmap cropImage = Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() - view.getHeight(), bitmap.getWidth(), view.getHeight());
  
                  return BlurBuilder.blur(context, cropImage);
              }
  
  
              @Override
              protected void onPostExecute(Bitmap bitmap) {
                  super.onPostExecute(bitmap);
                  if (bitmap != null) {
                      int sdk = android.os.Build.VERSION.SDK_INT;
                      if (sdk &lt; android.os.Build.VERSION_CODES.JELLY_BEAN) {
                          view.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bitmap));
                      } else {
                          view.setBackground(new BitmapDrawable(context.getResources(), bitmap));
                      }
  
                  }
  
              }
          }.execute();
  
  
      }
  
  }
  </code></pre>
  
  <p>In my MainActivity's onCreate I do:</p>
  
  <pre><code>BlurBuilder.blur(BitmapActivity.this, ((BitmapDrawable) mView.getDrawable()).getBitmap(), mDummyView);
  </code></pre>
  
  <p>Below is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/9RwVY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9RwVY.png" alt="enter image description here"></a></p>