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

发布于 2024-11-08 21:13:18 字数 1369 浏览 0 评论 0原文

我为我的 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;
}

}

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


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

}

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

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

发布评论

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

评论(1

各空 2024-11-15 21:13:18

在按钮字段上调用 ​​invalidate() 应该可以处理它。

另外,您的布局()方法中仍然存在我在之前的问题中指出的错误: 为什么当我绘制矩形位图时,它会被剪切成正方形?

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?

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