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

Thursday, July 26, 2012

Gauge Field in Blackberry(Like download status bar)

The following code will display a Download status Bar like Gauge field in Blackberry.




GaugeField gField;
 gField = new CustomGaugeField();
add(gField);
startProgressTimer();

private void startProgressTimer() {
       ttask = new TimerTask() {
            public void run() {
                Field f;
                for (int i = 0; i < getFieldCount(); i++) {
                    f = getField(i);
                    if (f instanceof CustomGaugeField) {
                        final CustomGaugeField gField = (CustomGaugeField) f;
                        final int increment = (i + 1) * 2;
                     
                        UiApplication.getUiApplication().invokeLater(
                            new Runnable() {
                                public void run() {
                                     
                                    if(gField.getValue()>=100)
                                    {
                                    ttask.cancel();
                                    //Implement your action here
                                    }
                                    else{
                                    gField.setValue((gField.getValue() + increment) % 101);
                                    }
                                   
                                }
                            }
                        );
                    }
                       
                }

            }
        };

        Timer ttimer = new Timer();
        ttimer.schedule(ttask, 1000, 300);
    }

//custom Gauge Field Class

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.GaugeField;

class CustomGaugeField extends GaugeField {
    // Default constructor, need improvement
    public CustomGaugeField() {
        super("", 0, 100, 0, GaugeField.PERCENT);
    }

    // Colors
    private static final int BG_COLOR = 0xd6d7d6;
    private static final int BAR_COLOR = 0x63cb52;
    private static final int FONT_COLOR = 0x5a55c6;

    protected void paint(Graphics graphics) {
        int xProgress = (int) ((getWidth() / 100.0) * getValue());
        int xProgressInv = getWidth() - xProgress;

        // draw background
        graphics.setBackgroundColor(BG_COLOR);
        graphics.clear();

        // draw progress bar
        graphics.setColor(BAR_COLOR);
      
        graphics.fillRect(0, 0, xProgress, getHeight());//left to right
        //graphics.fillRect(xProgressInv, 0, xProgress, getHeight());//right to left

        // draw progress indicator text
        String text = getValue() + "%";
        Font font = graphics.getFont();
        int xText = (getWidth() - font.getAdvance(text)) / 2;
        int yText = (getHeight() - font.getHeight()) / 2;
        graphics.setColor(FONT_COLOR);
        graphics.drawText(text, xText, yText);
    }
   
    protected void layout(int width, int height) {
        super.layout(width, height);
        setExtent (width, 15);
        }
}


        

No comments:

Post a Comment