在将完整滚动视图内容转换为位图时获得黑色



我正在尝试在单击ImageView时将scrollView转换为jpg。它仅转换可见的视图,而不转换完整内容。如果我手动输入 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);

最新更新