zoukankan      html  css  js  c++  java
  • ScreenShot 截图工具类

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Rect;
    import android.view.View;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * view截图实现
     */
    public class ScreenShot {
       private static final String TAG = ScreenShot.class.getSimpleName();
    
       // 获取指定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;
          //      int statusBarHeight = frame.height() - view.getHeight();
    
          //获取屏幕长和高   
          int width = activity.getWindowManager().getDefaultDisplay().getWidth();
          int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    
          Logger.d(TAG, "Rect height:" + frame.height() + " Rect   " + frame.width());
          Logger.d(TAG, "view height:" + view.getHeight() + " view     " + view.getWidth());
          Logger.d(TAG, "screen_shot:" + (width - 40) + " " + statusBarHeight + " " + 40 + " " + (height - statusBarHeight));
    
          //去掉标题栏   
          //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);   
          //        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); //全屏
          Bitmap b = Bitmap.createBitmap(b1, width - 40, statusBarHeight * 2, 40, height - statusBarHeight * 2); //全屏
          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 String shoot(Activity a) {
          ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/maizuo_xx.png");
          return "/maizuo_xx.png";
       }
    
    }
    
  • 相关阅读:
    面向对象的本质是算法的上下文封装,是同一类属的行为接口的一致性
    结构化方法和面向对象方法的比较
    需求文档和软件都是服务的集合
    注意 Laravel 清除缓存 php artisan cache:clear 的一个坑
    面向对象复习笔记(一)
    详解如何在Laravel中增加自定义全局函数
    Laravel 引入自定义类库或第三方类库
    PHP怎么调用其他类的方法
    Laravel如何引用第三方(自定义)库
    laravel框架手机发送验证码
  • 原文地址:https://www.cnblogs.com/loaderman/p/6435157.html
Copyright © 2011-2022 走看看