2017-01-03 27 views
0

一個很長的活動。如果您的活動有太多的內容,並顯示一個很長的用戶界面,像含有一個ListView或WebView中,如何得到一個完整的截圖,如果你有滾動條

我們怎樣才能截圖包含長影像內的全部內容?

+0

也許我不清楚,所以有人低估了這個問題,@Amir Ziarati的回答非常好。我將這個問題標記爲**重複** –

+0

[如何將scrollview中的所有內容轉換爲位圖?](http://stackoverflow.com/questions/8738448/how-to-convert-all -content-IN-A-滾動視圖到一個位圖) –

回答

1

我想在這個環節的回答可以幫助你:

How to convert all content in a scrollview to a bitmap?

我混在連接兩個答案以上,使你的一個優化的一段代碼:

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

您可以輕鬆地將佈局轉換爲「位圖圖像」

protected Bitmap ConvertToBitmap(LinearLayout layout) { 

    layout.setDrawingCacheEnabled(true); 

    layout.buildDrawingCache(); 

    Bitmap bitmap = layout.getDrawingCache(); 

    return bitmap; 

} 
1

嘗試使用此, 傳遞您的視圖該功能,

這個函數會返回一個位圖,你可以利用它你想要的方式。

相關問題