2012-02-15 50 views
6

問題描述:
我已經測試了以下兩種方法,以便從視圖(例如本例中的RelativeLayout)創建位圖快照。對於尺寸(例如寬度和高度)小於設備屏幕尺寸(例如480x900)的視圖,這兩種方法都非常適用。視圖的可見部分被捕獲並寫入到位圖中。不幸的是,位圖在視圖的不可見部分是黑色的。Android:從超大視圖的位圖

問題:
如何捕獲視圖的不可見部分?

CODE:

public class NewRelativeLayout extends RelativeLayout{ 

private Bitmap bmp; 

public NewRelativeLayout(Context context){ 
    super(context); 
    this.setLayoutParams(new RelativeLayout.LayoutParams(2000,2000)); 
    // add here methods to configure the RelativeLayout or to add children 
    } 

//This is the first method 
public void makeSnapshot_Method1(){ 
    this.setDrawingCacheEnabled(true); 
    bmp = Bitmap.createBitmap(this.getDrawingCache()); 
    this.setDrawingCacheEnabled(false); 
    } 

//This is the second method 
public void makeSnapshot_Method2(){ 
    bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888); 
    Canvas helpCanvas = new Canvas(bmp); 
    this.draw(helpCanvas); 
    } 

}

回答

0

取決於你想捕捉,從視圖中的背景或前景圖像的映像。無論哪種方式,你必須從它們中獲取Drawable對象,將它們轉換爲BitmapDrawable並從它們獲取Bitmap。 代碼: -

BitmapDrawable bd=(BitmapDrawable) view.getBackground(); //if you need background image 
BitmapDrawable bd=(BitmapDrawable) view.getDrawable(); //if you need foreground image (Only available to some views who have an android:src xml property) 
Bitmap bm=bd.getBitmap(); 

我希望這會對你有用。

+0

感謝創造者爲您的快速響應。 view.getDrawable()方法僅由Views支持,它像ImageView那樣「持有」一個前景圖片。像RelativeLayouts,LinearLayouts,TextViews等所有其他視圖不支持此方法。大多數互聯網論壇推薦方法1或2(見我的文章),以便從視圖製作位圖截圖(即背景和前景)。對於超大視圖,問題仍未解決。任何其他想法? – 2012-02-15 18:54:59

+0

那麼,我已經提到,view.getDrawable()不支持在帖子本身的每個視圖。 這兩種方法給出了完整的圖像,包括屏幕外部分,可以嘗試使用畫布和自定義視圖將這兩個單獨的圖像放在一個視圖中(如果forground一個是透明背景png圖像),並獲取雖然我從來沒有嘗試過,所以不能說出這將如何工作,但如果你能遵循,你可能會實現你所需要的。 – noob 2012-02-15 19:15:46

+0

getDrawingCache()方法旨在僅給出屏幕上的圖像,所以我不認爲這適用於您。使用getDrawingCache(true)可能適合你。 另一個想法可能是使用LayoutParams調整視圖以適合屏幕,然後從那裏獲取繪圖緩存。 – noob 2012-02-15 19:19:46

0

我也想弄清楚一個ListView,獲取所有不在屏幕上的行。

我發現很多東西我想與大家分享:

這裏有人問,如果一個位圖可以從不是繪製的視圖中創建: Bitmap from View not displayed on Android

在這裏,他們聲稱這是一個重複的,它的也回答正確(?)它包含一個URL,其中包含一些舊的編碼,並且不會編譯。此外可能重複的問題並沒有幫助我。 Android get full View as Bitmap

我想我會設法把事情通過此方法進行: List View using bitmap 已經想出了同樣的想法,現在我還是要探索如何。

希望你能使用一些這方面的信息。

1

我正在尋找解決方案,最終設法提出了我自己的解決方案,其實很簡單。

就我而言,我有一個LinearLayout,其中包含一些View元素,其中有些元素有時不在屏幕上,垂直低於屏幕邊界的末端。我能夠將View保存爲位圖(請參閱下面的loadBitmapFromView()),但當它延伸到屏幕底部時遇到問題。

我的解決方案是將LinearLayout組合爲ScrollView組合的一部分。

例如

此:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/my_linear_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
<!-- Some Views added here at runtime --> 
</LinearLayout> 

變成了:

注意使用wrap_content確保滾動型擴展到內容的高度。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 

    <LinearLayout 
     android:id="@+id/my_linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
    > 
     <!-- Some Views added here at runtime --> 
    </LinearLayout> 
</ScrollView> 

這似乎保證了屏幕外的項目可以手動佈局時,我想的View保存爲位圖。我用下面的方法保存位圖。我很早就發現了這一點,但我似乎無法找到正確引用它的問題(無論你是誰 - 謝謝!)。

對於以下方法,我通過參考上面的LinearLayoutmy_linear_layout)。

public static Bitmap loadBitmapFromView(View view) { 
    Bitmap bitmap = null; 

    // width measure spec 
    int widthSpec = View.MeasureSpec.makeMeasureSpec(
      view.getMeasuredWidth(), View.MeasureSpec.AT_MOST); 
    // height measure spec 
    int heightSpec = View.MeasureSpec.makeMeasureSpec(
      view.getMeasuredHeight(), View.MeasureSpec.AT_MOST); 

    // measure the view 
    view.measure(widthSpec, heightSpec); 

    // set the layout sizes 
    int left = view.getLeft(); 
    int top = view.getTop(); 
    int width = view.getMeasuredWidth(); 
    int height = view.getMeasuredHeight(); 
    int scrollX = view.getScrollX(); 
    int scrollY = view.getScrollY(); 

    view.layout(left, top, width + left, height + top); 

    // create the bitmap 

    bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), 
      Bitmap.Config.ARGB_8888); 

    // create a canvas used to get the view's image and draw it on the 
    // bitmap 

    Canvas c = new Canvas(bitmap); 
    // position the image inside the canvas 
    c.translate(-view.getScrollX(), -view.getScrollY()); 
    // get the canvas 
    view.draw(c); 

    return bitmap; 
}