Tome una captura de pantalla de RecyclerView en longitud COMPLETA

Quiero obtener una "página completa" captura de pantalla de la actividad. La vista contiene un RecyclerView con muchos elementos.

Puedo tomar una captura de pantalla de la vista actual con esta función:

public Bitmap getScreenBitmap() { View v= findViewById(R.id.container).getRootView(); v.setDrawingCacheEnabled(true); v.buildDrawingCache(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); // clear drawing cache return b; } 

Pero contiene sólo los elementos que puedo ver normalmente (como se esperaba).

¿Hay alguna manera de hacer el RecyclerView mágicamente mostrar en toda su longitud (mostrar todos los elementos a la vez) cuando tomo la captura de pantalla?

Si no es así, ¿cómo debo abordar este problema?

Aquí está mi solución para LinearLayoutManager cuando todos los artículos están en el mismo tamaño y hay solamente un tipo de artículo . Esta solución se basa en esta respuesta .

Nota : Puede llevar a un error de memoria.

 public static Bitmap getRecyclerViewScreenshot(RecyclerView view) { int size = view.getAdapter().getItemCount(); RecyclerView.ViewHolder holder = view.getAdapter().createViewHolder(view, 0); view.getAdapter().onBindViewHolder(holder, 0); holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight()); Bitmap bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), holder.itemView.getMeasuredHeight() * size, Bitmap.Config.ARGB_8888); Canvas bigCanvas = new Canvas(bigBitmap); bigCanvas.drawColor(Color.WHITE); Paint paint = new Paint(); int iHeight = 0; holder.itemView.setDrawingCacheEnabled(true); holder.itemView.buildDrawingCache(); bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint); holder.itemView.setDrawingCacheEnabled(false); holder.itemView.destroyDrawingCache(); iHeight += holder.itemView.getMeasuredHeight(); for (int i = 1; i < size; i++) { view.getAdapter().onBindViewHolder(holder, i); holder.itemView.setDrawingCacheEnabled(true); holder.itemView.buildDrawingCache(); bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint); iHeight += holder.itemView.getMeasuredHeight(); holder.itemView.setDrawingCacheEnabled(false); holder.itemView.destroyDrawingCache(); } return bigBitmap; } 

Nota 2: Se ha escrito originalmente en Kotlin. Aquí está el código original usado por mí.

Inspirado en la respuesta de Yoav. Este código funciona para los tipos de elementos de recyclerview y, probablemente, independientemente de su tamaño.

Se probó con un recyclerview que tiene el encargado linearlayout y tres tipos del artículo. Sin embargo, para comprobarlo con otros gestores de diseño.

 public Bitmap getScreenshotFromRecyclerView(RecyclerView view) { RecyclerView.Adapter adapter = view.getAdapter(); Bitmap bigBitmap = null; if (adapter != null) { int size = adapter.getItemCount(); int height = 0; Paint paint = new Paint(); int iHeight = 0; final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize); for (int i = 0; i < size; i++) { RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i)); adapter.onBindViewHolder(holder, i); holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight()); holder.itemView.setDrawingCacheEnabled(true); holder.itemView.buildDrawingCache(); Bitmap drawingCache = holder.itemView.getDrawingCache(); if (drawingCache != null) { bitmaCache.put(String.valueOf(i), drawingCache); } // holder.itemView.setDrawingCacheEnabled(false); // holder.itemView.destroyDrawingCache(); height += holder.itemView.getMeasuredHeight(); } bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888); Canvas bigCanvas = new Canvas(bigBitmap); bigCanvas.drawColor(Color.WHITE); for (int i = 0; i < size; i++) { Bitmap bitmap = bitmaCache.get(String.valueOf(i)); bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint); iHeight += bitmap.getHeight(); bitmap.recycle(); } } return bigBitmap; } 
  • Generar código java en Eclipse?
  • El Jar de este archivo de clase blongs al contenedor Android 2.0.1 que no permite modificaciones
  • Porcentaje de codificación en un URI
  • Rotar Marcador y Mover Animación en Mapa como Uber Android
  • Buen diseño de la interfaz de usuario: ¿Cómo manejar ListView vacío?
  • Android-java: comprueba la comprobación de valores booleanos para null
  • Problema al abrir el archivo de elementos con la ayuda del proveedor de contenido
  • Android Spinner - ¿Cómo hacer que la vista desplegable sea transparente?
  • ¿Cómo puedo escribir una declaración posterior a una HttpUrlConnection que está comprimida?
  • Tela que intenta utilizar un mapa de bits reciclado
  • Android - Establecer dinámicamente el gradiente de drawable
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.