计算滚动视图的高度和宽度,并将scrollview内容转换为图像

时间:2017-10-13 09:56:27

标签: android android-scrollview

我需要在我需要计算滚动视图的高度和宽度之前,将我的Scroll视图所有内容转换为Android中的Image。怎么算?

我使用下面的代码,但是我的应用程序因高度和宽度大于零的错误而崩溃。

 View u = findViewById(R.id.trustReportScrollView);
        ScrollView z = (ScrollView) findViewById(R.id.trustReportScrollView);
       /* int totalHeight = z.getChildAt(0).getHeight();
        int totalWidth = z.getChildAt(0).getWidth();*/
     /*   int totalHeight = 500;
        int totalWidth = 500;*/ -->if i used this two line, i get image but empty white image, nothing in that image

        Bitmap b = getBitmapFromView(u,totalHeight,totalWidth); -- >here i get crashed

        //Save bitmap
        String extr = Environment.getExternalStorageDirectory()+"/TestingDir1/";
        String fileName = "report.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(getContentResolver(), b, "Screen", "screen");
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:0)

检查出来

int totalHeight = scrollview.getChildAt(0).getHeight();
            int totalWidth = scrollview.getChildAt(0).getWidth();
            try {
               Bitmap  returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);

            } catch (Exception e) {
                e.printStackTrace();

            }

答案 1 :(得分:0)

使用以下代码将滚动视图内容转换为图像。

//step :1
    int height;
    int width;
    ScrollViewHelper iv;

//step :2
    iv = (ScrollViewHelper) findViewById(R.id.scrollViewHelper);
    iv.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

                @Override
                public void onScrollChanged() {


                    height = iv.getChildAt(0).getHeight();
                    width = iv.getChildAt(0).getWidth();

                }
    });

//step :3  
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    Canvas ccc = new Canvas(bitmap);
                    iv.getChildAt(0).draw(ccc);
                    SaveImage(bitmap);




//step :4

    private void SaveImage(Bitmap finalBitmap) {

            String root = Environment.getExternalStorageDirectory().toString();
        //File myDir = new File("/storage/emulated/0/DCIM");
             File myDir = new File( root+  File.separator + "Vengat.jpg");
            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();


                File filePath =new File(file.toString());  //optional //internal storage
               // File filePath =new File(Environment.getExternalStorageDirectory().toString());  //optional //internal storage

                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Open it in Google Play Store to Download the Application");
               shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath));  //optional//use this when you want to send an image
                shareIntent.setType("*/*");
                shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(Intent.createChooser(shareIntent, "send"));



            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.i("", "******* File not found. Did you"
                        + " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
            }


             catch (Exception e) {
                e.printStackTrace();
            }


        }   

//////////////////////

<ScrollViewHelper
android:id="@+id/scrollViewHelper"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ScrollView
 android:layout_width="match_parent"
    android:layout_height="wrap_content">

    </ScrollView>

    </ScrollViewHelper>

//////////////// ScrollViewHelper: -

公共类ScrollViewHelper扩展了ScrollView {

    private OnScrollViewListener mOnScrollViewListener;

    public ScrollViewHelper(Context context) {
        super(context);
    }
    public ScrollViewHelper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ScrollViewHelper(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public interface OnScrollViewListener {
        void onScrollChanged(ScrollViewHelper v, int l, int t, int oldl, int oldt);
    }

    public void setOnScrollViewListener(OnScrollViewListener l) {
        this.mOnScrollViewListener = l;
    }

    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        mOnScrollViewListener.onScrollChanged( this, l, t, oldl, oldt );
        super.onScrollChanged( l, t, oldl, oldt );
    }
}