Skip to content

Instantly share code, notes, and snippets.

@yumi0629
Created February 14, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yumi0629/5023b04fe7e71dccad914b119eec2b56 to your computer and use it in GitHub Desktop.
Save yumi0629/5023b04fe7e71dccad914b119eec2b56 to your computer and use it in GitHub Desktop.
fun BaseQuickAdapter<*, BaseViewHolder>.shotRecyclerView(
recyclerView: RecyclerView, bgColor: Int? = null): Bitmap {
val size = itemCount
var height = 0
val maxMemory: Int = ((Runtime.getRuntime().maxMemory() / 1024).toInt())
// Use 1/8th of the available memory for this memory cache.
val cacheSize = maxMemory / 8
val bitmapCache: LruCache<String, Bitmap> = LruCache(cacheSize)
for (i in 0 until size) {
var itemView: View?
itemView = when (getItemViewType(i)) {
BaseQuickAdapter.HEADER_VIEW -> headerLayout
else -> {
val holder = createViewHolder(recyclerView, getItemViewType(i))
onBindViewHolder(holder, i)
holder.itemView
}
}
itemView?.run {
measure(
View.MeasureSpec.makeMeasureSpec(recyclerView.width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
layout(left, top, measuredWidth, measuredHeight)
isDrawingCacheEnabled = true
buildDrawingCache()
var bitmap: Bitmap? = drawingCache
if (getItemViewType(i) == BaseQuickAdapter.HEADER_VIEW) {
// header需要特殊处理,因为在滑动时header的top会变成负数,导致截图其和第一条item之间会有空隙,所以要把空隙裁掉
fun cropBitmap(bmp: Bitmap?): Bitmap? {
return bmp?.let {
Bitmap.createBitmap(
bmp, 0, 0, width, it.height + headerLayout.top, null, false)
}
}
bitmap = cropBitmap(bitmap)
}
bitmapCache.put("$i", bitmap)
height += measuredHeight
// isDrawingCacheEnabled = false
// destroyDrawingCache()
}
}
val bigBitmap = Bitmap.createBitmap(recyclerView.measuredWidth, height, Bitmap.Config.ARGB_8888)
val bigCanvas = Canvas(bigBitmap)
// draw background if necessary
val lBackground: Drawable? = recyclerView.background
if (bgColor == null && lBackground == null) {
bigCanvas.drawColor(Color.WHITE)
} else if (lBackground != null && lBackground is ColorDrawable) {
bigCanvas.drawColor(lBackground.color)
} else {
bgColor?.let { bigCanvas.drawColor(it) }
}
// drawBitmap
var iHeight = 0f
for (i in 0 until size) {
val bitmap: Bitmap? = bitmapCache.get("$i")
bitmap?.run {
bigCanvas.drawBitmap(bitmap, 0f, iHeight, Paint())
iHeight += this.height
}
// 如果recycle掉了,第二次点击截图的时候会报错Canvas: trying to use a recycled bitmap android.graphics.Bitmap@xxx
// bitmap.recycle()
}
return bigBitmap
}
fun BaseQuickAdapter<*, BaseViewHolder>.shotRecyclerView(recyclerView: RecyclerView): Bitmap {
return shotRecyclerView(recyclerView, null)
}
fun BaseQuickAdapter<*, BaseViewHolder>.shotRecyclerViewHeader(): Bitmap {
return shotRecyclerViewHeader(null)
}
fun BaseQuickAdapter<*, BaseViewHolder>.shotRecyclerViewHeader(bgColor: Int? = null): Bitmap {
val bigBitmap = Bitmap.createBitmap(
headerLayout.measuredWidth, headerLayout.measuredHeight, Bitmap.Config.ARGB_8888)
headerLayout?.run {
val bigCanvas = Canvas(bigBitmap)
// draw background if necessary
val lBackground: Drawable? = background
if (bgColor == null && lBackground == null) {
bigCanvas.drawColor(Color.WHITE)
} else if (lBackground != null && lBackground is ColorDrawable) {
bigCanvas.drawColor(lBackground.color)
} else {
bgColor?.let { bigCanvas.drawColor(it) }
}
// drawBitmap
draw(bigCanvas)
}
return bigBitmap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment