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) {

    }
}

4 thoughts on “Blackberry Bitmap Button

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s