我有一個自定義的位圖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