2011-12-05 28 views
1

我正在繪製Bitmap作爲自定義字段的背景,但它不在整個字段的區域中繪製位圖。我也調整了圖像的大小,但總是會在右下方留下一些空間。這是什麼樣子:黑莓自定義字段drawBitmap不使用整個空間

enter image description here

下面是畫一些代碼:

public int getPreferredWidth(){ 
    return phoneWidth; 
} 

public int getPreferredHeight(){ 
    return cellHeight; 
} 

protected void paint(Graphics g) { 
    Bitmap img = Bitmap.getBitmapResource("cell_bg.png");   
    img.scaleInto(new Bitmap(phoneWidth, cellHeight), Bitmap.SCALE_TO_FIT);//or other scaling methods 
    g.drawBitmap(0, 0, phoneWidth, cellHeight, img, 0, 0);//draw background 
//other steps 
} 

它工作正常,如果我設置Background如下 -

Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("cell_bg.png"),0,0,Background.REPEAT_SCALE_TO_FIT); 
this.setBackground(bg); 

但這種方式,背景圖片不可見onFocus

我在這裏做錯了什麼?

+0

嘗試通過Display.getHeight()和Display.getWidth()方法獲取設備的高度和寬度。 –

+0

變量'phoneWidth'被計算爲Display.getWidth(),所以這不是問題 – tipycalFlow

回答

1

這裏的問題是您錯誤地使用了scaleInto()

試試這樣說:

Bitmap img = Bitmap.getBitmapResource("cell_bg.png"); 
Bitmap scaled = new Bitmap(phoneWidth, cellHeight);   
img.scaleInto(scaled, Bitmap.SCALE_TO_FIT);//or other scaling methods 
g.drawBitmap(0, 0, phoneWidth, cellHeight, scaled, 0, 0);// here draw the scaled image(You here draw the old one) 

scaled位圖圖像是作爲輸出,原始圖像。

+0

+ 1,我認爲這是一個重大問題:)! – tipycalFlow

+0

查看此[問題](http://stackoverflow.com/questions/8325140/bb-autocomplete-events-are-not-being-handled) – tipycalFlow

0

使用此方法來擴展你的代碼的圖像:

private Bitmap getScaledBitmap(String imageName, int width, int height) 
    { 
     try { 
      EncodedImage image = EncodedImage.getEncodedImageResource(imageName); 
      int currentWidthFixed32 = Fixed32.toFP(image.getWidth()); 
      int currentHeightFixed32 = Fixed32.toFP(image.getHeight()); 
      int requiredWidthFixed32 = Fixed32.toFP(width); 
      int requiredHeightFixed32 = Fixed32.toFP(height); 
      int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); 
      int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); 
      image = image.scaleImage32(scaleXFixed32, scaleYFixed32); 
      return image.getBitmap(); 
     } catch(Exception e) { 
      return null; // unable to resize the image 
     }  
    } 
1

創建一個編碼的圖像,然後通過這個編碼的圖像的功能scaleImage()

EncodedImage ei = EncodedImage.getEncodedImageResource("res/helpscreen.png"); 
     EncodedImage ei1= scaleImage(ei,Graphics.getScreenWidth(),Graphics.getScreenHeight()); 


    public EncodedImage scaleImage(EncodedImage source, int requiredWidth, int requiredHeight) 
    { 
     int currentWidthFixed32 = Fixed32.toFP(source.getWidth()); 
     int requiredWidthFixed32 = Fixed32.toFP(requiredWidth); 
     int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); 
     int currentHeightFixed32 = Fixed32.toFP(source.getHeight()); 
     int requiredHeightFixed32 = Fixed32.toFP(requiredHeight); 
     int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); 
     return source.scaleImage32(scaleXFixed32, scaleYFixed32); 
    } 

然後得到這個位圖

BitmapField logoBitmap = new BitmapField(ei1.getBitmap());