How to create Blur Effect on an Image in Android

Full source code to create Blur Effect on an Image using a 3rd party library called BlurImageView to add a blur effect to an ImageView. We will also change the intensitiy with a SeekBar using the OnSeekBarChangeListener onProgressChanged method

Add the JitPack repository to your build file

allprojects {
		repositories {
			...
			maven { url "https://jitpack.io" }
		}
	}

Add the dependency

dependencies {
		implementation 'com.github.jgabrielfreitas:BlurImageView:1.0.1'
	}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.application.blurproject.MainActivity">

    <com.jgabrielfreitas.core.BlurImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_above="@id/seekBar"
        android:layout_alignParentTop="true"
        android:scaleType="centerCrop"
        android:src="@drawable/image" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

MainActivity.java

package com.example.application.blurproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;

import com.jgabrielfreitas.core.BlurImageView;

public class MainActivity extends AppCompatActivity {

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

        final BlurImageView blurImageView = (BlurImageView) findViewById(R.id.imageView);
        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                blurImageView.setBlur(progress / 4);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

Leave a Comment