2010-05-10 86 views
96

我會盡力解釋我到底需要做什麼。將視圖轉換爲位圖而不顯示在Android中?

我有3個單獨的屏幕說A,B,C。還有另一個叫做HomeScreen的屏幕,其中所有3個屏幕位圖都應該顯示在圖庫視圖中,並且用戶可以選擇他想要去哪個視圖。

我已經能夠獲得所有3個屏幕的位圖,並通過將所有代碼放置在HomeScreen活動中,將其顯示在圖庫視圖中。現在,這已經使代碼複雜了許多,我想簡化它。

那麼,我可以從HomeScreen調用另一個Activity,不顯示它,只是獲得該屏幕的位圖。例如,假設我只是調用HomeScreen,它會調用活動A,B,C,並且不會顯示A,B,C中的任何活動。它只是通過getDrawingCache()給出該屏幕的位圖。然後我們可以在HomeScreen中的圖庫視圖中顯示這些位圖。

我希望我已經非常清楚地解釋了這個問題。

請讓我知道這是否可能。

+4

其實,我能做這個。 – sunil 2011-03-23 11:38:27

+1

我不完全確定,但我認爲你將無法做到這一點。問題在於活動是要顯示給用戶的。您可以啓動該活動,然後立即隱藏該活動,但該活動對於用戶而言仍然可以在瞬間看到。它顯示足夠長的時間才能被注意,因此屏幕閃爍幾次使得應用看起來不專業。但是,可能有一條命令啓動一個活動而不顯示它;我只是不知道它是否存在。 – 2010-05-10 09:45:16

+0

你好,如果你得到解決方案,那麼你能幫助我嗎? – Kalpesh 2012-07-10 12:24:49

回答

157

有一種方法可以做到這一點。你必須創建一個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(); 
+0

我試過這個,但我所得到的只是一個半透明的黑盒子。我需要在視圖上做些什麼來準備好進行位圖繪製嗎? – Bobbake4 2011-10-14 19:02:50

+4

我實際上不得不將它改成'v.layout(v.getLeft(),v.getTop(),v.getRight(),v.getBottom());'爲了讓它正常工作,但是感謝代碼:) – triad 2012-08-17 19:06:17

+2

我不得不使用v.getWidth()而不是v.getLayoutParams()。width和height。否則,現在工作。 – 2012-09-03 05:55:58

19

這裏是我的解決方案:

public static Bitmap getBitmapFromView(View view) { 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),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

+1謝謝你的回答 – 2012-08-01 05:45:25

+0

謝謝。如果身高超過某個值,我在某些設備上遇到問題。我還沒有完全測試,但這似乎解決了這個問題。 – 2013-11-13 00:58:20

19

試試這個,

/** 
     * Draw the view into a bitmap. 
     */ 
     private 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; 
     } 
+0

這個開箱即用,謝謝! – muetzenflo 2013-07-04 11:44:44

+0

@muetzenflo歡迎夥伴! – 2013-07-04 11:58:19

+0

如何從我的主要活動課程中使用它? – Si8 2014-02-06 01:19:20

10

我知道這可能是一個陳舊的問題,但我得到任何這些解決方案爲我工作遇到的問題。 具體而言,我發現如果對視圖進行了充氣後對視圖進行了任何更改,則這些更改將不會合併到呈現的位圖中。

這是最後爲我的案件工作的方法。但有一點需要注意。在致電getViewBitmap(View)之前,我誇大了我的觀點,並要求它以已知尺寸進行佈局。這是需要的,因爲我的視圖佈局會在內容放置到內部之前使其高度/寬度爲零。

View view = LayoutInflater.from(context).inflate(layoutID, null); 
//Do some stuff to the view, like add an ImageView, etc. 
view.layout(0, 0, width, height); 

Bitmap getViewBitmap(View view) 
{ 
    //Get the dimensions of the view so we can re-layout the view at its current size 
    //and create a bitmap of the same size 
    int width = view.getWidth(); 
    int height = view.getHeight(); 

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); 
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); 

    //Cause the view to re-layout 
    view.measure(measuredWidth, measuredHeight); 
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

    //Create a bitmap backed Canvas to draw the view into 
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas 
    view.draw(c); 

    return b; 
} 

「神奇武器」,我發現這裏: https://groups.google.com/forum/#!topic/android-developers/BxIBAOeTA1Q

乾杯,

列維

1

希望這有助於

View view="some view instance";   
view.setDrawingCacheEnabled(true); 
Bitmap bitmap=view.getDrawingCache(); 
view.setDrawingCacheEnabled(false); 
相關問題