Android:在ScrollView中为所有视图截图
我尝试在ScrollView中为所有视图截图,我的ScrollView有溢出屏幕的内容,因此可以进行滚动,如何确保截图包含屏幕上不可见的元素 我一直在使用以下代码:Android:在ScrollView中为所有视图截图,android,uiscrollview,screenshot,Android,Uiscrollview,Screenshot,我尝试在ScrollView中为所有视图截图,我的ScrollView有溢出屏幕的内容,因此可以进行滚动,如何确保截图包含屏幕上不可见的元素 我一直在使用以下代码: scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp); int totalHeight = scroll_pp.getChildAt(0).getHeight(); int totalWidth = scroll_pp.getChildAt(0).getWidth();
scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp);
int totalHeight = scroll_pp.getChildAt(0).getHeight();
int totalWidth = scroll_pp.getChildAt(0).getWidth();
Bitmap b= MethodSupport.loadBitmapFromView(scroll_pp, totalWidth, totalHeight);
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = "Test.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
创建了图像文件,但它只显示黑屏,而不是我想要的视图,所以我的代码有问题吗?请问有人能帮我吗
更新
在遵循@113408的答案后,我得到了错误,这是我的日志:
注释:08-27 10:40:57.555:E/AndroidRuntime4957:at id.co.ajsmssig.espaj.PemegangPolis.onClickPemegangPolis.java:1546
是指位图b=Bitmap.createBitmapu.getDrawingCache 试试这个:
Bitmap bitmap = Bitmap.createBitmap(scrollView.getChildAt(0).getWidth(), scrollView.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
scrollView.getChildAt(0).draw(c);
您可以获取scrollview的任何子布局的屏幕截图。您可以使用此代码获取完整scrollview的屏幕截图。相同的代码可以适用于所有类型的视图 代码:
View u = findViewById(R.id.scroll_pp);
u.setDrawingCacheEnabled(true);
ScrollView scroll_pp = (ScrollView) findViewById(R.id.scroll_pp);
int totalHeight = scroll_pp.getChildAt(0).getHeight();
int totalWidth = scroll_pp.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = "Test.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它在这一行中为我提供NullPointerException位图b=Bitmap.createBitmapu.getDrawingCache;我在ressource绑定上犯了一个小错误,但我认为您在使用我的代码时替换了它。你能发布日志吗?我只是测试了一下,似乎问题出在孩子的高度和宽度上对不起,我检查了很多次,但我都弄不清楚。如果你找到了一个可行的解决方案,就发布它。也许将来会有人需要它
View u = findViewById(R.id.scroll_pp);
u.setDrawingCacheEnabled(true);
ScrollView scroll_pp = (ScrollView) findViewById(R.id.scroll_pp);
int totalHeight = scroll_pp.getChildAt(0).getHeight();
int totalWidth = scroll_pp.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = "Test.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}