我有一个自定义的位图buttonfield,完全可以工作,但是,图像后面的背景显示一个白色的矩形。我发现它使颜色变白,但我无法弄清楚如何使它完全透明。有任何想法吗?我在黑莓的Java编程JDE 5.0如何让我的自定义位图按钮字段使用透明背景?
FYI按钮图像是圆角png文件是在角落使用透明度
代码:
public class BitmapButtonField extends Field
{
Bitmap _currentPicture;
private Bitmap _onPicture;
Bitmap _offPicture;
private int id;
public BitmapButtonField (Bitmap onImage, Bitmap offImage)
{
super(Field.FOCUSABLE|Field.FIELD_HCENTER);
_offPicture = offImage;
_onPicture = onImage;
_currentPicture = _onPicture;
}
public void setButtonImage (Bitmap onImage, Bitmap offImage)
{
_offPicture = offImage;
_onPicture = onImage;
_currentPicture = _onPicture;
}
public void setButtonId(int id)
{
this.id = id;
}
public int getButtonId()
{
return this.id;
}
public int getPreferredHeight()
{
return _onPicture.getHeight();
}
public int getPreferredWidth()
{
return _onPicture.getWidth();
}
protected void onFocus(int direction)
{
_currentPicture = _offPicture;
invalidate();
}
protected void onUnfocus()
{
_currentPicture = _onPicture;
invalidate();
}
protected void drawFocus(Graphics g, boolean on)
{
g.setBackgroundColor(Color.BLACK);
}
protected void layout(int width, int height)
{
setExtent(Math.min(width, getPreferredWidth()), Math.min(
height, getPreferredHeight()));
}
protected void paintBackground(Graphics g) {
int prevColor = g.getColor();
int prevAlpha = g.getGlobalAlpha();
g.setColor(Color.YELLOW);
g.setGlobalAlpha(0);
g.fillRect(0, 0, getWidth(), getHeight()); // or g.getClippingRect()
g.setColor(prevColor);
g.setGlobalAlpha(prevAlpha);
}
protected void paint (Graphics graph){
graph.setColor(Color.WHITE);
//super.paint(graph);
graph.fillRect(0, 0, getWidth(), getHeight());
graph.drawBitmap(0, 0, getWidth(), getHeight(),
_currentPicture, 0, 0);
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
public boolean keyChar(char key, int status, int time)
{
if (key == Characters.ENTER)
{
fieldChangeNotify(0);
return true;
}
return false;
}
}
您已经实现了'protected void drawFocus(Graphics g,boolean on)'和'protected void paintBackground(Graphics g)'。并且您还为指定状态指定了背景图像。你可以删除'paintBackground'和'drawFocus'的实现。另外,将图形颜色设置为白色并填充矩形的行可以从“paint”方法中删除。那就是你只需要在'paint'方法上绘制位图图像。我在这里修改了你的代码,http://pastebin.com/g9n8bqYc。你可以检查(我没有测试它)。 – Rupak 2012-04-23 04:28:24
Rupak工作完美!非常感谢你。我试图在这里给你赞誉,但它不让我:( – 2012-04-23 16:18:04