首页 > 解决方案 > 将完整滚动视图内容转换为位图时出现黑色

问题描述

单击时,我正在尝试将其转换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>

标签: androidscrollviewandroid-bitmap

解决方案


我得到了解决方案。我从滚动视图获取高度,从线性布局获取视图(滚动视图的子级)

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);

推荐阅读