Gaëtan Renaudeau 8 years ago
parent
commit
12b23da501
2 changed files with 18 additions and 2 deletions
  1. 0
    1
      README.md
  2. 18
    1
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

+ 0
- 1
README.md View File

48
 - Support of special components like Video / GL views remains untested.
48
 - Support of special components like Video / GL views remains untested.
49
 - It's preferable to **use a background color on the view you rasterize** to avoid transparent pixels and potential weirdness that some border appear around texts.
49
 - It's preferable to **use a background color on the view you rasterize** to avoid transparent pixels and potential weirdness that some border appear around texts.
50
 - on Android, you need to make sure `collapsable` is set to `false` if you want to snapshot a **View**. Otherwise that view won't reflect any UI View. ([found by @gaguirre](https://github.com/gre/react-native-view-shot/issues/7#issuecomment-245302844))
50
 - on Android, you need to make sure `collapsable` is set to `false` if you want to snapshot a **View**. Otherwise that view won't reflect any UI View. ([found by @gaguirre](https://github.com/gre/react-native-view-shot/issues/7#issuecomment-245302844))
51
-- on Android, you can't snapshot a big view. The limit looks like to be set to your screen size (number of pixels).
52
 
51
 
53
 ## Getting started
52
 ## Getting started
54
 
53
 

+ 18
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java View File

2
 
2
 
3
 import javax.annotation.Nullable;
3
 import javax.annotation.Nullable;
4
 import android.graphics.Bitmap;
4
 import android.graphics.Bitmap;
5
+import android.graphics.Canvas;
5
 import android.net.Uri;
6
 import android.net.Uri;
6
 import android.util.Base64;
7
 import android.util.Base64;
7
 import android.view.View;
8
 import android.view.View;
103
         }
104
         }
104
     }
105
     }
105
 
106
 
107
+    public static Bitmap loadBitmapFromView(View v)
108
+    {
109
+        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
110
+
111
+        Canvas c = new Canvas(b);
112
+        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
113
+        v.draw(c);
114
+        return b;
115
+    }
116
+
106
     /**
117
     /**
107
      * Screenshot a view and return the captured bitmap.
118
      * Screenshot a view and return the captured bitmap.
108
      * @param view the view to capture
119
      * @param view the view to capture
114
         if (w <= 0 || h <= 0) {
125
         if (w <= 0 || h <= 0) {
115
             throw new RuntimeException("Impossible to snapshot the view: view is invalid");
126
             throw new RuntimeException("Impossible to snapshot the view: view is invalid");
116
         }
127
         }
128
+        /*
117
         if (!view.isDrawingCacheEnabled())
129
         if (!view.isDrawingCacheEnabled())
118
           view.setDrawingCacheEnabled(true);
130
           view.setDrawingCacheEnabled(true);
119
 
131
 
120
         Bitmap bitmap = view.getDrawingCache();
132
         Bitmap bitmap = view.getDrawingCache();
133
+        */
134
+        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
135
+        Canvas c = new Canvas(bitmap);
136
+        view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
137
+        view.draw(c);
121
 
138
 
122
         if (width != null && height != null && (width != w || height != h)) {
139
         if (width != null && height != null && (width != w || height != h)) {
123
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
140
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
126
             throw new RuntimeException("Impossible to snapshot the view");
143
             throw new RuntimeException("Impossible to snapshot the view");
127
         }
144
         }
128
         bitmap.compress(format, (int)(100.0 * quality), os);
145
         bitmap.compress(format, (int)(100.0 * quality), os);
129
-        view.setDrawingCacheEnabled(false);
146
+        //view.setDrawingCacheEnabled(false);
130
     }
147
     }
131
 }
148
 }