如何使我的自定义位图按钮字段使用透明背景?

时间:2012-04-22 17:28:42

标签: java blackberry bitmap transparent 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;
    }
}

3 个答案:

答案 0 :(得分:0)

确保paint()方法使用drawARGB在屏幕上绘制位图。我有一个类似的问题,"Scale a Bitmap and preserve alpha, on BlackBerry",结果是drawRGB和drawBitmap没有使用alpha通道,所以不会留下任何透明的东西。

答案 1 :(得分:0)

你正在使用

graph.setColor(Color.WHITE);

 graph.fillRect(0, 0, getWidth(), getHeight());
代码中的

方法(在paint()和paintBackground()方法中)将创建一个白色矩形。

我认为您需要删除此代码。

如果您仍然无法找到问题,那么我将提供另一个自定义BitmapField示例。

答案 2 :(得分:0)

您已实施

protected void drawFocus(Graphics g, boolean on)

protected void paintBackground(Graphics g)

并且您还指定了聚焦状态的背景图像。您可以删除paintBackground()drawFocus()的实施。此外,可以从方法绘制中删除将图形颜色设置为白色并填充矩形的线。那就是你只需要在paint方法上绘制位图图像。我修改了你的代码here,你可以检查一下(我没有测试它)。

相关问题