Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
rahulkrishna081980
Posts: 18
Registered: ‎11-16-2011
My Device: 9900

carousel image menu in blackberry

Hi All,

 

How to create the carousel image menu in blackberry.

Please provide me some link or code, how to implement that.

 

 

Thanks

Rahul

 

Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: carousel image menu in blackberry

Did you want a spiral control or did you want a linear control like this one? I have code that is similar to this as well but does not overlap the images and doesn't alternate left to right in the order.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: carousel image menu in blackberry

[ Edited ]

If anyone is curious about the code for this field, here it is:

 

//#preprocess

/*
** To change this template, choose Tools | Templates
** and open the template in the editor.
*/

package CM.api.ui.blackberry.component;

import CM.api.ui.blackberry.MyField;
import CM.api.ui.blackberry.ToolTip;
import CM.api.ui.blackberry.CustomLayoutManager;
import CM.api.ui.blackberry.CustomFieldChangeListener;
import java.io.InputStream;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.TouchGesture;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.math.Fixed32;

import java.util.Vector;
import java.util.Timer;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.ui.UiApplication;

/**
***
*** @author Christopher Myers
*** Copyright (c) 2009 Backroom Software
**/
public class CustomFilmstripField extends Field implements MyField {

   //
   public static final int VERTICAL_STRIP   = 0;
   public static final int HORIZONTAL_STRIP = 10;

   private int stripColor = 0x0;
   private int cellColor = 0xCCCCCC;
   private int cellGradient = 0x333333;
   private int cellFocusColor = 0x4466FF;
   private int cellFocusGradient = 0x333333;
   private int fontColor = 0xFFFFFF;
   //private int bgAlpha = 255, fgAlpha = 255;
   private int currentCell, maxCells, usedCells, selectedCell = 0;
   private int width, height, cellWidth, cellHeight, xPos, yPos;
   int xoffset = 0, yoffset = 0, xoffset2 = 0, yoffset2 = 0;
   private int curx, prevx, cury, prevy;
   private int fontSize = -1;

   private int cellDirection = HORIZONTAL_STRIP;
   
   /********/
   int displayable;
   int count;
   int [] cell;
   int [] stripx, stripy;
   int cellx [];
   int celly [];
   int color [];
   int paths [];
   int startx;
   int starty;
   
   Bitmap [] normal;
   Bitmap [] selected;
   /********/

   boolean useGradient      = false;
   boolean showSelectedCell = false;
   boolean hasFocus         = false;
   boolean changeOnScroll   = false;
   boolean continuousScroll = true;
   boolean showItemInfo     = false;
   boolean paintingPrepped  = false;
   
   private String message;

   Font defaultFont = null;
   ToolTip tip;
   Vector cells;
   static Timer timer = null;
   InertialScroller inertial;

   /*
    *
    * @param tabs number of tabs that the field can hold
    * @param x x-position of tab field
    * @param y y-position of tab field
    * @param width width of tab field
    * @param height height of tab field
    * @param cellW width of individual tabs
    * @param cellH height of individual tabs
    */
   public CustomFilmstripField(int cells, int x, int y, int width, int height, int cellW, int cellH){
      message = "";
      maxCells = cells;
      this.cells = new Vector();
      xPos = x;
      yPos = y;
      this.width = width;
      this.height = height;
      cellWidth = cellW;  
      cellHeight = cellH; 
   }
      
   public void setNumberCells(int n){ maxCells = n; }
   public int getNumberCells(){ return maxCells; }
   public int getUsedCells(){ return usedCells; }
   
   public int getSelectedCell(){ return selectedCell; }
   public void setSelectedCell(int n){ selectedCell = n; }
   
   public int getCurrentCell(){ return currentCell; }
   public void setCurrentCell(int n){
      currentCell = n;
      if( cellDirection == HORIZONTAL_STRIP ){
         xoffset = (20 * currentCell) + (cellWidth * currentCell); //(20*n)+(cellWidth*n)
         xoffset *= -1;
      }
   }

   public void setShowSelectedCell(boolean b){ showSelectedCell = b; }
   public void setChangeOnScroll(boolean b){ changeOnScroll = b; }
   public void setContinuousScroll(boolean b){ continuousScroll = b; }
   public void showItemInfo(boolean b){ showItemInfo = b; }

   public void addCell(String msg,String img,boolean resource){
      if( cells.size() < maxCells ){
         CellInfo newcell = new CellInfo(msg,img,resource);
         cells.addElement(newcell);
         usedCells++;
      }
      paintingPrepped = false;
   }
   
   public void removeCell(int i){
      cells.removeElementAt(i);
      usedCells--;
      paintingPrepped = false;
   }

   public void setForegroundAlpha(int a){ }
   public void setBackgroundAlpha(int a){ }

   public void setStripColor(int c){ stripColor = c; }
   
   public void setFocusColor(int c){ cellFocusColor = c; }
   public void setFocusGradient(int c){ cellFocusGradient = c; useGradient = true; }

   public void setColor(int c){ cellColor = c; }
   public void setGradient(int c){ cellGradient = c; useGradient = true; }

   public void setFontColor(int c){ fontColor = c; }
   public void setFontSize(int s){ fontSize = s; }

   public void setDirection(int n){ cellDirection = n; paintingPrepped = false; }

