gpt4 book ai didi

image - BlackBerry Image Field 水平填充屏幕

转载 作者:行者123 更新时间:2023-12-04 05:36:53 24 4
gpt4 key购买 nike

我是 BlackBerry Development (5.0) 的新手。我在 Android 应用程序开发方面有一点经验。
我想要做的是在整个屏幕上填充图像(水平)
类似于您可以在 Android 中使用 fill_parent 执行的操作在布局文件中。
我在一些论坛上寻找解决方案,但没有得到满意的解决方案。

这就是我得到我的形象的方式

Bitmap headerLogo = Bitmap.getBitmapResource("uperlogo.png");
BitmapField headerLogoField =
new BitmapField(headerLogo, BitmapField.USE_ALL_WIDTH | Field.FIELD_HCENTER);
setTitle(headerLogoField);

这段代码在顶部(根据需要)和中心给了我我的标题。我只是想让它水平拉伸(stretch)以覆盖所有空间。

最佳答案

可以拉伸(stretch) Bitmap在创建 BitmapField 之前水平,这将解决问题。但拉伸(stretch)Bitmap并且使用它作为标题会给支持屏幕旋转的设备(例如 Storm、Torch 系列)带来问题。在这种情况下,您必须保持两个拉伸(stretch) Bitmap例如,一个用于纵向模式,另一个用于横向模式。您还需要编写一些额外的代码来设置适当的Bitmap取决于方向。如果您不想这样做,请检查以下两种方法:

使用 CustomBitmapField 实例

可以拉伸(stretch)的 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 的拉伸(stretch)。 .检查以下代码。

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) {
}
}
}

关于image - BlackBerry Image Field 水平填充屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com