2012-10-17 94 views
0

爲了在我的BlackBerry應用程序中顯示可拖動的滑塊,我創建了一個SliderField類。該滑塊基本上用於設置應用程序中位置更新的間隔(通過GPS)。該應用程序構建在OS 5.0上,因此沒有爲SliderField找到API。問題 我面臨的問題是我需要在滑塊移動時增加標籤中的整數值。例如,如果標籤中的整數值爲1,並且用戶移動滑塊,則該值應該增加到5,10,15等。我不知道如何鏈接移動滑塊以增加標籤字段中的值。任何人都可以幫忙嗎?我將SliderField對象添加到屏幕上,如下所示:當滑塊移動時增加LabelField值

Bitmap sliderBack = Bitmap.getBitmapResource("progress51.png"); 
    Bitmap sliderFocus = Bitmap.getBitmapResource("progress51.png"); 
    Bitmap sliderThumb = Bitmap.getBitmapResource("butt.png"); 
SliderField theSlider = new SliderField(sliderThumb, sliderBack, sliderFocus,20, 1, 1, 1); 
     secondHFM.add(theSlider) 

下面是我對SliderField類的代碼。

public class SliderField extends Field 
{ 
Bitmap _imageThumb; 

Bitmap _imageSlider; 
Bitmap _imageSliderLeft; 
Bitmap _imageSliderCenter; 
Bitmap _imageSliderRight; 

Bitmap _imageSliderFocus; 
Bitmap _imageSliderFocusLeft; 
Bitmap _imageSliderFocusCenter; 
Bitmap _imageSliderFocusRight; 

private int _numStates; 
private int _currentState; 
private boolean _selected; 

private int _xLeftBackMargin; 
private int _xRightBackMargin; 

private int _thumbWidth; 
private int _thumbHeight; 

private int _totalHeight; 
private int _totalWidth; 

private int _rop; 

private int _backgroundColours[]; 
private int _backgroundSelectedColours[]; 

private int _defaultSelectColour = 0x977DED; 
private int _defaultBackgroundColour = 0x000000; 
private int _defaultHoverColour = 0x999999; 


public SliderField(Bitmap thumb 
      , Bitmap sliderBackground 
      , int numStates 
      , int initialState 
      , int xLeftBackMargin 
      , int xRightBackMargin) 
{ 
    this(thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE); 
} 

public SliderField(Bitmap thumb 
      , Bitmap sliderBackground 
      , int numStates 
      , int initialState 
      , int xLeftBackMargin 
      , int xRightBackMargin 
      , long style) 
{ 
    this(thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, style); 
} 

public SliderField(Bitmap thumb 
      , Bitmap sliderBackground 
      , Bitmap sliderBackgroundFocus 
      , int numStates 
      , int initialState 
      , int xLeftBackMargin 
      , int xRightBackMargin) 
{ 
    this(thumb, sliderBackground, sliderBackgroundFocus, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE); 
} 

public SliderField(Bitmap thumb 
      , Bitmap sliderBackground 
      , Bitmap sliderBackgroundFocus 
      , int numStates 
      , int initialState 
      , int xLeftBackMargin 
      , int xRightBackMargin 
      , long style) 
{ 
    super(style); 

    if(initialState > numStates || numStates < 2){ 
    } 
    _imageThumb = thumb; 
    _imageSlider = sliderBackground; 
    _imageSliderFocus = sliderBackgroundFocus; 
    _numStates = numStates; 
    setState(initialState); 

    _xLeftBackMargin = xLeftBackMargin; 
    _xRightBackMargin = xRightBackMargin; 

    _rop = _imageSlider.hasAlpha() ? Graphics.ROP_SRC_ALPHA : Graphics.ROP_SRC_COPY; 

    _thumbWidth = thumb.getWidth(); 
    _thumbHeight = thumb.getHeight(); 
    initBitmaps(); 
}    
public SliderField(Bitmap thumb 
      , Bitmap sliderBackground 
      , int numStates 
      , int initialState 
      , int xLeftBackMargin 
      , int xRightBackMargin 
      , int[] colours 
      , int[] selectColours) 
{ 
    this(thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE); 

    if(colours.length != numStates+1){ 
     throw new IllegalArgumentException(); 
    } 
    _backgroundColours = colours; 
    _backgroundSelectedColours = selectColours; 
} 
public void initBitmaps() 
{ 
    int height = _imageSlider.getHeight(); 

    _imageSliderLeft = new Bitmap(_xLeftBackMargin, height); 
    _imageSliderCenter = new Bitmap(_imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height); 
    _imageSliderRight = new Bitmap(_xRightBackMargin, height); 

    copy(_imageSlider, 0, 0, _xLeftBackMargin, height, _imageSliderLeft); 
    copy(_imageSlider, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderCenter); 
    copy(_imageSlider, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderRight); 

    _imageSliderFocusLeft = new Bitmap(_xLeftBackMargin, height); 
    _imageSliderFocusCenter = new Bitmap(_imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height); 
    _imageSliderFocusRight = new Bitmap(_xRightBackMargin, height); 

    copy(_imageSliderFocus, 0, 0, _xLeftBackMargin, height, _imageSliderFocusLeft); 
    copy(_imageSliderFocus, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderFocusCenter); 
    copy(_imageSliderFocus, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderFocusRight); 
} 
private void copy(Bitmap src, int x, int y, int width, int height, Bitmap dest) { 
    int[] argbData = new int[width * height]; 
    src.getARGB(argbData, 0, width, x, y, width, height); 
    for(int tx = 0; tx < dest.getWidth(); tx += width) { 
     for(int ty = 0; ty < dest.getHeight(); ty += height) { 
      dest.setARGB(argbData, 0, width, tx, ty, width, height); 
     } 
    } 
} 
public void setState(int newState) { 
    if(newState > _numStates){ 
     throw new IllegalArgumentException(); 
    } else { 
     _currentState = newState; 
     invalidate(); 
    } 
} 
public int getState() { 
    return _currentState; 
} 
public int getNumStates() { 
    return _numStates; 
} 
public int getColour() { 
    if(_backgroundSelectedColours != null) { 
     return _backgroundSelectedColours[getState()]; 
    } 
    return 0x000000; 
} 

public int getPreferredWidth() { 
    return _totalWidth; 
} 

public int getPreferredHeight() { 
    return _totalHeight; 
} 
protected void layout(int width, int height) { 
    if (width < 0 || height < 0) 
     throw new IllegalArgumentException(); 
    _totalWidth = width; 
    _totalHeight = Math.max(_imageSlider.getHeight(), _imageThumb.getHeight()); 

    setExtent(_totalWidth, _totalHeight); 
} 

public void paint(Graphics g) 
{ 
    int sliderHeight = _imageSlider.getHeight(); 
    int sliderBackYOffset = (_totalHeight - sliderHeight) >> 1; 
    int backgroundColor = _defaultBackgroundColour; 
    if(_backgroundSelectedColours != null || _backgroundColours != null) { 

     if(_selected) { 
      backgroundColor = _backgroundSelectedColours != null ? _backgroundSelectedColours[getState()] : _defaultSelectColour; 
     } else if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) { 
      backgroundColor = _backgroundColours != null ? _backgroundColours[getState()] : _defaultHoverColour; 
     } else { 
      backgroundColor = _defaultBackgroundColour; 
     } 
    } 
    g.setColor(backgroundColor); 
    g.fillRect(1, sliderBackYOffset + 1, _totalWidth - 2, sliderHeight - 2); 

