我尝试为ScrollView
中的所有视图拍摄ScreenShot,我的ScrollView
内容溢出屏幕(因此滚动成为可能),我如何确保屏幕截图包含元素在屏幕上看不到?
我一直在使用这段代码:
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;
}
创建了Image文件,但它只显示黑屏,而不是我想要的视图,所以我的代码有问题吗?请问有人可以帮助我吗?
更新
关注@ 113408的答案后,我收到错误,这是我的logcat
:
08-27 10:40:57.555: E/AndroidRuntime(4957): FATAL EXCEPTION: main
08-27 10:40:57.555: E/AndroidRuntime(4957): java.lang.NullPointerException
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.graphics.Bitmap.createBitmap(Bitmap.java:484)
08-27 10:40:57.555: E/AndroidRuntime(4957): at id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.view.View.performClick(View.java:4222)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.view.View$PerformClick.run(View.java:17337)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.os.Handler.handleCallback(Handler.java:615)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.os.Looper.loop(Looper.java:137)
08-27 10:40:57.555: E/AndroidRuntime(4957): at android.app.ActivityThread.main(ActivityThread.java:4895)
08-27 10:40:57.555: E/AndroidRuntime(4957): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 10:40:57.555: E/AndroidRuntime(4957): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 10:40:57.555: E/AndroidRuntime(4957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-27 10:40:57.555: E/AndroidRuntime(4957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-27 10:40:57.555: E/AndroidRuntime(4957): at dalvik.system.NativeStart.main(Native Method)
注意:08-27 10:40:57.555: E/AndroidRuntime(4957): at id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
是指Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
答案 0 :(得分:0)
试试这个:
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的任何子布局的屏幕截图。
答案 1 :(得分:0)
您可以使用此代码截取您的完整ScrollView
的屏幕截图,相同的代码可以适用于所有类型的View
代码:
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();
}