在創建BitmapField
之前,可以水平拉伸Bitmap
,這樣可以解決問題。但是如果延伸到Bitmap
並將其作爲標題使用,將會爲支持屏幕旋轉的設備(例如Storm,Torch系列)帶來問題。在這種情況下,您必須保持兩個拉伸的Bitmap
實例,一個用於縱向模式,另一個用於橫向模式。而且您還需要編寫一些額外的代碼,以根據方向設置適當的Bitmap
。如果你不想這樣做,那麼請檢查下面的方法2:
使用CustomBitmapField例如
一個CustomBitmapField可拉長Bitmap
水平都可以使用。檢查實施。
class MyScreen extends MainScreen {
public MyScreen() {
Bitmap bm = Bitmap.getBitmapResource("uperlogo.png");
setTitle(new CustomBitmapField(bm));
}
class CustomBitmapField extends Field {
private Bitmap bmOriginal;
private Bitmap bm;
private int bmHeight;
public CustomBitmapField(Bitmap bm) {
this.bmOriginal = bm;
this.bmHeight = bm.getHeight();
}
protected void layout(int width, int height) {
bm = new Bitmap(width, bmHeight);
bmOriginal.scaleInto(bm, Bitmap.FILTER_BILINEAR);
setExtent(width, bmHeight);
}
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, bm.getWidth(), bmHeight, bm, 0, 0);
}
}
}
使用背景實例
Background
對象可以很容易地解決這個問題。如果一個Background
實例可以設置爲HorizontalFieldManager
,它將使用其可用的所有寬度,那麼在屏幕旋轉的情況下,它將處理其大小和背景繪畫。並且Background
實例本身會照顧所提供的Bitmap
的擴展。檢查下面的代碼。
class MyScreen extends MainScreen {
public MyScreen() {
setTitle(getMyTitle());
}
private Field getMyTitle() {
// Logo.
Bitmap bm = Bitmap.getBitmapResource("uperlogo.png");
// Create a manager that contains only a dummy field that doesn't
// paint anything and has same height as the logo. Background of the
// manager will serve as the title.
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
Background bg = BackgroundFactory.createBitmapBackground(bm, Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT);
hfm.setBackground(bg);
hfm.add(new DummyField(bm.getHeight()));
return hfm;
}
// Implementation of a dummy field
class DummyField extends Field {
private int logoHeight;
public DummyField(int height) {
logoHeight = height;
}
protected void layout(int width, int height) {
setExtent(1, logoHeight);
}
protected void paint(Graphics graphics) {
}
}
}
「使用後臺實例」實現解決了我的問題。感謝一堆:) – Jazib 2012-08-07 09:20:20