黑莓,创建了一个自定义按钮,并希望图像在draWN后更改图像

时间:2011-05-23 03:52:31

标签: blackberry button java-me

我为BlackBerry项目创建了一个自定义按钮字段。我想在绘制后更改按钮上显示的图片,但无法弄清楚如何。我有代码更改存储位图的成员变量,但不知道如何告诉黑莓更新它。

// CODE TO CHANGE BUTTONS Image
Bitmap image2 = Bitmap.getBitmapResource("aftera.png");
MyBut.image=image2;
// don’t know how to redraw buttn?????

// BUTTON CODE
public class cPictureButton extends Field{

   public 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) {
    // TODO Auto-generated method stub
    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 :(得分:1)

在你的按钮字段上调用invalidate()应该处理它。

此外,您仍然在我之前的问题中指出的layout()方法中存在错误:Why does my rectangular bitmap get clipped to a square when I draw it?