android截图

来源:互联网 发布:unity3d ios 模拟器 编辑:程序博客网 时间:2024/05/16 16:21

跳转到指定显示屏幕执行截图保存

 public static void shoot(Activity a, File filePath)
        {  
            if (filePath == null)
            {  
                return;  
            }  
            if (!filePath.getParentFile().exists())
            {  
                filePath.getParentFile().mkdirs();  
            }  
            savePic(takeScreenShot(a), filePath);  
        }
      
    public static Bitmap takeScreenShot(Activity activity)
    {  
        // View是你需要截图的View  ,属于activity
        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 width = activity.getWindowManager().getDefaultDisplay().getWidth();  
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();  
      
        //Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
       
        int y=b1.getHeight();//就是当前activity的高度
        LogUtil.e(TAG, "图片高度:"+y);
        if(y<height)
        {
            height=y;//如果activity高度小于屏幕高度,就让截图高度等于activity高度,否则下面会报错
        }
        LogUtil.e(TAG, "height高度:"+height);
        Bitmap b = Bitmap.createBitmap(b1, 0, 0, width, height);
        view.destroyDrawingCache();  
        return b;  
    }  
 
    public static void savePic(Bitmap b, File filePath)
    {  
        FileOutputStream fos = null;  
        try
        {  
            fos = new FileOutputStream(filePath);  
            if (null != fos)
            {  
                b.compress(Bitmap.CompressFormat.PNG, 100, fos);  
                fos.flush();  
                fos.close();  
            }  
        } catch (FileNotFoundException e)
        {  
            e.printStackTrace();  
        } catch (IOException e) {  
             e.printStackTrace();  
        }  
    } 

0 0
原创粉丝点击