android屏幕截图

来源:互联网 发布:中国传媒大学网络 编辑:程序博客网 时间:2024/05/16 13:55
android屏幕截图
  
    importjava.io.FileNotFoundException;  
    importjava.io.FileOutputStream;  
    importjava.io.IOException;  
        
    importandroid.app.Activity;  
    importandroid.graphics.Bitmap;  
    importandroid.graphics.Rect;  
    importandroid.view.View;  
        
    publicclass ScreenShot {  
        // 获取指定Activity的截屏,保存到png文件  
        privatestatic 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);    
            intstatusBarHeight = frame.top;    
            System.out.println(statusBarHeight);  
                
            //获取屏幕长和高  
            intwidth = activity.getWindowManager().getDefaultDisplay().getWidth();    
            intheight = 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();  
            returnb;  
        }  
            
        //保存到sdcard  
        privatestatic 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();  
            }  
        }  
            
        //程序入口  
        publicstatic void shoot(Activity a){  
            ScreenShot.savePic(ScreenShot.takeScreenShot(a),"sdcard/xx.png");  
        }  
    }
原创粉丝点击