为黑莓制作模拟时钟

时间:2011-09-06 09:04:56

标签: blackberry

我想在黑莓中制作模拟时钟,我希望时钟的手是自定义图像。

我经历了这个帖子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 Image Hour Image Minuts Image

1 个答案:

答案 0 :(得分: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]);
    }

然后,在这个片段中,你可以看到他通过传入当前的“second”作为索引从数组中获取Bitmap对象:

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

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