You need to enable JavaScript to run this app. Android 中将大Bitmap按1500高度拆分后存入Bitmap数组是否可行?
优惠活动
大模型
产品
解决方案
定价
更多

Android 中将大Bitmap按1500高度拆分后存入Bitmap数组是否可行?

这个需求完全可以实现,核心逻辑是通过Bitmap的区域裁剪API按指定高度逐段截取即可,以下是具体实现说明:

实现逻辑
  • 先获取原Bitmap的总高度、宽度参数,确认单块最大高度阈值(需求中指定的1500)
  • 计算拆分总块数:块数 = 向上取整(原总高度 / 单块最大高度),比如原高2300时,计算结果为2块
  • 遍历每一块计算裁剪参数:
    • 非最后一块的高度固定为1500,最后一块高度 = 原总高度 - 已裁剪高度总和
    • 用Bitmap裁剪API截取对应区域的图像,存入结果数组即可
注意事项
  • 裁剪得到的子Bitmap默认和原Bitmap共享像素数据,如果你需要子Bitmap完全独立可修改,可额外调用copy()方法生成独立实例
  • 处理完成后不需要的原Bitmap、临时对象要及时回收,避免内存溢出
  • 大尺寸Bitmap处理建议放在子线程执行,避免阻塞UI线程
Android 平台示例代码
import android.graphics.Bitmap;
import java.util.ArrayList;
import java.util.List;

public List<Bitmap> splitBitmap(Bitmap originBitmap, int maxBlockHeight) {
    List<Bitmap> splitResult = new ArrayList<>();
    if (originBitmap == null || maxBlockHeight <= 0) {
        return splitResult;
    }
    int originWidth = originBitmap.getWidth();
    int originHeight = originBitmap.getHeight();
    // 计算总块数
    int blockCount = (int) Math.ceil((double) originHeight / maxBlockHeight);
    
    int currentCropY = 0;
    for (int i = 0; i < blockCount; i++) {
        int currentBlockHeight = Math.min(maxBlockHeight, originHeight - currentCropY);
        // 裁剪对应区域
        Bitmap block = Bitmap.createBitmap(originBitmap, 0, currentCropY, originWidth, currentBlockHeight);
        splitResult.add(block);
        currentCropY += currentBlockHeight;
    }
    return splitResult;
}

调用示例:

// 传入原Bitmap和最大单块高度1500,得到拆分结果
List<Bitmap> result = splitBitmap(yourOriginBitmap, 1500);

按示例调用后,高度为2300的原Bitmap会得到包含2个元素的结果,第一个高度1500,第二个高度800,符合需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 11:06:07