studio programacion herramientas fundamentos con avanzado aplicaciones android webview capture

programacion - manual de android en pdf



Cómo capturar una vista web a mapa de bits en Android? (2)

Tengo una vista web y necesito desplazarme hacia abajo para ver todo el contenido. Ahora, quiero capturar la totalidad de la vista web en un mapa de bits.

Lo busqué muchas veces. La gente me sugirió usar la función capturePicture() . Sin embargo, esta función está en desuso . Entonces, ¿qué otros métodos puedo usar para alcanzar mi objetivo?

Gracias a todos.


Android L, debe llamar a WebView.enableSlowWholeDocumentDraw () antes de crear cualquier WebViews. Es decir, si tiene algún WebViews en su diseño, asegúrese de llamar a este método antes de llamar a setContentView () en su onCreate ().
vinculado Mikhail Naganov : https://.com/a/30084485/703225

@Override @TargetApi(Build.VERSION_CODES.LOLLIPOP) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { WebView.enableSlowWholeDocumentDraw(); } ... setContentView(layout); ... ... }

entonces:

/** * WevView screenshot * * @param webView * @return */ public static Bitmap screenshot(WebView webView, float scale11) { try { float scale = webView.getScale(); int height = (int) (webView.getContentHeight() * scale + 0.5); Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); webView.draw(canvas); return bitmap; } catch (Exception e) { e.printStackTrace(); } return null; }

si no usa webView.getScale (), también:

public static Bitmap screenshot2(WebView webView) { webView.measure(MeasureSpec.makeMeasureSpec( MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight()); webView.setDrawingCacheEnabled(true); webView.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(), webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); int iHeight = bitmap.getHeight(); canvas.drawBitmap(bitmap, 0, iHeight, paint); webView.draw(canvas); return bitmap; }


Descubrí la solución. Echa un vistazo a esto.

Utilicé el onDraw para reemplazar la función capturePicture que está obsoleta en la API 19. Si tiene alguna solución mejor, por favor dígame y la valoro. ¡Gracias!

Que puede reemplazar la función capturePicture