Blackberry 为黑莓手机制作模拟时钟

Blackberry 为黑莓手机制作模拟时钟,blackberry,Blackberry,我想在黑莓手机上做一个模拟时钟,我想时钟的指针是定制的图像 我试过这条线,但没有成功。有人能帮我做一个模拟时钟吗 更新 我得到的代码来自支持论坛 // How to use it ClockBitmapField clock = new ClockBitmapField(face, Field.NON_FOCUSABLE | Field.FIELD_HCENTER, hrPng, minPng, secPng)

我想在黑莓手机上做一个模拟时钟,我想时钟的指针是定制的图像

我试过这条线,但没有成功。有人能帮我做一个模拟时钟吗

更新 我得到的代码来自支持论坛

// 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;
    }
}

} 
我如何使用此功能

 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;
    }
}

} 
add(时钟);
//钟面
类ClockBitmapField扩展了BitmapField{
位图_face=null;
UpdateLockThread _UpdateLockThread=null;
位图[]\u hourbitmap=null;
位图[]\u minBitmaps=null;
位图[]_secBitmaps=null;
公共ClockBitmapField(位图面、长样式、字符串[]小时、字符串[]分钟、字符串[]秒){
超级(脸型、风格);
_面=面;
_ourBitmap=新位图(_face.getWidth(),_face.getHeight());
this.setBitmap(_ourBitmap);//切换到使用工作区
_hourbitmap=新位图[hourPngs.length];
对于(int i=0;i60)\u curHr=\u curHr-60;
_curSec=cal.get(日历秒);
_g、 drawBitmap(0,0,_faceWidth,_faceHeight,_secBitmaps[_curSec],0,0);
_g、 drawBitmap(0,0,_faceWidth,_faceHeight,_MinBitmap[_curMin],0,0);
_g、 drawBitmap(0,0,_faceWidth,_faceHeight,_Hourbitmap[_curHr],0,0);
_ourField.invalidate();
timeToSleep=LONG\u ONE\u一千-(System.currentTimeMillis()%LONG\u ONE\u一千);
如果(睡眠时间>20){
试一试{
睡眠时间(timeToSleep);
}捕获(例外e){
}
}
}
}
公共停车场(){
_停止=真;
}
}
} 
以分钟为秒 谢谢和问候

只需查看代码,输入似乎:
String[]hourPngs、String[]minPngs、String[]secPngs
都是代表每个位置时钟指针的图像文件名列表

在这段代码中,他从字符串中构建了一个由60个位图组成的数组:

    _secBitmaps = new Bitmap [secPngs.length];
    for ( int i = 0; i < secPngs.length; i++ ) {
        _secBitmaps[i] = Bitmap.getBitmapResource(secPngs[i]);
    }
在他旋转图像或任何东西的地方似乎没有任何代码。
所以我猜这意味着每个时钟指针需要60张图像。(总共180个图像,加上时钟面)。

该线程包含良好的代码,您尝试过其中任何一个吗?如果是这样的话,那么您能指定您的问题是什么吗?@medopal是的,但我不知道如何以字符串[]传递位图,因此我无法使用该代码。您能告诉我如何使用该代码更新您的问题,并指出您不理解的代码行,然后我们可能会提供帮助。@medopal已用代码更新。。。你现在能看到并给我解决方案吗?我制作了大小为60的数组,其中包含图像的文件名,但不工作…你能比“不工作”更具体一些吗?不编译吗?抛出异常?屏幕上出现了什么?我得到了空指针异常。。。。。。。。。。当我按下模拟器上的“继续”按钮时,我只能看到作为位图传递的图像的面。听起来好像你的一张图像没有加载。如果你想发布你所有图片的链接,我可以试试(我不想自己制作180张图片)好的@icchanobot我给你图片。。。但我不知道如何把链接。。。。。