zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    实现斐波那契数列的三种方式
    [LintCode]计算两个数的交集(二)
    [LintCode]计算两个数的交集(一)
    JNI技术概念小结
    require.js的用法
    JavaScript中模块“写法”
    模块化的JavaScript
    javascript如何判断访问网页的设备及是否支持触屏功能
    Javascript自由拖拽类
    JQuery中html()方法的注意事项
  • 原文地址:https://www.cnblogs.com/zhudongfang/p/3381002.html
Copyright © 2011-2022 走看看