如何在安卓设备中制作完整滚动视图的图像

4
如何在安卓设备上截取完整滚动视图的屏幕截图? 我在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制作整个布局的截图

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>

1
好问题。我从未找到合适的解决方案。 - s_bei
你怎么看待 XML?能把它发出来吗? - Vishal Patel
@StefanBeike,如果我们在这里找到了解决方案,那将非常有帮助。 :) - jomin v george
Bitmap bitmap = Bitmap.createBitmap(scrollView.getChildAt(0).getWidth(), scrollView.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888); 我认为你应该尝试这个。 - Vishal Patel
请查看此答案以获取可能的解决方案:https://dev59.com/3nE85IYBdhLWcg3wr1ge#54779181 - Peter
1个回答

3

尝试将scrollview传递以获取位图。这可能会有所帮助。我正在获取整个视图的位图。需要在处理程序或布局观察器中使用此代码,因为仅在布局膨胀后才有效。我已经尝试使用2秒的延迟后处理程序。

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
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);

//Do whatever you want with your bitmap

saveBitmap(bitmap);


只是给一个黑色的图像。 - jomin v george
我正在使用它在按钮的onclick事件中。 - jomin v george
你是如何测试位图结果的?是通过保存还是在imageView中? - Arth Tilva
可能是文件写入出现了问题...请尝试在 ImageView 中检查它。 - Arth Tilva
但是我得到了一个空的imageView。 - jomin v george
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接