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

发布于 2024-11-09 11:15:26 字数 1305 浏览 7 评论 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;
   }

}

I’m creating a custom field to make a image button. The image is drawn as a box with the w and h equal, but the image's height is almost double the width. I ran the debugger and the correct w,h are being put in g.drawBitmap(0, 0, w, h, image, 0, 0). Is thee a way to fix this?

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;
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-11-16 11:15:26

复制并粘贴可能会让您陷入困境。 setExtent 的第二个参数应该调用 getPreferredHeight():

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

Copy and paste probably did you in. Second argument to setExtent should call getPreferredHeight():

  setExtent(Math.min(width, getPreferredWidth()), 
        Math.min(height, getPreferredWidth()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文