【发布时间】:2015-12-11 10:53:41
【问题描述】:
如何在android中截取完整的滚动视图? 我在 stackoverflow 中发现了很多问题,但没有解决我的问题。
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
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;
}
这是我最终使用的代码。它使活动屏幕的图像与其他区域变暗。 当使用这个 android make screenshot of entire layout
xml布局文件内容
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/rootofall"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ScrollView
android:layout_below="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
>
<TextView android:text="@string/alotofdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/button"
android:id="@+id/textView" />
</ScrollView>
</RelativeLayout>
【问题讨论】:
-
好问题。我从来没有找到合适的解决方案。
-
你的观点是什么(xml)?可以发一下吗?
-
@StefanBeike ya 这真的很有帮助。如果我们在这里找到解决方案。 :)
-
位图位图 = Bitmap.createBitmap(scrollView.getChildAt(0).getWidth(), scrollView.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);我认为你应该试试这个
-
stackoverflow.com/a/54779181/7074112 检查此答案以获得可能的解决方案
标签: java android image-processing screen-scraping