    if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {  
     paintSliderBackground(g, _imageSliderFocusLeft, _imageSliderFocusCenter, _imageSliderFocusRight); 
    } else { 
     paintSliderBackground(g, _imageSliderLeft, _imageSliderCenter, _imageSliderRight); 
    } 
    int thumbXOffset = ((_totalWidth - _thumbWidth) * _currentState)/_numStates; 
    g.drawBitmap(thumbXOffset, (_totalHeight - _thumbHeight) >> 1, _thumbWidth, _thumbHeight, _imageThumb, 0, 0); 
} 

private void paintSliderBackground(Graphics g, Bitmap left, Bitmap middle, Bitmap right) 
{ 
    int sliderHeight = _imageSlider.getHeight(); 
    int sliderBackYOffset = (_totalHeight - sliderHeight) >> 1; 
    g.drawBitmap(0, sliderBackYOffset, _xLeftBackMargin, sliderHeight, left, 0, 0); 
    g.tileRop(_rop, _xRightBackMargin, sliderBackYOffset, _totalWidth - _xLeftBackMargin - _xRightBackMargin, sliderHeight, middle, 0, 0); 
    g.drawBitmap(_totalWidth - _xRightBackMargin, sliderBackYOffset, _xRightBackMargin, sliderHeight, right, 0, 0); 
} 

