In one of my project there was a need to show previous activity contents(views) into the present activity., Any guess??! I had too many ideas to implement this but none worked. At last this SO discussion gave me a solution for this problem. Solutions is simple,

  1. Create a Bitmap from your activity’s overall layout
  2. Blur that bitmap with whatever method you want
  3. Add(or unhide) a View in your layout that covers everything. Set background to blurred Bitmap.
  4. Open your dialog/transparent activity
  5. When it closes, either remove the View, or setVisibility(GONE) if you’ll be using it again

Follow these steps.,

  • Converting Activity’s layout to bitmap
public class MainActivity extends Activity {
private RelativeLayout baseLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseLayout = (RelativeLayout)findViewById(R.id.container);
baseLayout.setDrawingCacheEnabled(true);
baseLayout.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
baseLayout.layout(0, 0,baseLayout.getMeasuredWidth(),baseLayout.getMeasuredHeight());
baseLayout.buildDrawingCache(true);
Bitmap bMap = Bitmap.createBitmap(baseLayout.getDrawingCache());
//    sending this image to secondActivity
startActivity(new Intent(this,SecondActivity.class).putExtra("image", bMap));
}
}

As a result of above code we can now get Image, but still it is not blurred.

  • Blur the image with any method

I tried much more methods as mentioned in internet, but none worked for me. At last I found this link which saved my life. I modified slightly it to fit as java code.

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bitmap result = BlurImage((Bitmap)getIntent().getParcelableExtra("image"));
((ImageView)findViewById(R.id.secondImage)).setImageBitmap(result);
}
@SuppressLint("NewApi")
private Bitmap BlurImage(Bitmap input) { 
RenderScript rsScript = RenderScript.create(getApplicationContext());
Allocation alloc = Allocation.createFromBitmap(rsScript, input);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, alloc.getElement());
blur.setRadius(12);
blur.setInput(alloc);
Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig ());
Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);
blur.forEach(outAlloc);
outAlloc.copyTo(result);
rsScript.destroy();
return result;
}
}

Source code of this project is at github.
MainActivity

Screenshot_2014-05-31-20-17-07

SecondActivity

Screenshot_2014-05-31-20-17-15

Enjoy ….!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s