0

I want to take a picture of a view in Activity and share this picture. First I use this method :

private Bitmap getShareViewShot() {
    this.setDrawingCacheEnabled(true);
    this.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache(), 0, mSharePageRoot.getTop(), this.getWidth(), mSharePageRoot.getBottom());
    this.setDrawingCacheEnabled(false);
    return bitmap;
}

But if the view is in a ScrollView and the view has scrolled outside the screen, this method can't get a whole picture of this view.

So I change this method to below :

private Bitmap getShareViewShot() {
    Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
            Bitmap.Config.ARGB_8888);
    mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
    final Canvas canvas = new Canvas(bitmap);
    mSharePageRoot.draw(canvas);
    return bitmap;    
}

But another question comes: this view has no backgroud, so the picture I get has a black backgroud.In my code ,the Activity have a network image as the backgroud, so this means this method can't get the backgroud of the activity.

Please forgive me for my bad English. Is there any other method ? thanks.

Charon Chui
  • 71
  • 2
  • 8

2 Answers2

0

Yes, there is another method. There is a library by google called ASL(Android screenshot library). It's pretty simple.

https://code.google.com/archive/p/android-screenshot-library/wikis/DeveloperGuide.wiki

Hossam Alaa
  • 663
  • 5
  • 9
0

You can draw background by canvas, like this:

private Bitmap getShareViewShot() {
    Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
        Bitmap.Config.ARGB_8888);
    mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
    final Canvas canvas = new Canvas(bitmap);
    // the bitmap will have a white background
    canvas.drawColor(0xffffffff);
    mSharePageRoot.draw(canvas);
    return bitmap;    
}
HelloBird
  • 69
  • 5
  • The backgroud is a network image, if draw it ,it may be not loaded.But I can only use this way, listener download finish and use the backgroud image to draw. Thank you. – Charon Chui Apr 21 '16 at 11:42