欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android給scrollView截圖超過屏幕大小形成長圖

 更新時間:2017年12月14日 14:28:46   作者:lyy1104  
這篇文章主要為大家詳細介紹了Android給scrollView截圖超過屏幕大小形成長圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

代碼如下:

/** 
   * 截取scrollview的屏幕 
   * @param scrollView 
   * @return 
   */ 
  public static Bitmap getBitmapByView(ScrollView scrollView) { 
    int h = 0; 
    Bitmap bitmap = null; 
    // 獲取scrollview實際高度 
    for (int i = 0; i < scrollView.getChildCount(); i++) { 
      h += scrollView.getChildAt(i).getHeight(); 
      scrollView.getChildAt(i).setBackgroundColor( 
          Color.parseColor("#ffffff")); 
    } 
    // 創(chuàng)建對應(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)建一個新文件 
    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)); 

但是這樣寫的話有時候會因為截取的圖片太長太大而報outofmemory的錯,所以為了避免內(nèi)存溢出,程序崩掉,要注意用Config.RGB_565,會比ARGB_8888少占內(nèi)存。還有就是把圖片壓縮一下,至少我這樣就沒有報oom的錯了,即:

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論