android - 将完整滚动视图内容转换为位图时出现黑色
问题描述
单击时,我正在尝试将其转换scrollView
为 jpg ImageView
。它只转换可见的视图,而不是完整的内容。如果我手动输入 2500 值而不进行totalHeight
任何更改,它只会提供带有黑色的当前视图以获得额外尺寸。
shareEstimation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ScrollView structureEstimation = findViewById(R.id.structure_estimate);
View u = StructureResult.this.findViewById(R.id.structure_estimate);
int totalWidth = structureEstimation.getChildAt(0).getWidth();
int totalHeight = structureEstimation.getChildAt(0).getHeight();
Bitmap b = getBitmapFromView(u, totalHeight, totalWidth);
//Bitmap b = getBitmapFromView(u, 2500, totalWidth);
String root = getFilesDir().toString();
String fname = "Estimation.jpg";
File file = new File(root, fname);
try {
FileOutputStream out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
//MediaStore.Images.Media.insertImage(mContext.getContentResolver(), b, "Screen", "screen");
} catch (Exception e) {
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, file), "image/*");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, file));
startActivity(Intent.createChooser(i, "Share via"));
}
});
这是我的布局模式
<RelativeLayout>
<ScrollView>
<LinearLayout/>
</ScrollView>
</RelativeLayout>
解决方案
我得到了解决方案。我从滚动视图获取高度,从线性布局获取视图(滚动视图的子级)
ScrollView structureEstimation = findViewById(R.id.structure_estimate);
View u = StructureResult.this.findViewById(R.id.strct_layout);
int totalWidth = structureEstimation.getChildAt(0).getWidth();
int totalHeight = structureEstimation.getChildAt(0).getHeight();
Bitmap b = getBitmapFromView(u, totalHeight, totalWidth);
推荐阅读
- asp.net - 我的项目在发布后没有创建文件夹
- kubernetes - Kubernetes 是否允许并使用多个 imagePullSecrets 从私有注册表中提取图像?
- c# - 如何在 WPF (C#) 中获取鼠标移动方向
- r - 使用 gsub 替换 R 中最后出现的字符串
- c# - 了解 C# 属性
- react-native - 没有互联网时 React Native webview
- azure - Azure 文本到语音 API - 仅限 10 分钟的音频?
- c# - C#中的浮点解析
- c++ - OpenGL渲染黑色方块
- python - 使用 Raspberry Pi 3 在 Docker 上安装 TensorFlow