public void paintBackground(Graphics g) 
{ 
} 

protected void drawFocus(Graphics g, boolean on) 
{ 
    boolean oldDrawStyleFocus = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS); 
    try { 
     if(on) { 
      g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true); 
     } 
     paint(g); 
    } finally { 
     g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus); 
    } 
} 
protected boolean touchEvent(TouchEvent message) 
{ 
    boolean isConsumed = false; 
    boolean isOutOfBounds = false; 
    int x = message.getX(1); 
    int y = message.getY(1); 
    if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) { 
     isOutOfBounds = true; 
    } 
    switch(message.getEvent()) { 
     case TouchEvent.CLICK: 
     case TouchEvent.MOVE: 
      if(isOutOfBounds) return true; 
      _selected = true; 

      int stateWidth = getExtent().width/_numStates; 
      int numerator = x/stateWidth; 
      int denominator = x % stateWidth; 
      if(denominator > stateWidth/2) { 
       numerator++; 
      } 
      _currentState = numerator; 
      invalidate(); 

      isConsumed = true; 
      break; 
     case TouchEvent.UNCLICK: 
      if(isOutOfBounds) { 
       _selected = false; 
       return true; 
      } 
      _selected = false; 
      stateWidth = getExtent().width/_numStates; 
      numerator = x/stateWidth; 
      denominator = x % stateWidth; 
      if(denominator > stateWidth/2) { 
       numerator++; 
      } 
      _currentState = numerator; 
      invalidate(); 
      fieldChangeNotify(0); 
      isConsumed = true; 
      break; 
    } 
    return isConsumed; 
} 
protected boolean navigationMovement(int dx, int dy, int status, int time) 
{ 
    if(_selected) 
    { 
     if(dx > 0 || dy > 0) { 
      incrementState(); 
      fieldChangeNotify(0); 
      return true; 
     } else if(dx < 0 || dy < 0) { 
      decrementState(); 
      fieldChangeNotify(0); 
      return true; 
     } 
    } 
    return super.navigationMovement(dx, dy, status, time); 
} 
public void decrementState() { 
    if(_currentState > 0) { 
     _currentState--; 
     invalidate(); 
    } 
} 
public void incrementState() { 
    if(_currentState < _numStates) { 
     _currentState++; 
     invalidate(); 
    } 
} 
protected boolean invokeAction(int action) { 
    switch(action) { 
     case ACTION_INVOKE: { 
      toggleSelected(); 
      return true; 
     } 
    } 
    return false; 
} 
protected boolean keyChar(char key, int status, int time) { 
    if(key == Characters.SPACE || key == Characters.ENTER) { 
     toggleSelected(); 
     return true; 
    } 

    return false; 
} 
protected boolean trackwheelClick(int status, int time) { 
    if(isEditable()) { 
     toggleSelected(); 
     return true; 
    } 
    return super.trackwheelClick(status, time); 
} 
private void toggleSelected() { 
    _selected = !_selected; 
    invalidate(); 
} 
public void setDirty(boolean dirty) 
{ 
} 
public void setMuddy(boolean muddy) 
{ 
} 

} 