   /**
    * Changes the color of the button back to the default color when it loses focus
    * and forces the button to redraw.
    */
   protected void onUnfocus(){
      hasFocus = false;
      if( this.getFocusListener() != null ) getFocusListener().focusChanged(this, FOCUS_LOST);
   }

   /**
    * Changes the color of the button if a focus color has been set  and forces the
    * button to redraw.
    *
    * @param direction not used
    */
   protected void onFocus(int direction){
      hasFocus = true;
      if( this.getFocusListener() != null ) getFocusListener().focusChanged(this, FOCUS_GAINED);
   }

   public void setFocus() {
      hasFocus = true;
      super.setFocus();
   }

   /**
    * Sets the layout/extent  of this button.
    *
    * @param w not used
    * @param h not used
    */
   protected void layout(int w, int h){
      setExtent(myWidth(),myHeight());
      if( (myWidth()-cellWidth)<20 ) cellWidth = myWidth()-20;
      if( (myHeight()-cellHeight)<20 ) cellHeight = myHeight()-20;
      paintingPrepped = false;
   }

   //Prefill the arrays so do not have to calculate each time
   private void prepPainting(){
      displayable = myWidth()/(cellWidth+20);
      count = displayable;
      if( count > usedCells ){
         int n = 2;
         int size = (usedCells*n);
         while( size < count ){
            n++;
            size = (usedCells*n);
         }
         count = size;
      } else {
         count = usedCells;
      }
      
      cell = new int[count];
      for( int n=0; n<cell.length; ++n ){
         cell[n] = n%usedCells;
      }
      
      stripx = new int [4]; stripx[0] = stripx[3] = 0; stripx[1] = stripx[2] = myWidth(); 
      stripy = new int [4]; stripy[0] = stripy[1] = 0; stripy[2] = stripy[3] = myHeight();
      
      cellx = new int[count*4];
      celly = new int[count*4];
      color = new int[count*4];
      paths = new int[count+1];
      for( int n=0; n<paths.length; ++n ) paths[n] = n*4;
      
      starty = (myHeight()-cellHeight)>>>1;
      for( int n=0; n<count; n++ ){      
         celly[4*n] = starty;
         celly[(4*n)+1] = starty;
         celly[(4*n)+2] = starty+cellHeight;
         celly[(4*n)+3] = starty+cellHeight;
      }
      
      normal = null; selected = null;
      normal = new Bitmap[usedCells];
      selected = new Bitmap[usedCells];
      
      paintingPrepped = true;
   }
   
   public void paint(Graphics g){
      if( !paintingPrepped ) prepPainting();
      
      if( hasFocus ){ drawFocus(g,true); return; }      

      paintHorizontal(g);      
   }

   public void drawFocus(Graphics g, boolean on){
      drawFocusHorizontal(g,on);      
   }
   
   public void drawFocusHorizontal(Graphics g, boolean on){
      System.out.println("draw focus called on filmstrip field");
      g.setGlobalAlpha(255);
            
      int w = myWidth();
      int h = myHeight();
      
      g.setColor(stripColor);
      g.drawFilledPath(stripx, stripy, null, null);
      g.setColor(cellColor);
      g.drawLine(0, 0, w, 0);
      g.drawLine(0, h-1, w, h-1);

      for( int n=0; n<color.length; ++n ) color[n] = cellColor;

      startx = xoffset+((w>>>1)-(cellWidth>>>1));
      
      if( (startx+(count*(cellWidth+20))) <= 0 ){
         startx = (startx+(count*(cellWidth+20)));
         xoffset = startx-((w>>>1)-(cellWidth>>>1));
      } else if( startx >= w ){
         startx = (startx-(count*(cellWidth+20)));
         xoffset = startx-((w>>>1)-(cellWidth>>>1));
      }

      int left = (w>>>1)-(cellWidth>>>1)-15;
      int right = (w>>>1)+(cellWidth>>>1)+15;
      int center = (w>>>1);

      for( int n=0; n<count; ++n ){
         cellx[4*n]     = startx+(20*n)+(cellWidth*n);
         cellx[(4*n)+1] = startx+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+2] = startx+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+3] = startx+(20*n)+(cellWidth*n);

         celly[4*n]     = starty;
         celly[(4*n)+1] = starty;
         celly[(4*n)+2] = starty+cellHeight;
         celly[(4*n)+3] = starty+cellHeight;

