为什么我的矩形位图在绘制时会被剪裁成正方形?

时间:2011-05-20 20:10:19

标签: blackberry bitmap field

我正在创建一个自定义字段来制作图像按钮。图像绘制为一个框,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;
   }

}

1 个答案:

答案 0 :(得分:2)

复制和粘贴可能是你进去的.setExtent的第二个参数应该调用getPreferredHeight():

  setExtent(Math.min(width, getPreferredWidth()), 
        Math.min(height, getPreferredWidth()))