• Android截屏分享之View生成图片进行保存分享、全屏,半屏、指定VIew、弹窗.......


    Android截屏主要应用在分享这个操作,所有的截屏,截取的是视图。接下来给大家分享一下这个功能的干货

    温馨提示:我这里分享使用的是极光的Jshare,也可以使用原生分享
    这里是JshareSDK接入的链接:链接: https://docs.jiguang.cn/jshare/client/Android/android_sdk

    常规需求,我这里的示例是拿弹窗来演示,标题也说了截取的是view,后面会着重讲解
    弹窗中有二维码,顺便说下二维码生成
    1. build.gradle (app)中添加zxing依赖:
        //扫描二维码
        implementation 'com.github.yuzhiqiang1993:zxing:2.2.9'
    
    • 1
    • 2
    1. 生成二维码操作
        //生成二维码
    	Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);//二维码中心logo
    	Bitmap qrCode = CodeCreator.createQRCode(url, 400, 400, logo);
    	ImageView codeImg = view.findViewById(R.id.scan_code);
    		if (qrCode != null) {
    		    codeImg.setImageBitmap(qrCode);
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    回归正题 截取view
    1、获取需要截取的View 这里是弹窗中需要截取的view

    /**
    *dialog中的view获取和回调
    */
     LinearLayout screenshotView = view.findViewById(R.id.screenshot_view);//截取的视图
     //弹窗按钮点击截取 回传view 
     button.setOnClickListener(new View.OnClickListener() {
                @SingleClick
                @Override
                public void onClick(View v) {
                    mListener.onSaveAndShare(screenshotView);//需要回传的view
                }
            });
    
    /**
    *页面中的dialog回调
    */
      private View screenshotView;
      scanningDialog.setOnShareListener(new ScanningDialog.OnShareListener() {
                @Override
                public void oncLoseDialog() {
                    scanningDialog.dismiss();
                }
    
                @Override
                public void onSaveAndShare(View view) {
                    //保存图片并分享
                    screenshotView = view;
                    screenShotView();
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2、dialog弹窗截屏

      private final int REQUEST_STORAGE = 10223;//存储权限
    	 /**
         * dialog弹窗截屏
         * 最终调用方法
         */
        public void screenShotView() {
        	//动态获取权限
            if (checkPermission()) {
                saveShotView();//截取图片
            } else {
                requestPermission();//权限请求
            }
        }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == REQUEST_STORAGE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    saveShotView();//截取图片
                } else {
                    requestPermission();//权限请求
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3、获取权限 checkPermission() requestPermission()

    /**
        * check权限
        * request权限
        */
     private boolean checkPermission() {
            int selfPermission = ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
            return selfPermission == PackageManager.PERMISSION_GRANTED;
        }
    private void requestPermission() {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、截取并分享 saveShotView()

    /**
    * 截取图片并保存 可截取任意视图
    * screenshotView需要截取的view 第一步中的视图对象
    */
     private void saveShotView() {
            String CAMERA_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera";
            Bitmap tempBitmap = Bitmap.createBitmap(screenshotView.getWidth(), screenshotView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(tempBitmap);
            screenshotView.draw(canvas);
            File fileDir = new File(CAMERA_DIR);
            if (!fileDir.exists()) {
                boolean mkdirs = fileDir.mkdirs();
            }
            String path = CAMERA_DIR + "/share_screen.png";
            try {
                FileOutputStream foStream = new FileOutputStream(path);
                tempBitmap.compress(Bitmap.CompressFormat.PNG, 100, foStream);
                foStream.flush();
                foStream.close();
                if (scanningDialog != null) {
                    scanningDialog.dismiss();
                }
                ToastUtils.toast("保存成功,请在相册中查看");
                shareNowImage(path);//todo调用分享
            } catch (Exception e) {
                Log.e("保存图片出错", e.toString());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    5、极光分享 shareNowImage(String path)

    	/**
    	* path  本地图片路径
    	* shareListener分享回调
    	*/
        private void shareNowImage(String path) {
            List<String> platforms = JShareInterface.getPlatformList();
            PageEventUtils.inFriendsNow(requireContext());
            ShareParams shareParams = new ShareParams();
            shareParams.setShareType(Platform.SHARE_IMAGE);
            shareParams.setImagePath(path);
            JShareInterface.share(platforms.get(0), shareParams, shareListener);
        }
        //分享回调
         private final PlatActionListener shareListener = new PlatActionListener() {
            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
                ToastUtils.toast("分享成功");
            }
    
            @Override
            public void onError(Platform platform, int action, int errorCode, Throwable error) {
                Log.e("分享失败", "error:" + errorCode + ",msg:" + error);
            }
    
            @Override
            public void onCancel(Platform platform, int action) {
                ToastUtils.toast("取消分享");
            }
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    推荐链接: 自定义完整二维码分享Dialog代码分享

  • 相关阅读:
    javascript获取地址栏的绝对路径
    四种质数筛选算法总结(c++)
    低代码中的可视化表单:效率与灵活兼备的设计工具
    手撕代码(Simple)- Java后端高频面试算法题集锦 1
    七 R语言|箱须图、饼图的绘制
    MSE实现全链路灰度实践
    Windows批处理文件 @echo off作用
    java计算机毕业设计医院远程诊断系统源程序+mysql+系统+lw文档+远程调试
    jvm参数调优
    【2022 Q2&Q3 华为机试真题 C++】字符串子序列II
  • 原文地址:https://blog.csdn.net/weixin_43148719/article/details/126520890