Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ScrollView 長截圖:把超出屏幕的內容拼成一張長圖

編輯:關於 Android 程式設計

很多時候我們想把整個界面的內容分享出去,但內容太長、超出了屏幕範圍,普通截屏就不管用了。這時候可以根據 ScrollView 裡子 View 的實際高度來「畫」出一張長圖。

實現思路

不走「截屏 + 拼接」的路子,而是直接讓 ScrollView draw() 到一張足夠大的 Bitmap 畫布上。這樣一次性得到完整內容,不必處理滾動與拼接的接縫問題。

工具方法

/**
 * 截取 scrollView 的完整內容
 */
public static Bitmap getBitmapByView(ScrollView scrollView) {
    int h = 0;
    Bitmap bitmap = null;

    // 累加所有子 View 的實際高度
    for (int i = 0; i < scrollView.getChildCount(); i++) {
        h += scrollView.getChildAt(i).getHeight();
        // 鋪白底,避免透明區域在深色模式下發黑
        scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
    }

    // 創建對應尺寸的 bitmap
    bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);
    final Canvas canvas = new Canvas(bitmap);
    scrollView.draw(canvas);
    return bitmap;
}

/**
 * 質量壓縮,控制體積
 */
public static Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    int options = 100;
    // 循環判斷:超過 100KB 就繼續壓
    while (baos.toByteArray().length / 1024 > 100) {
        baos.reset();
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        options -= 10;      // 每次遞減 10
    }

    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    return BitmapFactory.decodeStream(isBm, null, null);
}

/**
 * 保存到 /sdcard/image
 */
public static String savePic(Bitmap b) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
    File outfile = new File("/sdcard/image");
    if (!outfile.isDirectory()) {
        try {
            outfile.mkdir();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    String fname = outfile + "/" + sdf.format(new Date()) + ".png";
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fname);
        if (null != fos) {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fname;
}

調用方式

String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView));

關於 OOM 的關鍵細節

這樣寫有時會因為截出的圖太長太大而崩潰。兩個必須注意的地方:

  • 用 RGB_565 而不是 ARGB_8888:前者每像素佔 2 字節,後者佔 4 字節,內存直接省一半。長圖場景幾乎沒有透明度需求,沒有理由用 ARGB_8888。
  • 先壓縮再保存:加上壓縮這一步之後,示例中就再沒出現過 OOM:
String fname = ScreenShot.savePic(
        ScreenShot.compressImage(ScreenShot.getBitmapByView(scrollView)));

實踐建議

  • 長圖高度受單個 Bitmap 大小限制。內容特別長時,更穩健的方案是分段截圖後用 Canvas 逐段拼接,或用 ViewTreeObserver 配合逐步滾動錄製。
  • 保存到 /sdcard 需要在 Manifest 聲明 WRITE_EXTERNAL_STORAGE,Android 7.0 之後建議改用 FileProvider 或 App 私有目錄,避免 FileUriExposedException。
  • 截圖前確保列表已經加載完成(圖片都回來了),否則會得到一片空白占位。
  • 記得在保存後回收 Bitmap,長圖的內存壓力遠大於普通截屏。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved