可以Bitmap
在创建 之前水平拉伸BitmapField
,这将解决问题。但是拉伸Bitmap
和使用它作为标题会给支持屏幕旋转的设备(例如 Storm、Torch 系列)带来问题。在这种情况下,您必须维护两个拉伸Bitmap
实例,一个用于纵向模式,另一个用于横向模式。而且您还需要编写一些额外的代码来Bitmap
根据方向设置适当的。如果您不想这样做,请检查以下两种方法:
使用 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
将使用其所有可用宽度的 a,那么在屏幕旋转的情况下,它将处理其大小和背景绘制。并且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) {
}
}
}