这个问题不应该太难。我想要能够截取我的布局(视图)并通过短信发送它的能力。有人可以指导我如何操作吗?
谢谢!
编辑: 我想,它不一定要是“截图”,只要我们能以某种方式获取视图中的所有渲染像素即可。
这个问题不应该太难。我想要能够截取我的布局(视图)并通过短信发送它的能力。有人可以指导我如何操作吗?
谢谢!
编辑: 我想,它不一定要是“截图”,只要我们能以某种方式获取视图中的所有渲染像素即可。
我在网络上找到了一些代码片段,最终让它们协同工作。
这里是一个很好的解决方案:
设置您的根布局:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
获取渲染视图的函数:
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();
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
,并且不要硬编码 /sdcard/
,而是使用 File file = new File( Environment.getExternalStorageDirectory() + "/test.png");
。 - Macarse