10

I'm currently working on a project which needs to display a dialog with a grayout (black/white) background. To achieve this I'm taking a screenshot and of the whole app, place this screenshot on the background of the fullscreen dialog and put an ColorFilter on it to have it grayed out.

This works perfect for the first time, but if I scroll in the underlaying content and request the dialog again, it shows just the same background as the one before.

I use the code:

Bitmap bitmap;
View rootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
rootView.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
imageView.setImageBitmap(bitmap);

In other words, the getDrawingCache() always returns the same screenshot of the app.

Ben Groot
  • 5,040
  • 3
  • 40
  • 47

2 Answers2

26

I think that's because your old Bitmap is still in your drawing cache. Because of this, you first need to delete it from the cache and then put the new Image in the cache. Take a look at this question, which seems to be on the same topic:

Deletion of drawing cache

EDIT: So, here is the code which is working for me. I use a Button to save the Bitmap and then set the Bitmap to an Image View:

private View rootView;
private ImageView bitmapView;
private Button switchButton;
public Bitmap capturedScreen;
public boolean bitmapNeeded = false;

...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // do the other stuff
    rootView.setDrawingCacheEnabled(true); //enable Drawing cache on your view
    switchButton.setOnClickListener(this);
}

...

@Override
public void onClick(View v) {
    if (v == switchButton) { //when the button is clicked
        captureScreen();
    }
}

public void captureScreen() {
    rootView.buildDrawingCache();       
    capturedScreen = Bitmap.createBitmap(rootView.getDrawingCache());
    imageView.setImageBitmap(capturedScreen);
    rootView.destroyDrawingCache();
}

.... 

//In the onDraw method of your View:
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(capturedScreen, 0, 0, paint);
}

This is how it works: Everytime the user clicks the button, everything inside rootView is saved as a bitmap and then drawn to the imageView. You can of course call the captureScreen Method from anywhere in your code, if you need to to.

I hope this example helps you.

Nils Schlüter
  • 1,418
  • 1
  • 19
  • 30
  • Hi nschl, I've tried to recycle and nullify the bitmap before calling getDrawingCache with no luck. Beside that the whole recycle method is no longer useful in Honeycomb+ right? – Ben Groot Mar 24 '14 at 15:02
  • 1
    Hey Ben groot, I think I have an example on my compluter which does what you want to achieve. I will take a look at it and post it here as soon as I'm at home. – Nils Schlüter Mar 24 '14 at 16:26
  • 1
    So, I added the code. Take a look at it and let me know if it helped you. – Nils Schlüter Mar 24 '14 at 18:37
  • 1
    I'm sorry to say, but it only works occasionally. I haven't found the pattern to reproduce it yet. – Ben Groot Mar 27 '14 at 12:51
0
    RelativeLayout rlThemeOne=findviewbyId(R.id.rlThemeOne);
    rlThemeOne.destroyDrawingCache();   //destroy preview Created cache if any
    rlThemeOne.buildDrawingCache();    //build new cache
    Bitmap bitmap = rlThemeOne.getDrawingCache();  //get the cache in bitmap
Adnan Malik
  • 67
  • 1
  • 3