我們假設我有一個ImageView,Grid或任何控件。我想要做的是截取這個控件背後的內容,讓我們來說說另一個控件,它包含背景圖像或任何可能存在的東西。有什麼辦法可以做到這一點?我想到了getDrawingCache();方法,但也會採用頂級控制。如何在Android中使用ImageView /任何UI組件後面的截圖/位圖?
我想這樣做,這樣我就可以對我的頂級控件背後的模糊透明效果。我已經有一個模糊的方法,我只需要採取背景的確切圖片。任何幫助將不勝感激!
我們假設我有一個ImageView,Grid或任何控件。我想要做的是截取這個控件背後的內容,讓我們來說說另一個控件,它包含背景圖像或任何可能存在的東西。有什麼辦法可以做到這一點?我想到了getDrawingCache();方法,但也會採用頂級控制。如何在Android中使用ImageView /任何UI組件後面的截圖/位圖?
我想這樣做,這樣我就可以對我的頂級控件背後的模糊透明效果。我已經有一個模糊的方法,我只需要採取背景的確切圖片。任何幫助將不勝感激!
您可以使用任何視圖的getDrawingCache()
方法,所以如果你有你的背景參考查看它應該是這樣工作的:
<your background view>.setDrawingCacheEnabled(true);
Bitmap contentBitmap = Bitmap.createBitmap(<your background view>.getDrawingCache());
<your background view>.setDrawingCacheEnabled(false);
的contentBitmap
現在應該包含什麼是位圖那種觀點。
public static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e(tag, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
謝謝!通過appContent你是什麼意思?相同的背景視圖? –
是的,這是相同的背景視圖,我錯過了一個,當我重新命名 –