资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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实现截屏功能的实例-创新互联
文章位置:http://cdkjz.cn/article/dodhee.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220