回答

1

如果使用下面的代碼,你會得到你的實際需求,您可以通過sliderfield引用調用的getValue()方法獲取滑塊電流值到你的屏幕類..

實施FieldChangeListener接口並嘗試覆蓋fieldChanged()方法。

使用Horizo​​ntalFieldManager類和setStatus()方法, 如果你想在你的屏幕的底部顯示你的滑桿,其他明智就可以正常顯示。

這裏的代碼:

public class SliderScreen extends MainScreen implements FieldChangeListener 
{ 
private SliderField slider; 
private HorizontalFieldManager hm; 

public SliderField(){ 

slider = new SliderField(
        Bitmap.getBitmapResource("slider2_thumb_normal.png"), 
        Bitmap.getBitmapResource("slider2_progress_normal.png"), 
        Bitmap.getBitmapResource("slider2_base_normal.png"), 
        Bitmap.getBitmapResource("slider2_thumb_focused.png"), 
        Bitmap.getBitmapResource("slider2_progress_focused.png"), 
        Bitmap.getBitmapResource("slider2_base_focused.png"), 8, 4, 
        8, 8, FOCUSABLE); 

      slider.setChangeListener(this); 
      hm = new HorizontalFieldManager(); 


      hm.add(slider); 

      setStatus(hm); 

} 



public void fieldChanged(Field field, int context) { 
     try { 

      if (field == slider) { 
       int value = slider.getValue(); 

      } 

     } catch (IllegalStateException e) { 

      e.printStackTrace(); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 

    } 
+0

你原來的答案是確定的;最新的鏈接將您的鏈接更改爲您爲了從此答案鏈接到博客而寫的博客文章,並且未包含您鏈接到自己博客的披露信息。我刪除了該鏈接,因爲它違反了規則。 –

1

我做的如何使用這個代碼簡單的例子,快來看

public final class MyScreen extends MainScreen implements FieldChangeListener//,FocusChangeListener 
{ 
    /** 
    * Creates a new MyScreen object 
    */ 
    public MyScreen() 
    {   
     // Set the displayed title of the screen  
     setTitle("MyTitle"); 

     SliderField slider; 

     slider = new SliderField( 
      Bitmap.getBitmapResource("slider_thumb_normal.png"), Bitmap.getBitmapResource("slider_progress_normal.png"), Bitmap.getBitmapResource("slider_base_normal.png"), 
      Bitmap.getBitmapResource("slider_thumb_focused.png"), Bitmap.getBitmapResource("slider_progress_focused.png"), Bitmap.getBitmapResource("slider_base_focused.png"), 
      Bitmap.getBitmapResource("slider_thumb_pressed.png"), Bitmap.getBitmapResource("slider_progress_pressed.png"), Bitmap.getBitmapResource("slider_base_pressed.png"), 
      10, 0, 12, 12, FOCUSABLE); 
     slider.setPadding(20, 20, 20, 20); 
     slider.setBackground(BackgroundFactory.createSolidBackground(0xD3D3D3)); 
     slider.setChangeListener(this); 
     //slider.focusChangeNotify(0); 
     add(slider); 




    } 

    public void fieldChanged(Field field, int context) { 
     // TODO Auto-generated method stub 
     if(field instanceof SliderField) 
     { 
      SliderField temp = (SliderField)field; 
      System.out.println("Temp value is"+temp.getValue()); 
     } 
    } 


} 
+0

如何減少這個滑動條的寬度?它佔用了全屏的寬度嗎? – String

+0

sliderField的佈局方法中,分配width = Display.getWidth()/ 2(無論你需要什麼),你將得到你需要的大小 – Rakesh

+0

hm = new Horizo​​ntalFieldManager(); hm.add(playimg_btn); \t \t \t hm.add(slider); setStatus(hm); 如果我像這樣放置圖像按鈕佔用了我的屏幕左側的一些滑塊空間,我該如何避免這種情況? – String