Why does my rectangular bitmap get clipped to a square when I draw it?

守給你的承諾、 提交于 2019-12-11 15:29:25

问题


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

}

回答1:


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

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


来源:https://stackoverflow.com/questions/6077107/why-does-my-rectangular-bitmap-get-clipped-to-a-square-when-i-draw-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!