         //if( ((cellx[4*n] >= left) && (cellx[(4*n)+1] <= right)) ){
         if( (cellx[4*n] <= center) && (cellx[(4*n)+1] >= center) ){
            boolean cellChanged = false;
            if( currentCell != cell[n] ) cellChanged = true;
            System.out.println("Current: "+currentCell+" Cell: "+cell[n]+" Changed: "+cellChanged);
            currentCell = cell[n];
            boolean scrolling = false;
            if( inertial != null ){
               scrolling = inertial.alive;
               if( !scrolling ){
                  cellChanged = true;
                  inertial = null;
                  System.out.println("Intertial scroller stopped but not null ... calling fieldChanged");
               }
            }            
            if( changeOnScroll && cellChanged && !scrolling ){
               selectedCell = cell[n];
               if( this.getChangeListener() != null ) this.getChangeListener().fieldChanged(this, -1);
            }
         }
      }

      for( int n=0; n<count; ++n ){
         if( cell[n] == selectedCell ){
            if( showSelectedCell ){
               color[4*n]     = cellColor;
               color[(4*n)+1] = cellColor;
               color[(4*n)+2] = cellGradient;
               color[(4*n)+3] = cellGradient;
            }
         }

         if( cell[n] == currentCell ){
            cellx[4*n]-=10;       celly[4*n]-=10;
            cellx[(4*n)+1]+=10;   celly[(4*n)+1]-=10;
            cellx[(4*n)+2]+=10;   celly[(4*n)+2]+=10;
            cellx[(4*n)+3]-=10;   celly[(4*n)+3]+=10;

            color[4*n]     = cellFocusColor;
            color[(4*n)+1] = cellFocusColor;
            color[(4*n)+2] = cellFocusGradient;
            color[(4*n)+3] = cellFocusGradient;
         }
      }

      g.drawShadedFilledPath(cellx, celly, null, color, paths);

      Bitmap image = null;
      for( int n=0; n<count; ++n ){
         CellInfo ci = (CellInfo)cells.elementAt(n%usedCells);
         int imx;// = (cellWidth - ci.image.getWidth())>>>1;
         int imy;// = (cellHeight - ci.image.getHeight())>>>1;
         //image = ci.image.getBitmap();
         if( (cell[n] == currentCell) ){
            if( selected[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth+20));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight+20));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               selected[cell[n]] = image;
            } else
               image = selected[cell[n]];
            imx = ((cellWidth+20) - image.getWidth())>>>1;
            imy = ((cellHeight+20) - image.getHeight())>>>1;            
         } 
         else {
            if( normal[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               normal[cell[n]] = image;
            } else
               image = normal[cell[n]];
            imx = ((cellWidth) - image.getWidth())>>>1;
            imy = ((cellHeight) - image.getHeight())>>>1;            
         } 
         g.drawBitmap(imx+cellx[4*n], imy+celly[4*n], image.getWidth(), image.getHeight(), image, 0, 0);
      }

      if( startx > 0 ){
         xoffset2 = startx-(count*(cellWidth+20));
      } else {
         xoffset2 = startx+(count*(cellWidth+20));
      }

      for( int n=0; n<color.length; ++n ) color[n] = cellColor;

      for( int n=0; n<count; ++n ){
         cellx[4*n]     = xoffset2+(20*n)+(cellWidth*n);
         cellx[(4*n)+1] = xoffset2+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+2] = xoffset2+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+3] = xoffset2+(20*n)+(cellWidth*n);

         celly[4*n]     = starty;
         celly[(4*n)+1] = starty;
         celly[(4*n)+2] = starty+cellHeight;
         celly[(4*n)+3] = starty+cellHeight;

         //if( ((cellx[4*n] >= left) && (cellx[(4*n)+1] <= right)) ){
         if( (cellx[4*n] <= center) && (cellx[(4*n)+1] >= center) ){   
            boolean cellChanged = false;
            if( currentCell != cell[n] ) cellChanged = true;
            System.out.println("Current: "+currentCell+" Cell: "+cell[n]+" Changed: "+cellChanged);
            currentCell = cell[n];
            boolean scrolling = false;
            if( inertial != null ){
               scrolling = inertial.alive;
               if( !scrolling ){
                  cellChanged = true;
                  inertial = null;
                  System.out.println("Intertial scroller stopped but not null ... calling fieldChanged");
               }
            }            
            if( changeOnScroll && cellChanged && !scrolling ){
               selectedCell = cell[n];
               if( this.getChangeListener() != null ) this.getChangeListener().fieldChanged(this, -1);
            }
         }
      }

      for( int n=0; n<count; ++n ){
         if( cell[n] == selectedCell ){
            if( showSelectedCell ){
               color[4*n]     = cellColor;
               color[(4*n)+1] = cellColor;
               color[(4*n)+2] = cellGradient;
               color[(4*n)+3] = cellGradient;
            }
         }

         if( cell[n] == currentCell ){
            cellx[4*n]-=10;       celly[4*n]-=10;
            cellx[(4*n)+1]+=10;   celly[(4*n)+1]-=10;
            cellx[(4*n)+2]+=10;   celly[(4*n)+2]+=10;
            cellx[(4*n)+3]-=10;   celly[(4*n)+3]+=10;

            color[4*n]     = cellFocusColor;
            color[(4*n)+1] = cellFocusColor;
            color[(4*n)+2] = cellFocusGradient;
            color[(4*n)+3] = cellFocusGradient;
         }
      }

      g.drawShadedFilledPath(cellx, celly, null, color, paths);

      for( int n=0; n<count; ++n ){
         CellInfo ci = (CellInfo)cells.elementAt(n%usedCells);
         int imx;// = (cellWidth - ci.image.getWidth())>>>1;
         int imy;// = (cellHeight - ci.image.getHeight())>>>1;
         //image = ci.image.getBitmap();
         if( (cell[n] == currentCell) ){
            if( selected[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth+20));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight+20));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               selected[cell[n]] = image;
            } else
               image = selected[cell[n]];
            imx = ((cellWidth+20) - image.getWidth())>>>1;
            imy = ((cellHeight+20) - image.getHeight())>>>1;            
         } 
         else {
            if( normal[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               normal[cell[n]] = image;
            } else
               image = normal[cell[n]];
            imx = ((cellWidth) - image.getWidth())>>>1;
            imy = ((cellHeight) - image.getHeight())>>>1;            
         } 
         g.drawBitmap(imx+cellx[4*n], imy+celly[4*n], image.getWidth(), image.getHeight(), image, 0, 0);
      }
   }
   
   public void paintHorizontal(Graphics g){            
      int w = myWidth();
      int h = myHeight();
      
      g.setColor(stripColor);
      g.drawFilledPath(stripx, stripy, null, null);
      g.setColor(cellColor);
      g.drawLine(0, 0, w, 0);
      g.drawLine(0, h-1, w, h-1);

      for( int n=0; n<color.length; ++n ) color[n] = cellColor;

      startx = xoffset+((w>>>1)-(cellWidth>>>1));
      
      if( (startx+(count*(cellWidth+20))) <= 0 ){
         startx = (startx+(count*(cellWidth+20)));
         xoffset = startx-((myWidth()>>>1)-(cellWidth>>>1));
      } else if( startx >= myWidth() ){
         startx = (startx-(count*(cellWidth+20)));
         xoffset = startx-((myWidth()>>>1)-(cellWidth>>>1));
      }

      for( int n=0; n<count; n++ ){
         cellx[4*n]     = startx+(20*n)+(cellWidth*n);
         cellx[(4*n)+1] = startx+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+2] = startx+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+3] = startx+(20*n)+(cellWidth*n);

         celly[4*n]     = starty;
         celly[(4*n)+1] = starty;
         celly[(4*n)+2] = starty+cellHeight;
         celly[(4*n)+3] = starty+cellHeight;

         if( showSelectedCell ){
            if( cell[n] == selectedCell ){
               color[4*n]     = cellColor;
               color[(4*n)+1] = cellColor;
               color[(4*n)+2] = cellGradient;
               color[(4*n)+3] = cellGradient;
            }
         }

         if( cell[n] == currentCell ){
            cellx[4*n]-=10;       celly[4*n]-=10;
            cellx[(4*n)+1]+=10;   celly[(4*n)+1]-=10;
            cellx[(4*n)+2]+=10;   celly[(4*n)+2]+=10;
            cellx[(4*n)+3]-=10;   celly[(4*n)+3]+=10;
         }
      }

      g.drawShadedFilledPath(cellx, celly, null, color, paths);

      Bitmap image = null;
      for( int n=0; n<count; ++n ){
         CellInfo ci = (CellInfo)cells.elementAt(n%usedCells);
         int imx;// = (cellWidth - ci.image.getWidth())>>>1;
         int imy;// = (cellHeight - ci.image.getHeight())>>>1;
         //image = ci.image.getBitmap();
         if( (cell[n] == currentCell) ){
            if( selected[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth+20));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight+20));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               selected[cell[n]] = image;
            } else
               image = selected[cell[n]];
            imx = ((cellWidth+20) - image.getWidth())>>>1;
            imy = ((cellHeight+20) - image.getHeight())>>>1;  
            g.setGlobalAlpha(255);
         } 
         else {
            if( normal[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               normal[cell[n]] = null;
            } else
               image = normal[cell[n]];
            imx = ((cellWidth) - image.getWidth())>>>1;
            imy = ((cellHeight) - image.getHeight())>>>1;  
            g.setGlobalAlpha(128);
         } 
         g.drawBitmap(imx+cellx[4*n], imy+celly[4*n], image.getWidth(), image.getHeight(), image, 0, 0);
      }      

      if( startx > 0 ){
         xoffset2 = startx-(count*(cellWidth+20));
      } else {
         xoffset2 = startx+(count*(cellWidth+20));
      }

      for( int n=0; n<color.length; n++ ) color[n] = cellColor;
      
      for( int n=0; n<count; n++ ){
         cellx[4*n]     = xoffset2+(20*n)+(cellWidth*n);
         cellx[(4*n)+1] = xoffset2+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+2] = xoffset2+cellWidth+(20*n)+(cellWidth*n);
         cellx[(4*n)+3] = xoffset2+(20*n)+(cellWidth*n);

         celly[4*n]     = starty;
         celly[(4*n)+1] = starty;
         celly[(4*n)+2] = starty+cellHeight;
         celly[(4*n)+3] = starty+cellHeight;

         if( showSelectedCell ){
            if( cell[n] == selectedCell ){
               color[4*n]     = cellColor;
               color[(4*n)+1] = cellColor;
               color[(4*n)+2] = cellGradient;
               color[(4*n)+3] = cellGradient;
            }
         }

         if( cell[n] == currentCell ){
            cellx[4*n]-=10;       celly[4*n]-=10;
            cellx[(4*n)+1]+=10;   celly[(4*n)+1]-=10;
            cellx[(4*n)+2]+=10;   celly[(4*n)+2]+=10;
            cellx[(4*n)+3]-=10;   celly[(4*n)+3]+=10;
         }
      }

      g.drawShadedFilledPath(cellx, celly, null, color, paths);

      for( int n=0; n<count; ++n ){
         CellInfo ci = (CellInfo)cells.elementAt(n%usedCells);
         int imx;// = (cellWidth - ci.image.getWidth())>>>1;
         int imy;// = (cellHeight - ci.image.getHeight())>>>1;
         //image = ci.image.getBitmap();
         if( (cell[n] == currentCell) ){
            if( selected[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth+20));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight+20));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               selected[cell[n]] = image;
            } else
               image = selected[cell[n]];
            imx = ((cellWidth+20) - image.getWidth())>>>1;
            imy = ((cellHeight+20) - image.getHeight())>>>1;  
            g.setGlobalAlpha(255);
         } 
         else {
            if( normal[cell[n]] == null ){
               EncodedImage enc = ci.image; //EncodedImage.getEncodedImageResource( ci.imgname );
               int ratiow = Fixed32.div(Fixed32.toFP(ci.image.getWidth()), Fixed32.toFP(cellWidth));
               int ratioh = Fixed32.div(Fixed32.toFP(ci.image.getHeight()), Fixed32.toFP(cellHeight));
               int ratio = ratiow;
               if( ratioh > ratiow ) ratio = ratioh;
               image = enc.scaleImage32(ratio, ratio).getBitmap();
               normal[cell[n]] = image;
            } else
               image = normal[cell[n]];
            imx = ((cellWidth) - image.getWidth())>>>1;
            imy = ((cellHeight) - image.getHeight())>>>1;  
            g.setGlobalAlpha(128);
         } 
         g.drawBitmap(imx+cellx[4*n], imy+celly[4*n], image.getWidth(), image.getHeight(), image, 0, 0);
      }      
      
      g.setGlobalAlpha(255);
   }

   public void mypaint(Graphics g){ paint(g); }

   /**
    * Returns whether the button can gain focus
    *
    * @return focusable
    */
   public boolean isFocusable(){
      return true;
   }

   /**
    * Notifies any waiting objects that the field has been changed and consumes the click.
    *
    * @param status not used
    * @param time   not used
    * @return true to show this button consumed the click
    */
   protected boolean navigationClick(int status, int time) {
      selectedCell = currentCell;
      if( this.getChangeListener() != null ) this.getChangeListener().fieldChanged(this, 1);
      return true;
   }

   protected boolean navigationUnclick(int status, int time) {
      return true;
   }

   //Make an inertialScroller like in CLM
   protected boolean navigationMovement(int dx, int dy, int status, int time) {
      if( dx != 0 ){
         if( dx > 0 ){
            xoffset -= (cellWidth+20);
         } else {
            xoffset += (cellWidth+20);
         }
         return true;
      }
      if( dy != 0 ){
         if( dy > 0 ){
            yoffset -= (cellWidth+20);
         } else {
            yoffset += (cellWidth+20);
         }
         return true;
      }
      return false;
   }


   public boolean myNavigationMovement(int dx, int dy, int status, int time){
      return navigationMovement(dx, dy, status, time);
   }

   /**
    * Handles screen touches while this button has the focus
    *
    * @param t TouchEvent object with data about the TouchEvent
    */
   protected boolean touchEvent(TouchEvent t){
      int event = t.getEvent();

      switch( event ){
         case TouchEvent.CLICK:
            selectedCell = currentCell;
            if( this.getChangeListener() != null ) this.getChangeListener().fieldChanged(this, 1);
            return true;
         case TouchEvent.DOWN:
            if( inertial != null ) inertial.stop();
            curx = t.getX(1);
            cury = t.getY(1);
            break;
         case TouchEvent.MOVE:
            prevx = curx;
            prevy = cury;
            curx = t.getX(1);
            cury = t.getY(1);

            int xchange = (curx-prevx);
            int ychange = (cury-prevy);

            if( xchange > 0 ) if( xchange > 10 ) xchange = 10;
            if( xchange < 0 ) if( xchange < -10 ) xchange = -10;
            if( ychange > 0 ) if( ychange > 10 ) ychange = 10;
            if( ychange < 0 ) if( ychange < -10 ) ychange = -10;

            xoffset+=xchange;
            yoffset+=ychange;
            break;
         case TouchEvent.GESTURE:
            if( t.getGesture().getEvent() == TouchGesture.SWIPE ){
               TouchGesture gesture = t.getGesture();
               System.out.println(message+" :Swipe magnitude: "+gesture.getSwipeMagnitude());
               inertial = new InertialScroller(gesture);
               inertial.start();
               return true;               
            }
            break;
         default:
            break;
      }
      return super.touchEvent(t);
   }   

   public boolean myTouchEvent(TouchEvent t){
      return touchEvent(t);
   }

   /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

   /**
   **
   ** @return the x position of the field within its manager
   */
   public int getX(){
      /******* ADDED ***************/
      int xpos = xPos;
      int index = getIndex();
      if( index > 0 ){
         MyField tmp = ((MyField)getManager().getField(index-1));

         if( xpos == MyField.POS_ALIGN_LEFT )   return tmp.getX();
         if( xpos == MyField.POS_FLOAT_LEFT )   return tmp.getX()-myWidth();
         if( xpos == MyField.POS_ALIGN_RIGHT )  return (tmp.getX()+tmp.myWidth())-myWidth();
         if( xpos == MyField.POS_FLOAT_RIGHT )  return tmp.getX()+tmp.myWidth();
         //if( xpos == MyField.POS_ALIGN_CENTER ) return (((MyField)getManager()).myWidth()-myWidth())/2;
         //if( xpos == MyField.POS_MGR_FLT_RGT )  return (((MyField)getManager()).myWidth()-myWidth());
      } else {
         if( xpos == MyField.POS_ALIGN_LEFT )  return 0;
         if( xpos == MyField.POS_FLOAT_LEFT )  return 0;
         if( xpos == MyField.POS_ALIGN_RIGHT ) return 0;
         if( xpos == MyField.POS_FLOAT_RIGHT ) return 0;
      }

      if( xpos == MyField.POS_ALIGN_CENTER ) return (((MyField)getManager()).myWidth()-myWidth())/2;
      if( xpos == MyField.POS_MGR_FLT_RGT )  return (((MyField)getManager()).myWidth()-myWidth());
      /*****************************/

      return xPos;
   }

   public void changeX(int x){ xPos = x; }

   public void moveX(int x){ xPos += x; }

   /**
   **
   ** @return the y position of field within its manager
   */
   public int getY(){
      /******* ADDED ***************/
      int ypos = yPos;
      int index = getIndex();
      if( index > 0 ){
         MyField tmp = ((MyField)getManager().getField(index-1));

         if( ypos == MyField.POS_ALIGN_TOP )    return tmp.getY();
         if( ypos == MyField.POS_FLOAT_ABOVE )  return tmp.getY()-myHeight();
         if( ypos == MyField.POS_ALIGN_BOTTOM ) return (tmp.getY()+tmp.myHeight())-myHeight();
         if( ypos == MyField.POS_FLOAT_BELOW )  return tmp.getY()+tmp.myHeight();
      } else {
         if( ypos == MyField.POS_ALIGN_TOP )    return 0;
         if( ypos == MyField.POS_FLOAT_ABOVE )  return 0;
         if( ypos == MyField.POS_ALIGN_BOTTOM ) return 0;
         if( ypos == MyField.POS_FLOAT_BELOW )  return 0;
      }      
      if( ypos == MyField.POS_ALIGN_CENTER ) return (((MyField)getManager()).myHeight()-myHeight())/2;
      if( ypos == MyField.POS_MGR_FLT_BTM )  return (((MyField)getManager()).myHeight()-myHeight());
      /*****************************/
      return yPos;
   }

   public void changeY(int y){ yPos = y; }

   public void moveY(int y){
      if( (yPos < 10120) || (yPos > 10123) )
         yPos += y;
   }

   /**
   **
   ** @return the width of the field
   */
   public int myWidth(){
      if( width == MyField.FULLWIDTH ) return Display.getWidth();
      if( width == MyField.HALFWIDTH ) return Display.getWidth() / 2;
      if( width == MyField.THIRDWIDTH ) return Display.getWidth() / 3;
      if( width == MyField.QUARTERWIDTH ) return Display.getWidth() / 4;
      if( width == MyField.TWOTHIRDWIDTH ) return (Display.getWidth() * 2) / 3;
      if( width == MyField.THREEQUARTERWIDTH ) return (Display.getWidth() * 3) / 4;
      if( width == MyField.FIFTHWIDTH) return (Display.getWidth() / 5);
      if( width == MyField.TWOFIFTHWIDTH) return (Display.getWidth() * 2) / 5;
      if( width == MyField.THREEFIFTHWIDTH) return (Display.getWidth() * 3) / 5;
      if( width == MyField.FOURFIFTHWIDTH) return (Display.getWidth() * 4) / 5;

      if( width == MyField.PCNT_10_WIDTH) return (Display.getWidth() / 10);
      if( width == MyField.PCNT_30_WIDTH) return (Display.getWidth() * 3) / 10;
      if( width == MyField.PCNT_70_WIDTH) return (Display.getWidth() * 7) / 10;
      if( width == MyField.PCNT_90_WIDTH) return (Display.getWidth() * 9) / 10;

      if( width == MyField.MGR_FULLWIDTH ) return ((MyField)getManager()).myWidth();
      if( width == MyField.MGR_HALFWIDTH ) return ((MyField)getManager()).myWidth() / 2;
      if( width == MyField.MGR_THIRDWIDTH ) return ((MyField)getManager()).myWidth() / 3;
      if( width == MyField.MGR_QUARTERWIDTH ) return ((MyField)getManager()).myWidth() / 4;
      if( width == MyField.MGR_TWOTHIRDWIDTH ) return (((MyField)getManager()).myWidth() * 2) / 3;
      if( width == MyField.MGR_THREEQUARTERWIDTH ) return (((MyField)getManager()).myWidth() * 3) / 4;
      if( width == MyField.MGR_FIFTHWIDTH) return (((MyField)getManager()).myWidth() / 5);
      if( width == MyField.MGR_TWOFIFTHWIDTH) return (((MyField)getManager()).myWidth() * 2) / 5;
      if( width == MyField.MGR_THREEFIFTHWIDTH) return (((MyField)getManager()).myWidth() * 3) / 5;
      if( width == MyField.MGR_FOURFIFTHWIDTH) return (((MyField)getManager()).myWidth() * 4) / 5;

      if( width == MyField.MGR_PCNT_10_WIDTH) return (((MyField)getManager()).myWidth() / 10);
      if( width == MyField.MGR_PCNT_30_WIDTH) return (((MyField)getManager()).myWidth() * 3) / 10;
      if( width == MyField.MGR_PCNT_70_WIDTH) return (((MyField)getManager()).myWidth() * 7) / 10;
      if( width == MyField.MGR_PCNT_90_WIDTH) return (((MyField)getManager()).myWidth() * 9) / 10;

      if( width == MGR_MINUS_10 ) return ((MyField)getManager()).myWidth()-10;
      if( width == MGR_MINUS_20 ) return ((MyField)getManager()).myWidth()-20;
      if( width == MGR_MINUS_30 ) return ((MyField)getManager()).myWidth()-30;
      if( width == MGR_MINUS_40 ) return ((MyField)getManager()).myWidth()-40;
      if( width == MGR_MINUS_50 ) return ((MyField)getManager()).myWidth()-50;
      if( width == MGR_MINUS_60 ) return ((MyField)getManager()).myWidth()-60;
      if( width == MGR_MINUS_70 ) return ((MyField)getManager()).myWidth()-70;
      if( width == MGR_MINUS_80 ) return ((MyField)getManager()).myWidth()-80;
      if( width == MGR_MINUS_90 ) return ((MyField)getManager()).myWidth()-90;
      if( width == MGR_MINUS_100 ) return ((MyField)getManager()).myWidth()-100;

      return width;
   }

   public void setWidth(int w){
      width = w;
   }

   /**
   **
   ** @return the height of the field
   */
   public int myHeight(){
      if( height == MyField.FULLHEIGHT ) return Display.getHeight();
      if( height == MyField.HALFHEIGHT ) return Display.getHeight() / 2;
      if( height == MyField.THIRDHEIGHT ) return Display.getHeight() / 3;
      if( height == MyField.QUARTERHEIGHT ) return Display.getHeight() / 4;
      if( height == MyField.TWOTHIRDHEIGHT ) return (Display.getHeight() * 2) / 3;
      if( height == MyField.THREEQUARTERHEIGHT ) return (Display.getHeight() * 3) / 4;
      if( height == MyField.FIFTHHEIGHT) return (Display.getHeight() / 5);
      if( height == MyField.TWOFIFTHHEIGHT) return (Display.getHeight() * 2) / 5;
      if( height == MyField.THREEFIFTHHEIGHT) return (Display.getHeight() * 3) / 5;
      if( height == MyField.FOURFIFTHHEIGHT) return (Display.getHeight() * 4) / 5;

      if( height == MyField.PCNT_10_HEIGHT) return (Display.getHeight() / 10);
      if( height == MyField.PCNT_30_HEIGHT) return (Display.getHeight() * 3) / 10;
      if( height == MyField.PCNT_70_HEIGHT) return (Display.getHeight() * 7) / 10;
      if( height == MyField.PCNT_90_HEIGHT) return (Display.getHeight() * 9) / 10;

      if( height == MyField.MGR_FULLHEIGHT ) return ((MyField)getManager()).myHeight();
      if( height == MyField.MGR_HALFHEIGHT ) return ((MyField)getManager()).myHeight() / 2;
      if( height == MyField.MGR_THIRDHEIGHT ) return ((MyField)getManager()).myHeight() / 3;
      if( height == MyField.MGR_QUARTERHEIGHT ) return ((MyField)getManager()).myHeight() / 4;
      if( height == MyField.MGR_TWOTHIRDHEIGHT ) return (((MyField)getManager()).myHeight() * 2) / 3;
      if( height == MyField.MGR_THREEQUARTERHEIGHT ) return (((MyField)getManager()).myHeight() * 3) / 4;
      if( height == MyField.MGR_FIFTHHEIGHT) return (((MyField)getManager()).myHeight() / 5);
      if( height == MyField.MGR_TWOFIFTHHEIGHT) return (((MyField)getManager()).myHeight() * 2) / 5;
      if( height == MyField.MGR_THREEFIFTHHEIGHT) return (((MyField)getManager()).myHeight() * 3) / 5;
      if( height == MyField.MGR_FOURFIFTHHEIGHT) return (((MyField)getManager()).myHeight() * 4) / 5;

      if( height == MyField.MGR_PCNT_10_HEIGHT) return (((MyField)getManager()).myHeight() / 10);
      if( height == MyField.MGR_PCNT_30_HEIGHT) return (((MyField)getManager()).myHeight() * 3) / 10;
      if( height == MyField.MGR_PCNT_70_HEIGHT) return (((MyField)getManager()).myHeight() * 7) / 10;
      if( height == MyField.MGR_PCNT_90_HEIGHT) return (((MyField)getManager()).myHeight() * 9) / 10;

      if( height == MGR_MINUS_10 ) return ((MyField)getManager()).myHeight()-10;
      if( height == MGR_MINUS_20 ) return ((MyField)getManager()).myHeight()-20;
      if( height == MGR_MINUS_30 ) return ((MyField)getManager()).myHeight()-30;
      if( height == MGR_MINUS_40 ) return ((MyField)getManager()).myHeight()-40;
      if( height == MGR_MINUS_50 ) return ((MyField)getManager()).myHeight()-50;
      if( height == MGR_MINUS_70 ) return ((MyField)getManager()).myHeight()-70;
      if( height == MGR_MINUS_80 ) return ((MyField)getManager()).myHeight()-80;
      if( height == MGR_MINUS_90 ) return ((MyField)getManager()).myHeight()-90;
      if( height == MGR_MINUS_100 ) return ((MyField)getManager()).myHeight()-100;

      return height;
   }

   public void setHeight(int h){
      height = h;
   }

   /**
   **
   ** @return text displayed on the field if any
   */
   public String getText(){ return message; }
   public void setText(String txt){ message = txt; }

   public void setTextJustify(int i){ }

   public void setTouchEnabled(boolean b){ }
   public boolean getTouchEnabled(){ return false; }

   public void setToolTip(ToolTip t){ tip = t; }
   public ToolTip getToolTip(){ return tip; }

   public void forceFocus(){ }
   public void removeFocus(){ }

   public void addUiTimer(Timer t){ timer = t; }

   /*******************************************************************************************/
   protected class CellInfo{
      //Bitmap image;
      EncodedImage image;
      String label;
      String imgname;
      protected CellInfo(String msg, String img, boolean resource){
         label = msg;
         imgname = img;
         if( img == null ){
            image = EncodedImage.getEncodedImageResource("CM", "images/misc/blank.png");
         } else if( img.equals("") ){
            image = EncodedImage.getEncodedImageResource("CM", "images/misc/blank.png");
         } else {
            if( resource ) image = EncodedImage.getEncodedImageResource(img);
            else {
               try {
                  FileConnection fc = (FileConnection)Connector.open("file:///"+img);
                  InputStream is = fc.openInputStream();
                  byte [] data = new byte [(int)fc.fileSize()];
                  is.read(data, 0, data.length);
                  is.close();
                  fc.close();
                  image = EncodedImage.createEncodedImage(data, 0, data.length);                  
               } catch(Exception e){
                  System.out.println(e.toString());
               }   
            }
         }
      }
   }
   /*************************************************************************************/
   protected class InertialScroller extends Thread {
      boolean alive = true;
      int scrollx = 0, scrolly = 0;
      boolean scrollxpos = true, scrollypos = true;
      InertialScroller(TouchGesture tg){
         int mag = tg.getSwipeMagnitude();
         int dir = tg.getSwipeDirection();
         switch(dir){
            case TouchGesture.SWIPE_NORTH:
               scrolly = -mag; scrollypos = false;
               break;
            case TouchGesture.SWIPE_SOUTH:
               scrolly = mag;
               break;
            case TouchGesture.SWIPE_EAST:
               scrollx = mag;
               break;
            case TouchGesture.SWIPE_WEST:
               scrollx = -mag; scrollxpos = false;
               break;
            case TouchGesture.SWIPE_NORTH | TouchGesture.SWIPE_EAST:
               break;
            case TouchGesture.SWIPE_SOUTH | TouchGesture.SWIPE_EAST:
               break;
            case TouchGesture.SWIPE_NORTH | TouchGesture.SWIPE_WEST:
               break;
            case TouchGesture.SWIPE_SOUTH | TouchGesture.SWIPE_WEST:
               break;
         }
         if( cellDirection == VERTICAL_STRIP ) scrollx = 0;
         if( cellDirection == HORIZONTAL_STRIP ) scrolly = 0;
         System.out.println("Inertial Scroller Magnitudes - x: "+scrollx+" y: "+scrolly);
      }
      public void run(){
         while( alive ){
            System.out.println("Inertial Scroller is scrolling..........");
            if( scrollx != 0 ){ 
               if( scrollx > 0 ){
                  xoffset+=Math.sqrt((scrollx));
                  xoffset2+=Math.sqrt((scrollx));
               } else {
                  xoffset-=Math.sqrt((-scrollx));
                  xoffset2-=Math.sqrt((-scrollx));
               }
               if( scrollxpos ){ scrollx-=2; if( scrollx < 0 ) scrollx = 0; }
               else{ scrollx+=2; if( scrollx > 0 ) scrollx = 0; }
            }
            if( scrolly != 0 ){
               if( scrolly > 0 ){
                  yoffset+=Math.sqrt((scrolly));
                  yoffset2+=Math.sqrt((scrolly));
               } else {
                  yoffset-=Math.sqrt((-scrolly));
                  yoffset2-=Math.sqrt((-scrolly));
               }
               if( scrollypos ){ scrolly-=2; if( scrolly < 0 ) scrolly = 0; }
               else{ scrolly+=2; if( scrolly > 0 ) scrolly = 0; }
            }
            UiApplication.getUiApplication().getActiveScreen().invalidate();
            try{Thread.sleep(20);}catch(Exception e){System.out.println(e.toString());}  
            
            if( (scrollx==0) && (scrolly==0) ) stop();
         }
      }
      public void stop(){ 
         alive = false; 
         System.out.println("Stopping Inertial scroller......updating screen");          
         UiApplication.getUiApplication().getActiveScreen().invalidate();         
      }
   }
}

 

