黑莓,创建了一个自定义按钮,并希望图像在绘制后更改图像
我为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在按钮字段上调用 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?