• android 截屏工具类


    /**
     * 截屏工具类
     * @author ooxx
     *
     */
    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;
            System.out.println(statusBarHeight);
    
            // 获取屏幕长和高
            int width = activity.getWindowManager().getDefaultDisplay().getWidth();
            int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    
            // 去掉标题栏
            // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
            Bitmap bitmap = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
            view.destroyDrawingCache();
            return bitmap;
        }
    
        // 保存
        private static boolean savePic(Bitmap bitmap, String savePath) {
            FileOutputStream fos = null;
            boolean success = false;
            try {
                fos = new FileOutputStream(savePath);
                
                if (null != fos) {
                    success = bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
                    fos.flush();
                    fos.close();
                }
                return success;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return success;
        }
    
        /**
         * 截屏
         * 注意添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
         * 
         * @param activity 保存的activity
         * @param savePath 文件的完整路径
         * @return 成功true 失败false
         */
        public static boolean shoot(Activity activity, String savePath) {
            return ScreenShot.savePic(ScreenShot.takeScreenShot(activity), savePath);
        }
    }
    原文地址 http://www.apkway.com/forum.php?mod=viewthread&tid=3225&extra=page%3D1

  • 相关阅读:
    Eclipse正确导入第三方project
    面试的基础_01字符串反向操作
    一个简单的实现了智能虚拟女友—图灵机器人
    Notepad++去除代码行号的几种方法
    fastjson将bean转成字符串时首字母变小写问题
    2015第34周二能收发邮件但不能打开网页解决方法
    2015第34周一
    2015第33周日
    2015第33周六
    构建自己的顾问团
  • 原文地址:https://www.cnblogs.com/zhudongfang/p/3381002.html
Copyright © 2020-2023  润新知