【问题标题】:How to make an image of a complete scrolling view in android如何在android中制作完整滚动视图的图像
【发布时间】: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


【解决方案1】:

尝试通过滚动视图来获取位图。这可能会帮助你。我正在获取整个视图的位图。需要将此代码与 Handler 或 layoutObserver 一起使用,因为这只有在布局膨胀后才有效。我曾尝试使用 Handler 延迟 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);

【讨论】:

  • 只是给一个黑色的图像。
  • 你可能直接在 onCreate 中使用它。
  • 我在按钮点击中使用它。
  • 您如何测试位图结果?通过保存还是在 imageView 中?
  • 可能是写入文件有问题...尝试在ImageView中查看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
  • 2012-09-01
  • 1970-01-01
  • 2011-03-04
  • 2020-06-22
  • 1970-01-01
相关资源
最近更新 更多