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:
- ¿Podemos instanciar una clase abstracta?
- Tamaño de vista previa para el escáner de código de barras de la visión api
- Gradle Android Build Project Error: No se pudo encontrar el recurso de metadatos
- Editar mediante programación las propiedades de build.prop en Android
- ¿Qué debo usar en Android cuando portar código C ++ escrito con libsndfile?
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?
- Cómo ocultar una vista mediante programación?
- ¿Cómo crear una alarma cuando se crea un evento de calendario?
- Cargando una imagen usando OpenCV en Android
- ¿Cuándo usar un Servicio o AsyncTask o Handler?
- System.out.println en la prueba de Android
- Java: Diferentes resultados al decodificar cadena base64 con java.util.Base64 vs android.util.Base64
- Conexión de http de Android con OkHttp No funciona
- Eliminar una cadena en todos los idiomas Android
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; }
- Eclipse dice que una versión más reciente de ADT es necesaria, pero no puede encontrar una actualización
- Tipo de letra para Android DatePicker