Android实现截屏功能的实例-创新互联

Android 实现截屏功能的实例

成都创新互联公司专注于企业营销型网站、网站重做改版、银海网站定制设计、自适应品牌网站建设、H5技术商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为银海等各大城市提供网站开发制作服务。

实现代码:

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");
  }
}

当前标题:Android实现截屏功能的实例-创新互联
链接URL:https://www.cdcxhl.com/article44/dodhee.html

成都网站建设公司_创新互联,为您提供网站建设小程序开发定制开发网页设计公司网站收录建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管