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

问题描述:

我需要将我的滚动视图的所有内容转换为android之前,我需要计算滚动视图的高度和宽度。如何计算?计算滚动视图的高度和宽度,并将滚动视图内容转换为图像

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

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(); 
     } 
+0

有什么目的其实?你想要做的事很不寻常。 –

+0

我需要将我的图像转换为我的应用程序的pdf文档。 –

检查了这一点

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

      } 
+0

谢谢你的回应,我会尝试 –

+0

它会给你整个Scrollview内容到Bitmap。 –

+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扩展滚动型{

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); 
    } 
} 
+0

谢谢你的回应 –

+0

我用scrollview,我可以改变我的scrollview到scrollViewHelper? –

+0

发生同样的错误,引起:java.lang.IllegalArgumentException:宽度和高度必须大于0 –