2011-02-18 50 views
1

我想將MyView的擴展TextView的內容保存(導出)爲位圖。將文本視圖(包括屏幕上的內容)轉換爲位圖

我跟着代碼:[this] [1]。

當文本的大小很小時,它工作正常。

但是,當有很多文本,並且一些內容不在屏幕上時,我得到的只是屏幕上顯示的內容。

然後,我在我的代碼添加一個「佈局」:

private class MyView extends TextView{ 
    public MyView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public Bitmap export(){ 
     Layout l = getLayout(); 
     int width = l.getWidth() + getPaddingLeft() + getPaddingRight(); 
     int height = l.getHeight() + getPaddingTop() + getPaddingBottom(); 

     Bitmap viewBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(viewBitmap); 


     setCursorVisible(false); 
     layout(0, 0, width, height); 
     draw(canvas); 

     setCursorVisible(true); 
     return viewBitmap; 
    } 
} 

現在,奇怪的事情發生了:

我第一次調用「出口」(我用的選項鍵來做到這一點) ,我只在屏幕上看到內容。

當我再次調用「導出」時,我得到了完整的內容,包括那些不在屏幕上的內容。

爲什麼?

如何「導出」一個視圖,包括內容無法顯示在屏幕上?

謝謝!

[1]:http://www.techjini.com/blog/2010/02/10/quicktip-how-to-convert-a-view-to-an-image-android/

回答

1

我發現了一個簡單的方法: 把TextView的一個滾動型。 現在myTextView.draw(canvas)將繪製所有的文本。

0

我想你應該減去寬度填充在高度而不是添加它的。添加它會給你一個比屏幕更大的區域。

+0

感謝您的提醒。但是這並沒有回答我的問題。 – 2011-02-19 09:27:32

0

我解決了這個問題,這種方式(奇怪,但工程):

public Bitmap export(){ 
    //... 
    LayoutParams lp = getLayoutParams(); 
    int old_width = lp.width; 
    int old_height = lp.height; 
    int old_scroll_x = getScrollX(); 
    int old_scroll_y = getScrollY(); 
    lp.width = width; 
    lp.height = height; 
    layout(0, 0, width, height); 
    scrollTo(0, 0); 
    draw(canvas); 
    lp.width = old_width; 
    lp.height = old_height; 
    setLayoutParams(lp); 
    scrollTo(old_scroll_x, old_scroll_y); 
    //... 

}

相關問題