我遇到了 Codenameone 未将组件显示为容器内的纯色图像标签的问题。如果图像是根据实际照片创建的,它会显示良好。
这不起作用(imageBlock 不显示在容器中):
Form form = new Form("Result", new BorderLayout());
Image solid = Image.createImage(350, 350, 0x0000ff);
Label imageBlock = new Label("", solid, "Container");
Label imageKey = new Label("Solid Image");
Container imageProfile = BoxLayout.encloseX(imageBlock, imageKey);
form.add(BorderLayout.NORTH, imageProfile);
这将起作用(imageBlock 显示在容器中):
Form form = new Form("Result", new BorderLayout());
Image photo = Image.createImage(Capture.capturePhoto(width, -1));
Label imageBlock = new Label("", photo.scaled(350, 350), "Container");
Label imageKey = new Label("Photo Image");
Container imageProfile = BoxLayout.encloseX(imageBlock, imageKey);
form.add(BorderLayout.NORTH, imageProfile);
任何人都可以解释为什么固体图像标签没有显示在容器中,我该怎么做才能使其显示?
问题在于图像的创建方法使用 ARGB 颜色,而不是 RGB 颜色。您需要将该行更改为:
Image solid = Image.createImage(350, 350, 0xff0000ff);
注意 alpha 通道颜色开头的
ff
。否则颜色是透明的。
请注意,使用颜色设置标签样式比创建图像更有效。您实际上正在创建一个占用内存的 350x350x4 字节的图像。这样做在 RAM 和性能方面会更有效:
// Space is needed since blank labels are hidden by default
Label solid = new Label(" ");
solid.setPreferredSize(350, 350);
solid.getUnselectedStyle().setBgTransparency(0xff);
solid.getUnselectedStyle().setBgColor(0xff);