It behaves much like the picture field in the FB application for blackberry.

Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10

Re: carousel image menu in blackberry

There are so many dependencies in this code. Share these classes also 

 

import CM.api.ui.blackberry.MyField;
import CM.api.ui.blackberry.ToolTip;
import CM.api.ui.blackberry.CustomLayoutManager;
import CM.api.ui.blackberry.CustomFieldChangeListener;
Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: carousel image menu in blackberry

The ToolTip class is irrellevant; it's required because of a function in the MyField interface. MyField and CustomFieldChangeListener are just interfaces to standardize functions in my UI Library (CustomFieldChangeListener is just an extension of the FieldChangeListener with an extra function called public void passData(Object o) so that I can pass info to the Listener). The CustomLayoutManager is complicated, but I don't think it is needed for this class either. The main parts of the class are just the drawing functions, the event handler functions (touch,navigation,click, etc), and the drawing prep function. The class is not meant to copy line for line as its part of a completely custom UI Library. All of the calculations are done in the paint and prep routines and the event functions just tell the class how to update itself.

Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10

Re: carousel image menu in blackberry

Thanks for update

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Contributor
rahulkrishna081980
Posts: 18
Registered: ‎11-16-2011
My Device: 9900

Re: carousel image menu in blackberry

Thanks for sharing the file. Please share the complete code with all the java classes as I am new to Blackberry it may be grate help if you provide all the related java classes.

 

 

Thanks in Advance

 

 

 

Regards

Rahul