Android代码截屏(仿系统截屏效果)

产品要求的新需求,在一个分享的页面对当前的页面截屏保存图片然后在第三方应用中进行图片分享,这里就做了一个简单的demo,为了更好的用户体验就做了一个类似系统按钮截屏的效果,截屏之后将截图做了一个本地存储操作,好了交代的也差不多了先来看效果图

Android代码截屏(仿系统截屏效果)_第1张图片Android代码截屏(仿系统截屏效果)_第2张图片

就直接上代码

/**
     * 弹出截屏框
     */
    private void popShotSrceenDialog() {
        final AlertDialog cutDialog = new AlertDialog.Builder(this).create();
        View dialogView = View.inflate(this, R.layout.show_cut_screen_layout, null);
        ImageView showImg = (ImageView) dialogView.findViewById(R.id.show_cut_screen_img);
        //获取当前屏幕的大小
        int width = getWindow().getDecorView().getRootView().getWidth();
        int height = getWindow().getDecorView().getRootView().getHeight();
        //生成相同大小的图片
        Bitmap temBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //找到当前页面的根布局,可以直接使用某个view的,截取的就是当前View的内容
        View view = getWindow().getDecorView().getRootView();//当前窗口布局
        //设置缓存
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        //从缓存中获取当前屏幕的图片
        temBitmap = view.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
        showImg.setImageBitmap(temBitmap);
        saveToLocal(temBitmap);
        //销毁当前屏幕图片的缓存
        view.destroyDrawingCache();
        view.setDrawingCacheEnabled(false);

        cutDialog.setView(dialogView);
        Window window = cutDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.8); // 高度设置为屏幕的0.6
        p.gravity = Gravity.CENTER;//设置弹出框位置
        window.setAttributes(p);
        cutDialog.show();
        //使用定时器,弹窗1秒后关闭,达到系统截屏效果
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                cutDialog.dismiss();
                timer.cancel();
            }
        }, 1000);
    }

    /**
     * 保存截屏至本地
     *
     * @param bitmap
     */
    public void saveToLocal(Bitmap bitmap) {
        //生成路径
        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
        String dirName = "coinrise";
        File appDir = new File(root, dirName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        //文件名为时间
        long timeStamp = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String sd = sdf.format(new Date(timeStamp));
        String fileName = sd + ".jpg";

        //获取文件
        File file = new File(appDir, fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            //通知系统相册刷新
            InviteQRCodeActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                    Uri.fromFile(new File(file.getPath()))));
            Toast.makeText(InviteQRCodeActivity.this, "图片已保存至相册", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

弹出框的布局文件show_cut_screen_layout.xml




    

上面代码可以直接copy到项目中直接使用就要,不过需要注意的是Android6.0之后要动态申请存储权限,如果没有获取到权限截图保存至本地会失败

路漫漫其修远兮,吾将上下而求索

 

 

你可能感兴趣的:(Android工具类)