黑莓图像按钮

时间:2010-10-23 07:47:04

标签: java blackberry blackberry-simulator

我想创建六个类似于主屏幕上的图像按钮,例如设置,下载或消息。当我滚动到下一个按钮时,前一个按钮必须褪色,就像模拟器一样。我正在使用9700模拟器和JDE 5.0.0。

1 个答案:

答案 0 :(得分:0)

请使用此课程制作自定义图片按钮并添加到屏幕中:

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;


public class BitmapButtonField extends ButtonField {
    private Bitmap bitmap;
    private Bitmap bitmapHighlight;
    private boolean highlighted = false;

    /**
     * Instantiates a new bitmap button field.
     * 
     * @param bitmap the bitmap to use as a label
     */
    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight) {
        this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER);
    }

    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight, long style) {
        super(style);
        this.bitmap = bitmap;
        this.bitmapHighlight = bitmapHighlight;
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.component.ButtonField#layout(int, int)
     */
    protected void layout(int width, int height) {
            setExtent(getPreferredWidth(), getPreferredHeight());
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.component.ButtonField#getPreferredWidth()
     */
    public int getPreferredWidth() {
            return bitmap.getWidth();
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.component.ButtonField#getPreferredHeight()
     */
    public int getPreferredHeight() {
            return bitmap.getHeight();
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.ui.Graphics)
     */
    protected void paint(Graphics graphics) {
            super.paint(graphics);
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Bitmap b = bitmap;
            if (highlighted)
                b = bitmapHighlight;
            graphics.drawBitmap(0, 0, width, height, b, 0, 0);
    }

    public void setHighlight(boolean highlight)
    {
        this.highlighted = highlight;           
    }
}