曲线解决Android中对布局的截屏

来源:互联网 发布:淘宝主图ps无缝拼接 编辑:程序博客网 时间:2024/05/19 00:11

网上看了一些关于截屏的代码案例,但是像调用JNI这类,以及使用源码等方法,感觉不适合我一个小初去用,以及快速的加入到你工作中的代码中,这里我偷懒先调用SDK的窗口截屏的方法(不多就三四行),然后把得到的图片,根据你要截屏的布局在窗口的位置信息,进行对图片的剪切,,算是曲线救国了啊,,呵呵哒()

private Bitmap getBitmap() {
        View view = getActivity().getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
bitmap = view.getDrawingCache();
Rect frame = new Rect();
getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// int toHeight = frame.top;
int[] location = new int[2];
layout_request.getLocationOnScreen(location);
bitmap = Bitmap.createBitmap(bitmap, 0, location[1], view.getWidth(), layout_request.getHeight());
view.setDrawingCacheEnabled(false);
try {//把得到的图片保存到本地,FileUtils是我自己的写文件的工具类,你们随意
FileOutputStream fout = new FileOutputStream(FileUtils.getHomeDir() + time + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}

0 0
原创粉丝点击