2011-05-20 52 views
0

我正在創建一個自定義字段來製作一個圖像按鈕。圖像被繪製成一個w和h相等的盒子,但圖像的高度幾乎是寬度的兩倍。我運行了調試器,正確的w,h正在輸入g.drawBitmap(0, 0, w, h, image, 0, 0)。你是解決這個問題的方法嗎?爲什麼繪製時我的矩形位圖會被裁剪爲正方形?

public class cPictureButton extends Field{ 

    private Bitmap image; 

    public cPictureButton(Bitmap image, long style) 
    { 
     super(style); 

     this.image=image; 
    } 

    public int getPreferredHeight() 
    { 
     return image.getHeight(); 
     // return getFont().getHeight(); 
    } 

    public int getPreferredWidth() 
    { 
     return image.getWidth(); 
     // return getFont().getAdvance(label)+8; 
    } 

    protected void drawFocus(Graphics g, boolean on) 
    { 
    } 


    protected void paint(Graphics g) 
    { 
     int w=image.getWidth(); 
     int h=image.getHeight(); 
     g.drawBitmap(0, 0, w, h, image, 0, 0); 
     if (isFocus()) 
      g.drawRect(0,0,image.getWidth(), image.getHeight()); 
    } 

    protected void layout(int width, int height) { 
     setExtent(Math.min(width, getPreferredWidth()), 
       Math.min(height, getPreferredWidth())); 
    } 


    public boolean isFocusable() { 
     return true; 
    } 
    protected boolean navigationClick(int status, int time) 
    { 
     fieldChangeNotify(0); 
     return true; 
    } 

} 
+0

什麼是你getExtent()返回時,漆叫什麼名字? – Swati 2011-05-20 20:29:55

回答

2

複製並粘貼可能沒有你的第二個引setExtent應該調用getPreferredHeight():

setExtent(Math.min(width, getPreferredWidth()), 
     Math.min(height, getPreferredWidth())) 
相關問題