custom_buttonfield模糊不清

时间:2012-07-19 02:55:35

标签: layout blackberry custom-fields

This是原始按钮图片。按钮的背景是透明的。

应用于应用时,按钮看起来像this。请看左上方的按钮。按钮的背景变为灰色而不是透明。

Here是Android版的按钮。

不仅是按钮,还有背景透明的所有相同类型的按钮。

custom_buttonfield

public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;

int mWidth;
int mHeight;

private int color = -1;
String text;

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color) {
    super(CONSUME_CLICK | Field.FOCUSABLE | Field.FIELD_HCENTER
            | Field.FIELD_VCENTER);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

protected void onFocus(int direction) {
    super.onFocus(direction);
}

protected void onUnfocus() {
    super.onUnfocus();
}

protected void paint(Graphics graphics) {   
    int fontcontent;
    if (Display.getWidth() > 480)
        fontcontent = 28;
    else if (Display.getWidth() < 481 && Display.getWidth() > 320)
        fontcontent = 23;
    else
        fontcontent = 18;

    Bitmap bitmap = null;
    switch (getVisualState()) {
    case VISUAL_STATE_NORMAL:
        bitmap = mNormal;
        break;
    case VISUAL_STATE_FOCUS:
        bitmap = mFocused;
        break;
    case VISUAL_STATE_ACTIVE:
        bitmap = mActive;
        break;
    default:
        bitmap = mNormal;
    }
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
            bitmap, 0, 0);
    graphics.setFont(Font.getDefault().derive(Font.BOLD, fontcontent));
    graphics.setColor(color);
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault()
            .getAdvance(text)) / 2, ((mNormal.getHeight() - Font
            .getDefault().getHeight()) / 2) + 10, DrawStyle.HCENTER
            | DrawStyle.VCENTER);
}

public int getPreferredWidth() {
    return mWidth;
}

public int getPreferredHeight() {
    return mHeight;
}

protected void layout(int width, int height) {
    setExtent(mWidth, mHeight);
}
}

装载机在这里

private Bitmap news = Config_GlobalFunction.Bitmap("icon_news.png");
private Bitmap newsactive = Config_GlobalFunction
        .Bitmap("icon_news_active.png");

if (left == 1) {
        newsbtn = new Custom_ButtonField(news, newsactive, newsactive) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().pushScreen(
                        new Menu_PopupMenu(thisid));
                return true;
            }
        };

        add(newsbtn);
    } else if (left == 2) {
        backbtn = new Custom_ButtonField(back, backctive, backctive) {
            protected boolean navigationClick(int status, int time) {
                Main.getUiApplication().popScreen(mainscreen);
                return true;
            }
        };
        add(backbtn);
    }

if (left == 1) {
        field = getField(1);
        layoutChild(field, back.getWidth(), back.getHeight());
        setPositionChild(field, 10, Height);
    } else if (left == 2) {
        field = getField(1);
        layoutChild(field, news.getWidth(), news.getHeight());
        setPositionChild(field, 10, Height);
    }

如果我像这样设置layoutChild(field, 60, 60);,那么它就没有问题,灰色的背后也没有了。但是,我无法设置固定且必须动态的大小。

2 个答案:

答案 0 :(得分:1)

setBackground(BackgroundFactory.createBitmapBackground(bitmap)); 中调用此graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);而不是Custom_ButtonField

答案 1 :(得分:0)

我看不到加载Bitmap对象的实际代码,但我知道您在发布的其他问题中正在讨论它。试试这个:

Config_GlobalFunction.java:

public static Bitmap Bitmap(String name) {
    Bitmap result;

    // do whatever you do to load the Bitmap (e.g. Bitmap.getBitmapResource(name))

    result.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
    return result;
}