BlackBerry Image字段水平填充屏幕
问题描述:
我是BlackBerry Development(5.0)的新手。我在Android应用程序开发方面有一点经验。 我想要做的是在整个屏幕上填充一个图像(水平) 类似于你可以在Android中使用fill_parent
在布局文件中完成的操作。 我看了一些论坛找到解决方案,但没有得到满意的。BlackBerry Image字段水平填充屏幕
这是我应得我的形象
Bitmap headerLogo = Bitmap.getBitmapResource("uperlogo.png");
BitmapField headerLogoField =
new BitmapField(headerLogo, BitmapField.USE_ALL_WIDTH | Field.FIELD_HCENTER);
setTitle(headerLogoField);
此代码是给了我在上面标题(如需要),并在该中心。我只是想让它水平延伸以覆盖所有空间。
答
在创建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