通过code的Android抓图 [英] Android take screenshot via code
本文介绍了通过code的Android抓图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这应该不会太艰难的问题。我想利用我的布局(视图)的屏幕截图,并通过手机短信发送的能力。有人可以走我虽然步骤?
This shouldn't be too tough of a question. I want the ability to take a screenshot of my layout (view) and send it via sms. Can someone walk me though the steps?
谢谢!
编辑: 它没有成为一个屏幕截图我想,只要你将我们从一个视图以某种方式让所有的渲染像素。
It doesn't have to be a 'screenshot' I guess, just as long as we can get all of the rendered pixels from a view somehow.
推荐答案
围绕网络我发现code一些片段,我能得到协同工作。
Around the web I found some snippets of code that I was able to get working together.
下面是一个解决方案,效果很好:
Here is a solution that works well:
设置根布局:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
函数来获取渲染视图:
Function to get the rendered view:
private void getScreen()
{
View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
这篇关于通过code的Android抓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文