[英]Android: Taking a ScreenShot for all view in ScrollView
I try to take a ScreenShot for all view in ScrollView
, my ScrollView
has content that spills out of the screen (hence scrolling made possible), how can I ensure that the screenshot includes the elements that are not visible on the screen? 我尝试为ScrollView
所有视图获取ScreenShot,我的ScrollView
内容溢出到屏幕外(因此可以进行滚动),如何确保屏幕截图包含屏幕上不可见的元素?
I have been using this code: 我一直在使用此代码:
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;
}
The Image file created, but it only display the black screen, not the view as I wanted, so Is there something wrong with my code? 创建了Image文件,但是它仅显示黑屏,而不显示所需的视图,因此我的代码是否有问题? Is there anyone can help me, please? 请问有人可以帮助我吗?
UPDATE UPDATE
After follow @113408 's Answer, I got Error, this is my logcat
: 按照@ 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)
for note : 08-27 10:40:57.555: E/AndroidRuntime(4957): at id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
is refer to Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
请注意: 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 id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
Try this: 尝试这个:
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);
You can get the screenshot any of the child layout of scrollview. 您可以在scrollview的任何子布局中获取屏幕截图。
You can use this code to take a screenshot of your full ScrollView
the same code can be adapted to all kind of View
s 您可以使用此代码截取完整ScrollView
的屏幕快照,相同的代码可以适应所有View
Code : 代码:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.