本文介绍了黑莓手机 - 与中心位图ButtonField字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从ButtonField字段扩展一个类:

I have a class that extends from ButtonField :

class BitmapButtonField extends ButtonField
{
    private Bitmap _bitmap;
    private int _buttonWidth;
    private int _buttonHeight;

    BitmapButtonField(Bitmap bitmap, int buttonWidth, int buttonHeight, long style)
    {
        super(style);
        _buttonWidth = buttonWidth;
        _buttonHeight = buttonHeight;
        _bitmap = bitmap;
    }

    public int getPreferredHeight()
    {
        return _buttonHeight;
    }

    public int getPreferredWidth()
    {
        return _buttonWidth;
    }

    protected void layout(int width, int height)
    {
        setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight()));
    }

    protected void paint(Graphics graphics)
    {
        // THIS IS NOT CENTERED
        int x = (getPreferredWidth() - _bitmap.getWidth()) >> 1;
        graphics.drawBitmap(x, 0, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);

        // THIS IS NOT LEFTMOST, TOPMOST
        graphics.drawBitmap(0, 0, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
    }
}

如果你可以从我的paint方法的意见看,显然ButtonField字段0,0位置是不完全在最左边和按钮的最上面的一角,在某种程度上它填补未知偏移,所以这使得中心的形象是很难

If you could see from my comments on the paint method, clearly the ButtonField 0,0 position is not exactly in leftmost and topmost corner of the button, somehow it is padded with unknown offset, so this makes centering the image is difficult.

但是,如果我从一个Field类扩展,问题消失了,但我需要保持从ButtonField字段延长,因为我需要ButtonField字段边框和焦点风格(蓝白色的圆角矩形),我只需要显示在居中的图像一个ButtonField字段,并且仍然保留了所有的标准ButtonField字段属性。

But if I extend from a Field class, the problem goes away, but I need to keep extending from ButtonField since I need the ButtonField border and focus style (blue-white rounded rectangle), I just need to display centered image on a ButtonField, and still retaining all of the standard ButtonField attributes.

有没有办法消除软垫上ButtonField字段在paint方法弥补的方法吗?我试过setPadding和setMargin,但没有这样的运气。非常感谢!

Is there a way to eliminate the padded offset in paint method on a ButtonField? I've tried setPadding and setMargin but no such luck. Thanks a lot!

推荐答案

在paint()来定义x对于图像偏移有一个code:

In paint() to define x offset for image there is a code:

int x = (getPreferredWidth() - _bitmap.getWidth()) >> 1;

这是确定的,还是按钮可以有其他的大小,因为:

It is ok, still button can have other size, because of:

protected void layout(int width, int height)
{
    setExtent(Math.min( width, getPreferredWidth()),
       Math.min( height, getPreferredHeight()));
}

尝试使用

protected void layout(int width, int height)
{
    setExtent(getPreferredWidth(), getPreferredHeight());
}

这篇关于黑莓手机 - 与中心位图ButtonField字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:45