Android页面View截图(分享、保存相册)
Android View 截屏上下拼接
Android View截屏长图拼接(RecyclerView)
Android View截屏长图拼接(NestedScrollView)
RecyclerView截屏,其实就是遍历每个itemView,得到每个itemView的Bitmap,然后拼接成一个Bitmap。根据这个思路我们具体实现代码:
ublic static Bitmap bitmapByRecyclerView(RecyclerView recyclerView) {RecyclerView.Adapter adapter = recyclerView.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<Integer, Bitmap> bitmapCache = new LruCache<>(cacheSize);for (int i = 0; i < size; i++) {RecyclerView.ViewHolder holder = adapter.createViewHolder(recyclerView, adapter.getItemViewType(i));adapter.onBindViewHolder(holder, i);holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(recyclerView.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){drawingCache = Bitmap.createBitmap(holder.itemView.getWidth(), holder.itemView.getHeight(), Bitmap.Config.RGB_565);Canvas itemCanvas = new Canvas(drawingCache);holder.itemView.draw(itemCanvas);}if (drawingCache != null) {bitmapCache.put(i, drawingCache);}height += holder.itemView.getMeasuredHeight();}bigBitmap = Bitmap.createBitmap(recyclerView.getMeasuredWidth(), height, Bitmap.Config.RGB_565);Canvas bigCanvas = new Canvas(bigBitmap);Drawable lBackground = recyclerView.getBackground();if (lBackground instanceof ColorDrawable) {ColorDrawable lColorDrawable = (ColorDrawable) lBackground;int lColor = lColorDrawable.getColor();bigCanvas.drawColor(lColor);}for (int i = 0; i < size; i++) {Bitmap bitmap = bitmapCache.get(i);if (bitmap != null){bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);iHeight += bitmap.getHeight();bitmap.recycle();}}}return bigBitmap;}