2011-09-06 183 views
3

我想制作一个模拟时钟在黑莓手机上,我希望时钟之手可以成为自定义图像。为黑莓制作模拟时钟

我通过这个线程Help with analog clock code,但没有得到它的工作。任何机构可以帮我做一个模拟时钟

更新 我得到的代码是从支持论坛

// How to use it 

ClockBitmapField clock = new ClockBitmapField(face, Field.NON_FOCUSABLE | Field.FIELD_HCENTER, 
              hrPng, minPng, secPng); 

我我可以用这个做一个模拟时钟

如何使用此

add(clock); 

// Clock Face 

class ClockBitmapField extends BitmapField { 

Bitmap _face = null; 
UpdateClockThread _updateClockThread = null; 
Bitmap [] _hourBitmaps = null; 
Bitmap [] _minBitmaps = null; 
Bitmap [] _secBitmaps = null; 

public ClockBitmapField(Bitmap face, long style, String [] hourPngs, String [] minPngs, String [] secPngs) { 
    super(face, style); 
    _face = face; 
    _ourBitmap = new Bitmap(_face.getWidth(), _face.getHeight()); 
    this.setBitmap(_ourBitmap); // Swap to using work area 
    _hourBitmaps = new Bitmap [hourPngs.length]; 
    for (int i = 0; i < hourPngs.length; i++) { 
     _hourBitmaps[i] = Bitmap.getBitmapResource(hourPngs[i]); 
    } 
    _minBitmaps = new Bitmap [minPngs.length]; 
    for (int i = 0; i < minPngs.length; i++) { 
     _minBitmaps[i] = Bitmap.getBitmapResource(minPngs[i]); 
    } 
    _secBitmaps = new Bitmap [secPngs.length]; 
    for (int i = 0; i < secPngs.length; i++) { 
     _secBitmaps[i] = Bitmap.getBitmapResource(secPngs[i]); 
    } 
} 
protected void onDisplay() { 
    onExposed(); 
} 
protected void onUnDisplay() { 
    onObscured(); 
} 
protected void onExposed() { 
    if (_updateClockThread == null || !_updateClockThread.isAlive()) { 
     _updateClockThread = new UpdateClockThread(_ourBitmap, _face, this, _hourBitmaps, _minBitmaps, _secBitmaps); 
     _updateClockThread.start(); 
    } 
} 
protected void onObscured() { 
    if (_updateClockThread != null) { 
     _updateClockThread.stop(); 
     _updateClockThread = null; 
    } 
} 
public void invalidate() { 
    super.invalidate(); 
} 
class UpdateClockThread extends Thread { 
    private Calendar _cal = null; 
    int _curHr = 0; 
    int _curMin = 0; 
    int _curSec = 0; 
    Bitmap _face = null; 
    int _faceWidth = 0; 
    int _faceHeight = 0; 
    _ourBitmap = null; 
    Graphics _g = null; 
    ClockBitmapField _ourField = null; 
    long LONG_ONE_THOUSAND = 1000; 
    boolean _stopped = false; 
    Bitmap [] _hourBitmaps = null; 
    Bitmap [] _minBitmaps = null; 
    Bitmap [] _secBitmaps = null; 
    public UpdateClockThread(Bitmap ourBitmap, Bitmap face, ClockBitmapField fieldToInvalidate, 
          Bitmap [] hourBitmaps, Bitmap [] minBitmaps, Bitmap [] secBitmaps) { 
     super(); 
     _cal = Calendar.getInstance(); 
     _face = face; 
     _faceWidth = _face.getWidth(); 
     _faceHeight = _face.getHeight(); 
     _ourBitmap = ourBitmap; 
     _g = new Graphics(_ourBitmap); 
     _ourField = fieldToInvalidate; 
    } 
    public void run() { 
     long timeToSleep = 0; 
     while (!_stopped) { 
      _g.setBackgroundColor(0x00191919); 
      _g.clear(); 
      _g.drawBitmap(0, 0, _faceWidth, _faceHeight, _face, 0, 0); 
      _cal.setTime(new Date(System.currentTimeMillis())); 
      _curHr = cal.get(Calendar.HOUR);     
      _curMin = cal.get(Calendar.MINUTE); 
      _curHr = (_curHr * 5) + (5 * _curMin/60); 
      if (_curHr > 60) _curHr = _curHr - 60; 
      _curSec = cal.get(Calendar.SECOND); 
      _g.drawBitmap(0, 0, _faceWidth, _faceHeight, _secBitmaps[_curSec], 0, 0); 
      _g.drawBitmap(0, 0, _faceWidth, _faceHeight, _minBitmaps[_curMin], 0, 0); 
      _g.drawBitmap(0, 0, _faceWidth, _faceHeight, _hourBitmaps[_curHr], 0, 0); 
      _ourField.invalidate(); 
      timeToSleep = LONG_ONE_THOUSAND - (System.currentTimeMillis() % LONG_ONE_THOUSAND); 
      if (timeToSleep > 20) { 
       try { 
        Thread.sleep(timeToSleep); 
       } catch (Exception e) { 
       } 
      } 
     } 
    } 
    public void stop() { 
     _stopped = true; 
    } 
} 

} 

,并分钟为第二 感谢,并就通过查看代码对于 Face ImageHour ImageMinuts Image

+0

该线程包含良好的代码,你有尝试过吗?如果是这样,那么你能否指定你的问题是什么? – medopal

+0

@medopal是的,但我不知道如何传递字符串[]中的位图,所以我不能使用该代码,你可以告诉我如何去做 – BBdev

+1

用代码更新你的问题,并指出你没有代码行理解,那么我们可能会提供帮助。 – medopal

回答

3

,看来输入: String [] hourPngs, String [] minPngs, String [] secPngs分别代表时钟指针在每个位置上的图像的文件名列表。

在这个片段中,他建立的60 Bitmaps从字符串数组:

_secBitmaps = new Bitmap [secPngs.length]; 
    for (int i = 0; i < secPngs.length; i++) { 
     _secBitmaps[i] = Bitmap.getBitmapResource(secPngs[i]); 
    } 

然后,在这个片段中,你可以看到他通过传递当前的“第二”会从一个阵列的Bitmap对象作为索引:

_g.drawBitmap(0, 0, _faceWidth, _faceHeight, _secBitmaps[_curSec], 0, 0); 

似乎没有任何代码,他旋转的图像或任何东西。 所以我想这意味着每个钟针需要60个图像。 (共180幅图像,加上钟面)。

+0

我做了大小为60的数组与文件名称的图像,但不工作... – BBdev

+1

你可以比“不加工”?不会编译?抛出异常?屏幕上出现什么? – icchanobot

+0

我得到了空指针异常..........,当我按下继续模拟器上,我只能看到我已经作为位图传递的图像的脸.. – BBdev