我有ScrollView。在这个ScrollView里面我有HorizontalScrollView。在HorizontalScrollView里面我有RelativeLayout。这是我的布局:
<ScrollView
android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<HorizontalScrollView
android:id="@+id/hor"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/lay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ffffff" >
</RelativeLayout >
</HorizontalScrollView>
</ScrollView>
在OnCreate中,我使用一些控件来提升RelativeLayout。然后我尝试对ScrollView中的所有内容进行屏幕截图(这可以在屏幕上看到,这在屏幕上看不到)。这是我的代码:
public void takeScreenshot() throws IOException {
View u = findViewById(R.id.scroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.scroll);
z.setEnabled(false);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
int storedHeight = z.getHeight();
int storedWidth = z.getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
u.layout(0, 0, storedWidth, storedHeight);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
File folderPath = new File(extr);
folderPath.mkdirs();
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").format(new Date());
File myPath = new File(extr, fileName);
myPath.createNewFile();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.PNG, 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();
}
}
当我尝试构建位图时,我在u.getDrawingCache()
中得到null,这会导致我的应用崩溃。如果我对u.layout(0, 0, totalWidth, totalHeight);
发表评论,请注意它不会崩溃,但我的屏幕截图是错误的。如何在ScrollView /我在这里做错的内容中对所有内容进行大屏幕排版?