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

Android 實現(xiàn)截屏功能的實例

 更新時間:2017年08月21日 14:11:14   作者:Jlins  
這篇文章主要介紹了Android 實現(xiàn)截屏功能的實例的相關(guān)資料,這里實現(xiàn)截屏的實例在代碼中注釋非常清楚,希望能幫助到大家,需要的朋友可以參考下

Android 實現(xiàn)截屏功能的實例

實現(xiàn)代碼:

public class ScreenShot {
  // 獲取指定Activity的截屏,保存到png文件
  private static Bitmap takeScreenShot(Activity activity) {
    // View是你需要截圖的View
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();

    // 獲取狀態(tài)欄高度
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    Log.i("TAG", "" + statusBarHeight);

    // 獲取屏幕長和高
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay()
        .getHeight();
    // 去掉標(biāo)題欄
    // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
        - statusBarHeight);
    view.destroyDrawingCache();
    return b;
  }

  // 保存到sdcard
  private static void savePic(Bitmap b, String strFileName) {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(strFileName);
      if (null != fos) {
        b.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.flush();
        fos.close();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  // 程序入口
  public static void shoot(Activity a) {
    ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
  }
}

需要注意的是,shoot方法只能在view已經(jīng)被加載后方可調(diào)用。

或者在 以下方法這里調(diào)用。

 @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    ScreenShot.shoot(this);
  }

以上就是Android截屏的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論