2011-04-21 110 views
0

我想讓背景圖像拉伸所有屏幕。我只是創建了一個VerticalFieldManager並修改了繪製方法。將位圖繪製爲MainScreen背景。如何拉伸圖像?

verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH 
          | VerticalFieldManager.NO_HORIZONTAL_SCROLLBAR) { 
      public void paint(Graphics graphics) { 
        graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(), 
             backgroundBitmap, 0, 0); 
        super.paint(graphics); 
     } 

    }; 

而當我添加一個ListField到verticalFieldManager和滾動,背景圖像不重畫。滾動時如何重繪背景?

回答

2

從您的Bitmap創建EncodedImage,或者甚至直接從資源中提取EncodedImage -​​。

然後用它來擴展它:

public static EncodedImage resize(EncodedImage eImage, int toWidth, 
     int toHeight, boolean keepAspectRatio) { 

    int scaleX = Fixed32.div(
     Fixed32.toFP(eImage.getWidth()), 
     Fixed32.toFP(toWidth) 
    ); 

    int scaleY = Fixed32.div(
     Fixed32.toFP(eImage.getHeight()), 
     Fixed32.toFP(toHeight) 
    ); 

    if (keepAspectRatio) { 
     int scale = (scaleX > scaleY) ? scaleX : scaleY; 
     return eImage.scaleImage32(scale, scale); 
    } else { 
     return eImage.scaleImage32(scaleX, scaleY); 
    } 
} 

那麼您可Graphics對象上繪製EncodedImage

graphics.drawImage(
    0, 0, 
    encodedImage.getScaledWidth(), ncodedImage.getScaledHeight(), 
    encodedImage, 0, 0, 0 
); 
+0

好極了,它的工作原理。非常感謝你阿哈邁德。 你能幫我解決ListField滾動問題嗎?當我滾動時,背景圖像也滾動。滾動事件發生時應該重繪背景圖像嗎? – redline 2011-04-21 11:31:23

+0

哦,不,當我使用這個代碼時,在listview的最後一項之後總是有一個空格。這是不好的。 – redline 2011-04-21 11:56:05

+0

爲了幫助解決滾動問題,我需要試驗一下代碼。對不起,現在實驗是不可能的 - 我也面臨時間壓力。然而,您嘗試的想法是將BG繪圖的開始Y座標轉換爲與當前Ysroll相同的像素數(使用容器的'Manager.getVerticalScroll()')。 – 2011-04-21 11:58:32