You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Kotlin代码实现截图上下裁剪,获取指定蓝色区域截图?

解决Kotlin截图指定区域及裁剪顶部/底部问题

Hey there! Let's fix your screenshot cropping troubles. The key here is that after capturing the full screen, we can use Android's Bitmap utilities to carve out exactly the area you need—whether that's the blue region or a version with top/bottom cropped off.

1. 搞定蓝色区域截取:精准定位坐标

The main issue with targeting the blue area is probably incorrect coordinate calculations. Here's how to get it right:

First, capture the full screen bitmap (I'll include a quick snippet for that if you don't have it already). Then, define the bounding rectangle of your blue area using its left, top, right, and bottom coordinates relative to the full screen.

蓝色区域截取示例代码

import android.graphics.Bitmap
import android.graphics.Rect
import android.view.View

fun captureBlueArea(view: View): Bitmap? {
    // 步骤1:捕获全屏Bitmap
    view.isDrawingCacheEnabled = true
    val fullScreenBitmap = Bitmap.createBitmap(view.drawingCache)
    view.isDrawingCacheEnabled = false

    // 步骤2:定义蓝色区域坐标(请根据你的UI实际数值调整!)
    // 示例:蓝色区域从左100px、上200px开始,到右800px、下600px结束
    val blueAreaRect = Rect(100, 200, 800, 600)

    // 步骤3:裁剪全屏Bitmap到蓝色区域
    return try {
        Bitmap.createBitmap(
            fullScreenBitmap,
            blueAreaRect.left,
            blueAreaRect.top,
            blueAreaRect.width(),
            blueAreaRect.height()
        )
    } catch (e: IllegalArgumentException) {
        // 处理坐标超出Bitmap范围的情况
        e.printStackTrace()
        null
    }
}

小技巧:如果不知道蓝色区域的准确坐标,可以用Android Studio的Layout Inspector查看,或者给蓝色区域的View加临时OnTouchListener,点击时打印触摸坐标来获取。

2. 裁剪顶部和底部:通用实现方法

如果需要从截图顶部和底部剪掉固定高度的区域,只需要调整裁剪矩形的范围即可。下面是一个可复用的函数:

顶/底部裁剪代码

fun cropTopAndBottom(fullBitmap: Bitmap, cropTopHeight: Int, cropBottomHeight: Int): Bitmap? {
    // 先检查裁剪高度是否合法(不能超过Bitmap总高度)
    if (cropTopHeight + cropBottomHeight >= fullBitmap.height) {
        return null // 无效的裁剪请求
    }

    // 计算裁剪后的可视区域
    val newTop = cropTopHeight
    val newBottom = fullBitmap.height - cropBottomHeight
    val cropWidth = fullBitmap.width
    val cropHeight = newBottom - newTop

    // 执行裁剪
    return try {
        Bitmap.createBitmap(
            fullBitmap,
            0, // 左右不裁剪,所以left设为0
            newTop,
            cropWidth,
            cropHeight
        )
    } catch (e: IllegalArgumentException) {
        e.printStackTrace()
        null
    }
}

使用示例

// 捕获全屏Bitmap后,调用裁剪函数:顶部裁掉150px,底部裁掉200px
val croppedBitmap = cropTopAndBottom(fullScreenBitmap, 150, 200)

3. 组合需求:先截全屏→再裁蓝色区域→再裁顶/底

如果需要同时实现蓝色区域截取+顶/底部裁剪,可以把函数链式调用:

fun captureCroppedBlueArea(view: View, cropTop: Int, cropBottom: Int): Bitmap? {
    val fullBitmap = captureFullScreen(view) ?: return null
    val blueAreaBitmap = captureBlueAreaFromFullBitmap(fullBitmap) ?: return null
    return cropTopAndBottom(blueAreaBitmap, cropTop, cropBottom)
}

// 辅助函数:捕获全屏
private fun captureFullScreen(view: View): Bitmap? {
    view.isDrawingCacheEnabled = true
    val bitmap = Bitmap.createBitmap(view.drawingCache)
    view.isDrawingCacheEnabled = false
    return bitmap
}

// 辅助函数:从全屏Bitmap裁剪蓝色区域
private fun captureBlueAreaFromFullBitmap(fullBitmap: Bitmap): Bitmap? {
    val blueRect = Rect(100, 200, 800, 600) // 替换为你的蓝色区域实际坐标
    return try {
        Bitmap.createBitmap(fullBitmap, blueRect.left, blueRect.top, blueRect.width(), blueRect.height())
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}

快速排障小贴士

  • IllegalArgumentException:出现这个异常说明你的裁剪矩形超出了原Bitmap的范围,请检查坐标是否满足left + width <= bitmap.width和top + height <= bitmap.height。
  • 截图空白/黑屏:如果捕获到空白Bitmap,要确保你是在View完全布局完成后调用的截图(比如在onViewCreated或onLayout回调之后)。
  • 适配屏幕密度:如果你的UI用dp单位定义尺寸,记得用resources.displayMetrics.density把dp转换成像素,避免不同分辨率设备上裁剪尺寸不一致。

内容的提问来源于stack exchange,提问作者Dave Chambers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:42:32