Monday 1 October 2012

BlackBerry Custom Play/Pause Image Button


This Tutorial Lets you to Create a BlackBerry Custom Play/Pause Image Button on BlackBerry Screen.When this Application gets Executed. Initially, We  Displayed With a Play Image Button on Screen.After we click on Play Image Button,it will be Changed to Pause Image Button.The Remaining Business Logic will be written in  navigationClick(int status, int time) method.

Here The Sample Code:


import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.Bitmap;


public class CustomImageButtonApp extends UiApplication
{
    static Bitmap image;
 
    public static void main(String[] args)
    {
        CustomImageButtonApp app = new CustomImageButtonApp ();
        app.enterEventDispatcher();
    }
    public CustomImageButtonApp ()
    {
     final imagebutton img_btn = new imagebutton("",Field.FOCUSABLE, "play.png",
     "pause.png", 0x9cbe95);
        MainScreen screen=new MainScreen();
         pushScreen(screen);
         screen.setTitle("title");
         screen.add(img_btn);
     }
 
 
 public class CustomImageButton extends Field  {
     
        private String _label;
        private int _labelHeight;
        private int _labelWidth;
        private Font _font;   
        private Bitmap _currentPicture;
        private Bitmap _playPicture;
        private Bitmap _pausePicture;
        int color;
 
     
        public  CustomImageButton (String text, long style ,String playimg, String pauseimg, int color){
               super(style);
         
            _playPicture= Bitmap.getBitmapResource( playimg );
            _pausePicture=Bitmap.getBitmapResource( pauseimg );
         
            _font = getFont();
            _label = text;
            _labelHeight = _playPicture.getHeight();
            _labelWidth = _pausePicture.getWidth();
         
            this.color = color;
         
            _currentPicture = _playPicture;
        }
   

        /**
         * @return The text on the button
         */
        String getText(){
            return _label;
        }
     
   
       /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#getPreferredHeight()
         */
        public int getPreferredHeight(){
            return _labelHeight;
        }
     
         /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#getPreferredWidth()
         */
        public int getPreferredWidth(){
            return _labelWidth;
        }
     
             
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
         */
        protected void drawFocus(Graphics graphics, boolean on) {
        // Do nothing
        }
     
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#layout(int, int)
         */
        protected void layout(int width, int height) {
            setExtent(Math.min( width, getPreferredWidth()),
            Math.min( height, getPreferredHeight()));
        }
        /**
         * Field implementation.
         * @see net.rim.device.api.ui.Field#paint(Graphics)
         */
        protected void paint(Graphics graphics){    
            // First draw the background colour and picture
            graphics.setColor(this.color);
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
         
            // Then draw the text
            graphics.setColor(Color.BLACK);
            graphics.setFont(_font);
            graphics.drawText(_label, 4, 2,
                (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
                getWidth() - 6 );
        }
         
        /**
         * Overridden so that the Event Dispatch thread can catch this event
         * instead of having it be caught here..
         * @see net.rim.device.api.ui.Field#navigationClick(int, int)
         */
        protected boolean navigationClick(int status, int time){
         
        if (_currentPicture ==  _playPicture) {
               _currentPicture =  _pausePicture;
              // invalidate();

           } else {
               _currentPicture = _playPicture;
               //invalidate();

           }

           //fieldChangeNotify(1);
           return true;
     
    }
    }
}

No comments:

Post a Comment