美文网首页
Android--布局图像

Android--布局图像

作者: dus_code | 来源:发表于2017-04-18 01:52 被阅读0次

    将当前界面的可视组件以同样的相同位置和大小保存在png图像文件中:
    <pre>
    /**
    * @param view ----当前界面的布局view对象
    * @return
    */
    public Bitmap getBitmap(View view, String path){

        // 打开图像缓存
        view.setDrawingCacheEnabled(true);
        // 必须要先调用measure和layout方法才能成功保存截图到png图像文件
        // 测量view的大小
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        // 发送位置和尺寸到view及其所有的子view
        view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
        // 获取可视化截图
        Bitmap bitmap = view.getDrawingCache();
        try{
            // 将截图保存到SD卡根目录的test.png中 path
            FileOutputStream outputStream = new FileOutputStream(path);
            // 将图像压缩成png格式并保存
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.close();
        }catch (Exception e){
    
        }
        return bitmap;
    }
    

    </pre>

    相关文章

      网友评论

          本文标题:Android--布局图像

          本文链接:https://www.haomeiwen.com/subject/ndnxzttx.html