开发者

Blackberry, created a custom button and would like image to change image after it is draWN

I have created a custom button field for my BlackBerry project. I would like to change the picture displayed on the button after it is drawn, but cannot figure out how. I have code that changes the member variable that stores the bitmap, but do not know how to tell the blackberry to update it.

// 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)
   {
   }


   protec开发者_开发技巧ted 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;
}

}


Calling invalidate() on your button field should take care of it.

Also, you still have the bug in your layout() method that I pointed out in your earlier question: Why does my rectangular bitmap get clipped to a square when I draw it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