2011-12-05 6 views
1

私はカスタムフィールドの背景としてBitmapを描画しますが、フィールド全体の領域にビットマップを描画しません。私もイメージのサイズを変更しましたが、それは常に右と下にいくつかのスペースを残します。これは、次のようになります。Blackberryカスタムフィールド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 
} 

私はfollows-

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

が、この方法としてBackgroundを設定した場合、それは正常に動作します、背景画像は表示されません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-autocompletefield-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()); 
関連する問題