将可视化组件保存在png图像文件中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yy471101598/article/details/54584699
 View view = getLayoutInflater().inflate(R.layout.activity_main, null);
        //打开图像缓冲
        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());
        try {
            //获取可视组件截图
            Bitmap bitmap = view.getDrawingCache();
            FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {

        }

猜你喜欢

转载自blog.csdn.net/yy471101598/article/details/54584699