Skip to content

Instantly share code, notes, and snippets.

@mrrobot97
Last active January 12, 2021 08:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrobot97/c6be0b2b9524ee3028c79cedffdd2ec0 to your computer and use it in GitHub Desktop.
Save mrrobot97/c6be0b2b9524ee3028c79cedffdd2ec0 to your computer and use it in GitHub Desktop.
X5WebView Capture Screen
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Point
import com.tencent.smtt.sdk.WebView
/**
* Created by yangjw on 2019/3/14.
*/
fun WebView?.getSize(): Point? {
if (this == null) {
return null
}
return Point(computeHorizontalScrollRange(), computeVerticalScrollRange())
}
fun WebView?.getScreenShot(startX: Int, startY: Int, endX: Int, endY: Int): Bitmap? {
if (this == null || startX < 0 || startY < 0 || endX < 0 || endY < 0) {
return null
}
val size = getSize() ?: return null
val wholeWidth = size.x
val wholeHeight = size.y
val wholePageBitmap = Bitmap.createBitmap(wholeWidth, wholeHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(wholePageBitmap)
canvas.scale(wholeWidth.toFloat() / contentWidth, wholeHeight.toFloat() / contentHeight)
if (x5WebViewExtension == null) {
return null
}
x5WebViewExtension.snapshotWholePage(canvas, false, false)
if (startX == 0 && startY == 0 && endX == wholeWidth && endY == wholeHeight) {
return wholePageBitmap
}
return Bitmap.createBitmap(wholePageBitmap, startX, startY, endX - startX, endY - startY)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment