new Thread(new Runnable() {
@Override
public void run() {
while(true) {
View view = findViewById(R.id.rootView);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
if (bmp == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
String fn = "image_test.png";
String path = OmegaApp.getInstance().getFilesDir().getAbsolutePath() + File.separator + fn;
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
//压缩保存到本地
bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}).start();
将layout布局转成图片下载到本地
最新推荐文章于 2024-06-06 09:43:05 发布
本文介绍了一种在Android应用中持续获取指定视图组件的截图并将其保存为PNG格式图片的方法。通过创建一个新的线程,应用程序可以不断地尝试从定义的 rootView 中捕获图像,即使在第一次尝试失败的情况下也会继续重试直到成功。最终将截图保存到应用的私有文件目录下。
4800

被折叠的 条评论
为什么被折叠?



