这是一个简单的例子,但你必须自己处理装饰,平滑滚动等。
假设您有6张70x70图像。
简单的BitmapField扩展,用于绘制当前插槽图像、上半个图像和下半个图像:
class SlotField extends BitmapField {
Bitmap bmp1 = Bitmap.getBitmapResource("img1.png");
Bitmap bmp2 = Bitmap.getBitmapResource("img2.png");
Bitmap bmp3 = Bitmap.getBitmapResource("img3.png");
Bitmap bmp4 = Bitmap.getBitmapResource("img4.png");
Bitmap bmp5 = Bitmap.getBitmapResource("img5.png");
Bitmap bmp6 = Bitmap.getBitmapResource("img6.png");
Bitmap[] bmps = new Bitmap[] { bmp1, bmp2, bmp3, bmp4, bmp5, bmp6 };
int mPos = 0;
public SlotField(int position) {
mPos = position;
}
public int getBitmapHeight() {
return bmp1.getHeight() * 2;
}
public int getBitmapWidth() {
return bmp1.getWidth();
}
protected void layout(int width, int height) {
setExtent(getBitmapWidth(), getBitmapHeight());
}
int getNextPos() {
if (mPos == bmps.length - 1) {
return 0;
} else
return mPos + 1;
}
int getPrevPos() {
if (mPos == 0) {
return bmps.length - 1;
} else
return mPos - 1;
}
protected void paint(Graphics g) {
Bitmap hImg = bmps[getPrevPos()];
Bitmap mImg = bmps[mPos];
Bitmap lImg = bmps[getNextPos()];
g.drawBitmap(0, 0, 70, 35, hImg, 0, 35);
g.drawBitmap(0, 35, 70, 70, mImg, 0, 0);
g.drawBitmap(0, 105, 70, 35, lImg, 0, 0);
}
}
现在,将这些字段放到屏幕上,并用计时器设置动画:
class MainScr extends MainScreen {
SlotField slot1 = new SlotField(0);
SlotField slot2 = new SlotField(3);
SlotField slot3 = new SlotField(5);
boolean running = false;
public MainScr() {
HorizontalFieldManager hField = new HorizontalFieldManager();
add(hField);
hField.add(slot1);
hField.add(slot2);
hField.add(slot3);
ButtonField btnRoll = new ButtonField("Roll");
btnRoll.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (!running)
rollSlots();
}
});
add(btnRoll);
}
void rollSlots() {
Timer timer = new Timer();
final Random rnd = new Random();
TimerTask ttask1 = new TimerTask() {
int cycle = 0;
public void run() {
slot1.mPos = slot1.getNextPos();
invalidate();
cycle++;
if (cycle >= 100+rnd.nextInt(6))
cancel();
}
};
TimerTask ttask2 = new TimerTask() {
int cycle = 0;
public void run() {
slot2.mPos = slot2.getNextPos();
invalidate();
cycle++;
if (cycle >= 100+rnd.nextInt(6))
cancel();
}
};
TimerTask ttask3 = new TimerTask() {
int cycle = 0;
public void run() {
slot3.mPos = slot3.getNextPos();
invalidate();
cycle++;
if (cycle >= 100+rnd.nextInt(6))
cancel();
}
};
timer.schedule(ttask1, 0, 50);
timer.schedule(ttask2, 200, 50);
timer.schedule(ttask3, 400, 50);
}
}
alt text http://img534.imageshack.us/img534/2172/slots.jpg
有关UI功能,请阅读
Blackberry User Interface Design - Customizable UI?
和
Blackberry - fields layout animation