有一種方法可以做到這一點。你必須創建一個Bitmap和一個Canvas並調用view.draw(canvas);
這裏是代碼:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
,如果它的大小將是零之前的觀點並沒有顯示出來。它可以測量它是這樣的:
if (v.getMeasuredHeight() <= 0) {
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
編輯:根據this post,傳遞WRAP_CONTENT
作爲價值makeMeasureSpec()
並沒有做任何好的(雖然一些視圖類它的工作),以及推薦方法是:
// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();
其實,我能做這個。 – sunil 2011-03-23 11:38:27
我不完全確定,但我認爲你將無法做到這一點。問題在於活動是要顯示給用戶的。您可以啓動該活動,然後立即隱藏該活動,但該活動對於用戶而言仍然可以在瞬間看到。它顯示足夠長的時間才能被注意,因此屏幕閃爍幾次使得應用看起來不專業。但是,可能有一條命令啓動一個活動而不顯示它;我只是不知道它是否存在。 – 2010-05-10 09:45:16
你好,如果你得到解決方案,那麼你能幫助我嗎? – Kalpesh 2012-07-10 12:24:49