您现在的位置是:主页 > news > 做网站的公司为什么人少了/百度引擎搜索网址
做网站的公司为什么人少了/百度引擎搜索网址
admin2025/6/27 22:41:14【news】
简介做网站的公司为什么人少了,百度引擎搜索网址,做网站怎么设置背景,科大讯飞哪些做教学资源的网站Android 实现全屏截图、剪裁、分享功能 项目中需要用到 截图分享 的功能,特此写下查询资料过程中的踩坑记录。 android 26以上,google 官方文档支持 PixelCopy 实现截图效果 获取虚拟导航栏的高度并剪裁图片 1、全屏截图 1.1 踩坑记录 第一个坑&a…
做网站的公司为什么人少了,百度引擎搜索网址,做网站怎么设置背景,科大讯飞哪些做教学资源的网站Android 实现全屏截图、剪裁、分享功能
项目中需要用到 截图分享 的功能,特此写下查询资料过程中的踩坑记录。 android 26以上,google 官方文档支持 PixelCopy 实现截图效果 获取虚拟导航栏的高度并剪裁图片
1、全屏截图
1.1 踩坑记录
第一个坑&a…
Android 实现全屏截图、剪裁、分享功能
项目中需要用到 截图分享 的功能,特此写下查询资料过程中的踩坑记录。
android 26以上,google 官方文档支持 PixelCopy 实现截图效果
获取虚拟导航栏的高度并剪裁图片
1、全屏截图
1.1 踩坑记录
第一个坑:方法已被弃用
(先前实现的全屏截图方法,已经在api28及以上弃用)
// View是全屏截图 **已弃用**View getView = this.getWindow().getDecorView();getView.setDrawingCacheEnabled(true);getView.buildDrawingCache();Bitmap b1 = getView.getDrawingCache();
第二个坑:能实现全屏截图,但是 MaterialCardView 圆角显示失效
//获取全屏截图(包括状态栏、标题栏和底部)val screenView = window.decorViewval bitmap = screenView.drawToBitmap()
1.2 正确截图方式
经过查询资料,终于找到了能正确显示 MaterialCardView 圆角的截图方式
val EVENT_SCREENSHOT = 22 //截图事件private var mediaProjectionManager: MediaProjectionManager? = nullprivate var mediaProjection: MediaProjection? = nullprivate var image: Image? = nullprivate fun takeScreenShot() {mediaProjectionManager = application.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManagerstartActivityForResult(mediaProjectionManager!!.createScreenCaptureIntent(), EVENT_SCREENSHOT)}@SuppressLint("WrongConstant")override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)if(requestCode == EVENT_SCREENSHOT_LOCK ||requestCode == EVENT_SCREENSHOT_SHARE){val displayMetrics = resources.displayMetricsval width = displayMetrics.widthPixelsval height = displayMetrics.heightPixelsval mImageReader: ImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)mediaProjection = mediaProjectionManager!!.getMediaProjection(resultCode, data!!)val virtualDisplay = mediaProjection!!.createVirtualDisplay("screen-mirror", width, height,displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.surface, null, null)Handler(Looper.myLooper()!!).postDelayed({try {image = mImageReader.acquireLatestImage()if (image != null) {val planes: Array<Image.Plane> = image!!.planesval buffer: ByteBuffer = planes[0].bufferval width: Int = image!!.widthval height: Int = image!!.heightval pixelStride: Int = planes[0].pixelStrideval rowStride: Int = planes[0].rowStrideval rowPadding = rowStride - pixelStride * widthvar bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888)bitmap!!.copyPixelsFromBuffer(buffer)bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width, bitmap.height, false)if (bitmap != null) {if (requestCode == EVENT_SCREENSHOT_SHARE) {cutScreenShotToShare(bitmap)}else if(requestCode == EVENT_SCREENSHOT_LOCK){cutScreenShotToLock(bitmap)}}bitmap.recycle()}} catch (e: Exception) {e.printStackTrace()} finally {image?.close()mImageReader.close()virtualDisplay?.release()//必须代码,否则出现BufferQueueProducer: [ImageReader] dequeueBuffer: BufferQueue has been abandonedmImageReader.setOnImageAvailableListener(null, null)mediaProjection!!.stop()}}, 100)}}
android 26以上,google 官方文档支持 PixelCopy 实现截图效果
val view = window.decorViewif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//获取layout的位置val location = IntArray(2)view.getLocationInWindow(location)//准备一个bitmap对象,用来将copy出来的区域绘制到此对象中val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888, true)PixelCopy.request(this.window,Rect(location[0], location[1], location[0] + view.width, location[1] + view.height),bitmap, { copyResult ->//如果成功if (copyResult == PixelCopy.SUCCESS) {cutScreenShotToShare(bitmap)}else{Toast.makeText(this, "截图出现错误", Toast.LENGTH_SHORT).show()}}, Handler(Looper.getMainLooper()))}else{takeScreenShotToShare()}
2、剪裁图片 (截去状态栏和标题栏)
private fun cutScreenShot(bitmap: Bitmap) {val screenView = window.decorView//获取状态栏高度val frame = Rect()screenView.getWindowVisibleDisplayFrame(frame)val statusbarHeight = frame.top//获取标题栏高度(toolbar里有menu,需要截掉)val typeValue = TypedValue()theme.resolveAttribute(android.R.attr.actionBarSize, typeValue, true)val toolbarHeight = TypedValue.complexToDimensionPixelSize(typeValue.data, resources.displayMetrics)//获取屏幕长宽val width = screenView.widthval height = screenView.height//去掉状态栏和标题栏val screenShot = bitmap.let { Bitmap.createBitmap(it, 0, toolbarHeight + statusbarHeight, width, height - toolbarHeight - statusbarHeight - getVirtualBarHeight()) }if (screenShot != null) {ShotShareUtil(this).shotShare(this, screenShot)}}
获取虚拟导航栏的高度
//获取虚拟导航栏的高度private fun getVirtualBarHeight():Int{var height = 0val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {display} else {val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManagerwindowManager.defaultDisplay}val dm = DisplayMetrics()try {val c = Class.forName("android.view.Display")val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)method.invoke(display, dm)val displayMetrics = resources.displayMetricsheight = dm.heightPixels - displayMetrics.heightPixels} catch (e:Exception) {e.printStackTrace()}return height}
3、实现分享功能
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import androidx.core.content.FileProvider
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOExceptionclass ShotShareUtil(context: Context){private val BITMAP_DIR = "${context.getExternalFilesDir(null)?.absolutePath}/BitmapCache"private lateinit var bitmapName:Stringprivate lateinit var bitmapPath:Stringfun shotShare(context: Context,bitmap: Bitmap){bitmapName = "${System.currentTimeMillis()}"bitmapPath = "$BITMAP_DIR/$bitmapName.png"SaveBitmap(bitmap)ShareImage(context, bitmapPath)}private fun ShareImage(context: Context, imagePath: String) {val uri: Urival file = File(imagePath)if (context == null || file == null) {throw NullPointerException()}uri = if (Build.VERSION.SDK_INT >= 24) {FileProvider.getUriForFile(context, "com.example.customlockscreen.fileprovider", file)} else {Uri.fromFile(file)}var intent = Intent(Intent.ACTION_SEND)intent.type = "image/*"intent.putExtra(Intent.EXTRA_STREAM, uri) // 分享的内容intent.putExtra(Intent.EXTRA_SUBJECT,"分享")intent = Intent.createChooser(intent, "分享到:")context.startActivity(intent)}private fun SaveBitmap(bitmap: Bitmap) {try { // 获取SDCard指定目录下val sdCardDir = BITMAP_DIRval dirFile = File(sdCardDir) //目录转化成文件夹if (!dirFile.exists()) { //如果不存在,那就建立这个文件夹dirFile.mkdirs()}val file = File(sdCardDir, "$bitmapName.png") // 在该目录下创建图片if (file.exists()) {file.delete()}file.createNewFile() //创建文件val out = FileOutputStream(file)bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)out.flush()out.close()} catch (e: FileNotFoundException) {e.printStackTrace()} catch (e: IOException) {e.printStackTrace()}}}