本人在安卓程序开发时用到的各种小功能(一) 屏幕截图

新建一个方法 
private File captureScreen() {
        webview.setDrawingCacheEnabled(true);
        webview.buildDrawingCache();
        Bitmap bmp = webview.getDrawingCache();
        File file = new File(APKUtil.getDiskCacheDir(this, Constants.TEMP_DIR), messageTitle + ".jpg");
        if (file.exists()) {
            file.delete();
        }
        file.getParentFile().mkdirs();
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            if (bmp.compress(Bitmap.CompressFormat.JPEG, 70, out)) {
                out.flush();
                out.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

其中的webview是一个网页控件,想切图像的哪一个部分,直接定义即可 比如 一个页面是一个线性布局,那就先初始化xml中的线性布局  LinearLayout  linearLayout; 把

webview 替换成linearLayout即可

APKUtil为用户自定义的一个工具类
APKUtil.getDiskCacheDir();

 /**
     * 获得磁盘缓存目录 [PS:应用卸载后会被自动删除]
     *
     * @param context
     * @param uniqueName
     * @return
     */
    public static File getDiskCacheDir(Context context, String uniqueName) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getApplicationContext().getExternalCacheDir().getPath();
        } else {
            cachePath = context.getApplicationContext().getFilesDir().getPath();
        }
        File file = new File(cachePath + File.separator + uniqueName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

Constants用户自定义的网络连接地址
Constants.TEMP_DIR
public static final String TEMP_DIR = "temp"; // 临时文件夹


之后选择进行截图的方式  以下是一个按钮点击时截图

btnright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = captureScreen();
                if (file != null || file.getAbsolutePath() != null) {
                    path = file.getAbsolutePath();
                }
              
            }
        });


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值