机器人:以一个ScreenShoot在滚动型的所有视图 [英] Android: Taking a ScreenShoot for all view in ScrollView
问题描述
我尝试采取ScreenShoot所有视图滚动型
,我的滚动型
都有溅出的内容屏幕(因此滚动成为可能),我怎么能保证屏幕截图包括不可见的屏幕上?
I try to take a ScreenShoot 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?
我一直用这个code:
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;
}
图片文件创建,但只显示黑屏,而不是我想要的看法,所以是不是有什么毛病我code?是否有任何人能帮助我,好吗?
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? Is there anyone can help me, please?
更新
请@ 113408的回答后,我得到了错误,这是我的的logcat
:
After follow @113408 's Answer, I got Error, this is my 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)
有关注意事项:十月8号至27号:40:57.555:E / AndroidRuntime(4957):在id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
是指位图B = Bitmap.createBitmap(u.getDrawingCache());
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());
推荐答案
您可以使用此code采取完整的滚动型
相同的$ C截图$ C可适应所有类型的查看
取值
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
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();
}
这篇关于机器人:以一个ScreenShoot在滚动型的所有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!