List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Monday, April 16, 2012

Custom Image Button in Blackberry



  1. final imagebutton img_btn = new imagebutton("",Field.FOCUSABLE, "normal_image.png","mouse_over_image.png", 0x9cbe95);


    //Image Button Class

    import net.rim.device.api.ui.*;
  2. import net.rim.device.api.system.*;
  3. public class imagebutton extends Field {
  4.        
  5.     private String _label;
  6.     private int _labelHeight;
  7.     private int _labelWidth;
  8.     private Font _font;
  9.    
  10.     private Bitmap _currentPicture;
  11.     private Bitmap _onPicture;
  12.     private Bitmap _offPicture;
  13.     int color;
  14.    
  15.     public imagebutton(String text, long style ,String img, String img_hvr, int color){
  16.         super(style);
  17.        
  18.         _offPicture = Bitmap.getBitmapResource(img);
  19.         _onPicture = Bitmap.getBitmapResource(img_hvr);
  20.        
  21.         _font = getFont();
  22.         _label = text;
  23.         _labelHeight = _onPicture.getHeight();  
  24.         _labelWidth = _onPicture.getWidth();
  25.        
  26.         this.color = color;
  27.        
  28.         _currentPicture = _offPicture;
  29.     }
  30.    
  31.     /**
  32.      * @return The text on the button
  33.      */
  34.     String getText(){
  35.         return _label;
  36.     }
  37.    
  38.     /**
  39.      * Field implementation.
  40.      * @see net.rim.device.api.ui.Field#getPreferredHeight()
  41.      */
  42.     public int getPreferredHeight(){
  43.         return _labelHeight;
  44.     }
  45.     /**
  46.      * Field implementation.
  47.      * @see net.rim.device.api.ui.Field#getPreferredWidth()
  48.      */
  49.     public int getPreferredWidth(){
  50.         return _labelWidth;
  51.     }
  52.    
  53.     /**
  54.      * Field implementation.  Changes the picture when focus is gained.
  55.      * @see net.rim.device.api.ui.Field#onFocus(int)
  56.      */
  57.     protected void onFocus(int direction) {
  58.         _currentPicture = _onPicture;
  59.         invalidate();
  60.     }
  61.     /**
  62.      * Field implementation.  Changes picture back when focus is lost.
  63.      * @see net.rim.device.api.ui.Field#onUnfocus()
  64.      */
  65.     protected void onUnfocus() {
  66.         _currentPicture = _offPicture;
  67.         invalidate();
  68.     }
  69.    
  70.     /**
  71.      * Field implementation.  
  72.      * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
  73.      */
  74.     protected void drawFocus(Graphics graphics, boolean on) {
  75.         // Do nothing
  76.     }
  77.    
  78.     /**
  79.      * Field implementation.
  80.      * @see net.rim.device.api.ui.Field#layout(int, int)
  81.      */
  82.     protected void layout(int width, int height) {
  83.         setExtent(Math.min( width, getPreferredWidth()),
  84.         Math.min( height, getPreferredHeight()));
  85.     }
  86.     /**
  87.      * Field implementation.
  88.      * @see net.rim.device.api.ui.Field#paint(Graphics)
  89.      */
  90.     protected void paint(Graphics graphics){      
  91.         // First draw the background colour and picture
  92.         graphics.setColor(this.color);
  93.         graphics.fillRect(0, 0, getWidth(), getHeight());
  94.         graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
  95.        
  96.         // Then draw the text
  97.         graphics.setColor(Color.BLACK);
  98.         graphics.setFont(_font);
  99.         graphics.drawText(_label, 4, 2,
  100.             (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
  101.             getWidth() - 6 );
  102.     }
  103.        
  104.     /**
  105.      * Overridden so that the Event Dispatch thread can catch this event
  106.      * instead of having it be caught here..
  107.      * @see net.rim.device.api.ui.Field#navigationClick(int, int)
  108.      */
  109.     protected boolean navigationClick(int status, int time){
  110.         fieldChangeNotify(1);
  111.         return true;
  112.     }
  113.    
  114. }






        

No comments:

Post a Comment