溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖

發(fā)布時(shí)間:2021-08-11 10:47:04 來(lái)源:億速云 閱讀:177 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

很多的時(shí)候,我們想要分享一個(gè)界面的所有內(nèi)容,可是內(nèi)容太多,超過(guò)了屏幕的大小,簡(jiǎn)單的截屏已經(jīng)滿足不了我們的需要,這時(shí)候我們就可以根據(jù)布局里scrollView的高度來(lái)截取圖片。

代碼如下:

/** 
   * 截取scrollview的屏幕 
   * @param scrollView 
   * @return 
   */ 
  public static Bitmap getBitmapByView(ScrollView scrollView) { 
    int h = 0; 
    Bitmap bitmap = null; 
    // 獲取scrollview實(shí)際高度 
    for (int i = 0; i < scrollView.getChildCount(); i++) { 
      h += scrollView.getChildAt(i).getHeight(); 
      scrollView.getChildAt(i).setBackgroundColor( 
          Color.parseColor("#ffffff")); 
    } 
    // 創(chuàng)建對(duì)應(yīng)大小的bitmap 
    bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, 
        Bitmap.Config.RGB_565); 
    final Canvas canvas = new Canvas(bitmap); 
    scrollView.draw(canvas); 
    return bitmap; 
  } 
 
  /** 
   * 壓縮圖片 
   * @param image 
   * @return 
   */ 
  public static Bitmap compressImage(Bitmap image) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    int options = 100; 
    // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 
    while (baos.toByteArray().length / 1024 > 100) { 
      // 重置baos 
      baos.reset(); 
      // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 
      image.compress(Bitmap.CompressFormat.JPEG, options, baos); 
      // 每次都減少10 
      options -= 10; 
    } 
    // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); 
    // 把ByteArrayInputStream數(shù)據(jù)生成圖片 
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); 
    return bitmap; 
  } 
 
/** 
   * 保存到sdcard 
   * @param b 
   * @return 
   */ 
  public static String savePic(Bitmap b) { 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", 
        Locale.US); 
    File outfile = new File("/sdcard/image"); 
    // 如果文件不存在,則創(chuàng)建一個(gè)新文件 
    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; 
  }

在需要用到的地方調(diào)用getBitmapByView()方法即可:

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

但是這樣寫(xiě)的話有時(shí)候會(huì)因?yàn)榻厝〉膱D片太長(zhǎng)太大而報(bào)outofmemory的錯(cuò),所以為了避免內(nèi)存溢出,程序崩掉,要注意用Config.RGB_565,會(huì)比ARGB_8888少占內(nèi)存。還有就是把圖片壓縮一下,至少我這樣就沒(méi)有報(bào)oom的錯(cuò)了,即:

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

感謝各位的閱讀!關(guān)于“Android如何實(shí)現(xiàn)給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI