Blackberry Bitmap Button

custom button with bitmap/image decorator

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

public class BitmapButtonField extends ButtonField {
	private Bitmap bmpNormal;
	private Bitmap bmpFocused;
	private int width;
	private int height;

	public BitmapButtonField(Bitmap normal, Bitmap focused) {
        super(CONSUME_CLICK);
        bmpNormal = normal;
        bmpFocused = focused;
        width = bmpNormal.getWidth();
        height = bmpNormal.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)));
    }

    protected void paint(Graphics graphics) {
        Bitmap bitmap = null;
        switch (getVisualState()) {
        case VISUAL_STATE_NORMAL:
                bitmap = bmpNormal;
                break;
        case VISUAL_STATE_FOCUS:
                bitmap = bmpFocused;
                break;
        case VISUAL_STATE_ACTIVE:
                bitmap = bmpFocused;
                break;
        default:
                bitmap = bmpNormal;
        }

        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),bitmap, 0, 0);
    }

    public int getPreferredWidth() {
        return width;
    }

    public int getPreferredHeight() {
        return height;
    }

    protected void layout(int width, int height) {
        setExtent(this.width, this.height);
    }

    protected void applyTheme(Graphics g, boolean arg1) {

    }
}

taken from: jampasir’s blog

3 Responses to Blackberry Bitmap Button

  1. Amri Shodiq says:

    Saya juga bikin code yang tujuannya sama dengan ini. Tapi caranya beda. Cekidot di sini gan:
    ImageButton.java

  2. Waa, ternyata ada BorderFactory Mas. Gak jalan deh untuk 4.3.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: