新增方法

This commit is contained in:
renhaoting 2026-01-10 16:08:44 +08:00
parent 0f8a41e1a1
commit d5e9bacab4
1 changed files with 34 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import android.view.Gravity
import android.view.PixelCopy
import android.view.View
import android.view.Window
import android.webkit.WebView
import android.widget.Toast
import androidx.core.app.NotificationManagerCompat
import androidx.core.graphics.createBitmap
@ -152,7 +153,7 @@ class AndroidUtil private constructor() {
}
fun captureView(window: Window, targetView: View, callback: (Bitmap?) -> Unit) {
fun getThumbOfView1(window: Window, targetView: View, callback: (Bitmap?) -> Unit) {
val location = IntArray(2)
targetView.getLocationInWindow(location)
val (x, y) = location
@ -185,6 +186,38 @@ class AndroidUtil private constructor() {
return bitmap
}
fun getThumbOfView3(targetView: View): Bitmap {
val bitmap = Bitmap.createBitmap(
targetView.width,
targetView.height,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
targetView.draw(canvas)
return bitmap
}
fun getThumbOfView4(webView : WebView): Bitmap? {
val picture = webView.capturePicture();
val width = picture.getWidth();
val height = picture.getHeight();
if (width <= 0 || height <= 0) {
return null
}
try {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
picture.draw(canvas)
return bitmap
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
inline fun <reified T> json2Object(jsonString: String): T? {