如何将scrollview中的所有内容转换为位图?

我使用下面的代码进行转换,但似乎只能得到显示屏幕中的内容,并且无法获取不在显示屏幕中的内容。

有没有办法让所有的内容甚至没有滚动?

Bitmap viewBitmap = Bitmap.createBitmap(mScrollView.getWidth(),mScrollView.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(viewBitmap); mScrollView.draw(canvas); 

Solutions Collecting From Web of "如何将scrollview中的所有内容转换为位图?"

我们可以使用下面显示的代码将scrollView的所有内容转换为位图图像

 private void takeScreenShot() { View u = ((Activity) mContext).findViewById(R.id.scroll); HorizontalScrollView z = (HorizontalScrollView) ((Activity) mContext).findViewById(R.id.scroll); int totalHeight = z.getChildAt(0).getHeight(); int totalWidth = z.getChildAt(0).getWidth(); Bitmap b = getBitmapFromView(u,totalHeight,totalWidth); //Save bitmap String extr = Environment.getExternalStorageDirectory()+"/Folder/"; 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(mContext.getContentResolver(), b, "Screen", "screen"); }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) { Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(returnedBitmap); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) bgDrawable.draw(canvas); else canvas.drawColor(Color.WHITE); view.draw(canvas); return returnedBitmap; } 

Pops的答案是非常好的,但在某些情况下,您可能必须创build一个非常大的位图,在创build位图时可能会触发OutOfMemoryException。

所以我做了一点优化,以温和的内存:)

 public static Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) { int height = Math.min(MAX_HEIGHT, totalHeight); float percent = height / (float)totalHeight; Bitmap canvasBitmap = Bitmap.createBitmap((int)(totalWidth*percent),(int)(totalHeight*percent), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(canvasBitmap); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) bgDrawable.draw(canvas); else canvas.drawColor(Color.WHITE); canvas.save(); canvas.scale(percent, percent); view.draw(canvas); canvas.restore(); return canvasBitmap; } 

您需要获取scrollview的总宽度和高度,否则您创build的viewBitmap太小而不能包含scrollview的全部内容。

检查这个链接Android:ScrollView的总高度

这个为我工作

首先保存位图检查运行时权限

  @OnClick(R.id.donload_arrow) public void DownloadBitMap() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { downloadData(); Log.e("callPhone: ", "permission" ); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); Toast.makeText(this, "need permission", Toast.LENGTH_SHORT).show(); } } 

获取位图

  private void downloadData() { ScrollView iv = (ScrollView) findViewById(R.id.scrollView); Bitmap bitmap = Bitmap.createBitmap( iv.getChildAt(0).getWidth()*2, iv.getChildAt(0).getHeight()*2, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); c.scale(2.0f, 2.0f); c.drawColor(getResources().getColor(R.color.colorPrimary)); iv.getChildAt(0).draw(c); // Do whatever you want with your bitmap saveBitmap(bitmap); } 

保存位图

  public void saveBitmap(Bitmap bitmap) { File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "SidduInvoices"); boolean success = true; if (!folder.exists()) { success = folder.mkdirs(); } if (success) { // Do something on success } else { // Do something else on failure } File imagePath = new File(Environment.getExternalStorageDirectory() + "/SidduInvoices/Siddus.png"); if(imagePath.exists()) { imagePath=new File(Environment.getExternalStorageDirectory() + "/SidduInvoices/Siddus"+custamername.getText().toString()+".png"); } FileOutputStream fos; try { fos = new FileOutputStream(imagePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); progressBar.cancel(); final File finalImagePath = imagePath; new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE) .setTitleText("Saved") .setContentText("Do you want to share this with whatsapp") .setCancelText("No,cancel !") .setConfirmText("Yes,share it!") .showCancelButton(true) .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sweetAlertDialog) { sweetAlertDialog.cancel(); shareImage(finalImagePath); } }) .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) { sDialog.cancel(); } }) .show(); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } } 

这里的问题是唯一存在的实际像素内容是在显示屏上可见的像素内容。 Android和其他移动平台对内存使用非常小心,滚动视图可以保持性能的方式之一是不要画任何不在屏幕外的东西。 所以在任何地方都没有“完整的”位图 – 包含移出屏幕外的内容的内存被回收。