gpt4 book ai didi

java - 将 ScrollView 中的所有内容转换为位图?

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:52 26 4
gpt4 key购买 nike

我尝试将我的 ScrollView 转换为 bitmap,我的 ScrollView 有内容溢出屏幕(因此滚动成为可能), 在我 Ask here 之后我可以在 ScrollView 中捕获我的 Activity,但我遇到了一个问题,用于保存屏幕截图的 Bitmap 不会创建所有 View ,只有 ScrollView 的最后一个>,其余部分为黑屏,如下所示: enter image description here

这是我为 ScrollView 中的所有 View 截屏的代码:

 scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp);
utktest=polis.findViewById(R.id.scroll_pp);
int totalHeight = scroll_pp.getChildAt(0).getHeight();
int totalWidth = scroll_pp.getChildAt(0).getWidth();
Bitmap b= MethodSupport.loadBitmapFromView(utktest, 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) {

e.printStackTrace();
} 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.draw(c);
return b;
}

那么是不是我的代码出了什么问题,所以我无法根据我的需要显示位图

最佳答案

您需要获取 ScrollView 的 First children将其转换为 Bitmap

示例:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
android:id="@+id/rlChildContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

</RelativeLayout>
</ScrollView>

您应该尝试仅在加载屏幕和绘制布局时加载位图,不要在 Activity 的 onCreate 方法中执行此操作。

关于java - 将 ScrollView 中的所有内容转换为位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25561752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com