Skip to content



Blur Mask Filter on Bitmap in Android



07 October 2013no comments Android Posts, Android Tutorials

5,655 Total Views

apply BlurMaskFilter on Bitmap

[subscribe2]This takes a mask, and blurs its edge by the specified radius.
Whether or or not to include the original mask, and whether
the blur goes outside, inside, or straddles, the original mask’s border,
is controlled by the Blur enum.


package com.example.androiddrawbitmap;
import java.io.FileNotFoundException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

 Button btnLoadImage;
 ImageView imageResult;

 final int RQS_IMAGE1 = 1;

 Uri source;
 Bitmap bitmapMaster;
 Canvas canvasMaster;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btnLoadImage = (Button) findViewById(R.id.loadimage);
  imageResult = (ImageView) findViewById(R.id.result);

  btnLoadImage.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    Intent intent = new Intent(
      Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RQS_IMAGE1);
   }
  });
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  if (resultCode == RESULT_OK) {
   switch (requestCode) {
   case RQS_IMAGE1:
    source = data.getData();

    try {
     bitmapMaster = BitmapFactory
       .decodeStream(getContentResolver().openInputStream(
         source));
     loadGrayBitmap(bitmapMaster);

    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

    break;
   }
  }
 }

 private void loadGrayBitmap(Bitmap src) {
  if (src != null) {

   int w = src.getWidth();
   int h = src.getHeight();
   RectF rectF = new RectF(w/4, h/4, w*3/4, h*3/4);
   float blurRadius = 100.0f;

   Bitmap bitmapResult = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
   Canvas canvasResult = new Canvas(bitmapResult);

         Paint blurPaintOuter = new Paint();
         blurPaintOuter.setColor(0xFFffffff);
         blurPaintOuter.setMaskFilter(new 
           BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.INNER));
         canvasResult.drawBitmap(bitmapMaster, 0, 0, blurPaintOuter);

         Paint blurPaintInner = new Paint();
         blurPaintInner.setColor(0xFFffffff);
         blurPaintInner.setMaskFilter(new 
           BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.OUTER));
         canvasResult.drawRect(rectF, blurPaintInner);

   imageResult.setImageBitmap(bitmapResult);
  }
 }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/background_dark"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/loadimage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Load Image 1" />

    <ImageView
                android:id="@+id/result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="centerInside" />

</LinearLayout>

download filesDownload the files.

Spread The Love, Share Our Article

  • Delicious
  • Digg
  • Newsvine
  • RSS
  • StumbleUpon
  • Technorati
  • Twitter

Related Posts

There are no related posts on this entry.

Facebook Iconfacebook like buttonTwitter Icontwitter follow buttonFollow Us
Follow

Get every new post delivered to your Inbox

Join other followers

%d bloggers like this: