1

I want to blur the background of an imageview. I am using single image-view right now. So is it possible to do that in single image-view or should i have to use two image-view's (one for the background and one for the foreground).

I want to achieve something like this

enter image description here

ps_who
  • 137
  • 2
  • 9

2 Answers2

1

try----->>

XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="400dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
   android:scaleType="centerCrop"      // scaletype depends upon image you adjust or dont put
    android:id="@+id/customProfileGridImg"
    android:layout_width="match_parent"
    android:layout_height="400dp"
        />
<ImageView
    android:layout_width="match_parent"      
    android:layout_height="500dp"
    android:scaleType="centerCrop"     // scaletype depends upon image you adjust or dont put
    android:src="@drawable/nature"
    android:layout_centerInParent="true"/>
</RelativeLayout>

java-->

public class MainActivity  extends AppCompatActivity {

private static final float BLUR_RADIUS = 25f;

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

     imageView = (ImageView) findViewById(R.id.customProfileGridImg);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
    Bitmap blurredBitmap = blur(bitmap);
    imageView.setImageBitmap(blurredBitmap);


}

public Bitmap blur(Bitmap image) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

   //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}
  }

OUTPUT

enter image description here

Wini
  • 1,906
  • 1
  • 12
  • 31
0

Use Picasso for image related tasks. It has many useful predefined functionalities. For your reference checkout this link how to blur using picasso.Picasso image blur

ashik
  • 1
  • 2