2012-01-05 36 views
8

Tôi sử dụng mã bên dưới để chuyển đổi, nhưng có vẻ như chỉ có thể lấy nội dung trong màn hình hiển thị và không thể lấy nội dung không có trong màn hình hiển thị.Làm thế nào để chuyển đổi tất cả nội dung trong một scrollview sang một bitmap?

Có cách nào để đưa tất cả nội dung ra khỏi cuộn không?

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

Trả lời

15

Chúng tôi có thể chuyển đổi tất cả các nội dung của một scrollview đến một hình ảnh bitmap sử dụng mã hiển thị dưới đây

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

này là không hoạt động khi quan điểm sử dụng scrollTo có nhu cầu nội bộ di chuyển nội dung của họ http://developer.android.com/reference/android/view/View.html#scrollTo% 28int,% 20int% 29 –

+0

hoạt động tốt .. cảm ơn bạn –

0

Vấn đề ở đây là nội dung pixel thực tế duy nhất từng tồn tại là hiển thị trên màn hình hiển thị. Android và các nền tảng di động khác rất cẩn thận về việc sử dụng bộ nhớ và một trong những cách mà chế độ xem cuộn có thể duy trì hiệu suất là không vẽ bất kỳ thứ gì ngoài màn hình. Vì vậy, không có bitmap "đầy đủ" ở bất cứ đâu - bộ nhớ chứa nội dung di chuyển ngoài màn hình được tái chế.

1

Bạn cần có tổng chiều rộng và chiều cao của chế độ xem cuộn hoặc bạn đã tạo chế độ xemBitmap quá nhỏ để chứa toàn bộ nội dung của chế độ xem cuộn.

kiểm tra liên kết này Android: Total height of ScrollView

5

Câu trả lời Pops là thực sự tốt, nhưng trong một số trường hợp bạn có thể phải tạo một bitmap thực sự lớn có thể kích hoạt một OutOfMemoryException khi bạn tạo bitmap.

Vì vậy, tôi thực hiện một ít tối ưu hóa để được nhẹ nhàng với bộ nhớ :)

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; 
} 
1

một này làm việc cho tôi

Để tiết kiệm cho phép thời gian chạy kiểm tra bitmap đầu tiên

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

      } 

Để nhận bitmap

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

    } 

Để lưu 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); 
     } 



    } 
+0

hoạt động hoàn hảo! cảm ơn bạn .) –

Các vấn đề liên quan