国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現截屏功能的實例

2019-10-22 18:29:41
字體:
來源:轉載
供稿:網友

Android 實現截屏功能的實例

實現代碼:

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();    // 獲取狀態欄高度    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();    // 去掉標題欄    // 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已經被加載后方可調用。

或者在 以下方法這里調用。

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

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 唐海县| 天镇县| 惠来县| 沂南县| 株洲县| 安徽省| 开阳县| 永昌县| 汽车| 专栏| 鄂温| 青神县| 共和县| 永登县| 定日县| 明星| 洞头县| 新竹县| 宁乡县| 河间市| 砚山县| 信丰县| 百色市| 昔阳县| 岢岚县| 环江| 莫力| 屯留县| 芮城县| 图们市| 昌平区| 西城区| 楚雄市| 广元市| 方城县| 甘泉县| 英吉沙县| 景东| 阿城市| 客服| 德钦县|