Created
June 8, 2015 15:03
-
-
Save PrashamTrivedi/809d2541776c8c141d9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
When this method be called more than once will be crash!!!
image is not shown using this method. if we are putting image statically it will show, but mostly in recycler view we use dynamic content and this image is not caught .
Inspired from : http://stackoverflow.com/questions/30085063/take-a-screenshot-of-recyclerview-in-full-length
Is it Inspired by or Copied from??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When the item count of the RecylerView is too big or the memory is not enough,we will not get the big map。It may cause the OOM。