代号一 - 透明的前景和字体

Codename One - transparent foreground and fonts

我还没有找到任何选项来为组件的前景色或相关字体提供透明度。

我想做的是:

    Label halfTransparentLabel = new Label("Half Transparent text");
    Style s = halfTransparentLabel.getAllStyles();
    s.setBgColor(0);
    s.setBgTransparency(255);
    s.setFgColor(0xffffff);
    s.setFgTransparency(128); // this method does not exist

我知道画半透明的东西对性能很重要,但我只想在特定的部分上画。有了这个选项,将大大改善视觉吸引力和设计。

这可以解决吗?

更新的答案

感谢, I update my answer. The workaround I suggested is not necessary. The same result of the posted screenshot can be obtained with https://www.codenameone.com/javadoc/com/codename1/ui/plaf/Style.html#setOpacity-int-或在CSS中加上opacity属性,例如:

BigLabel {
    font-size: 6mm;
    font-family: "native:MainRegular";
    color: red;
    background-color: transparent;
    opacity: 0.5;
}

旧答案 - 是的,可以使用 .toImage() 方法解决此问题,如以下屏幕截图所示:

我不确定这个解决方法是否最好,但它确实有效。 本例代码:

Form hi = new Form("Semitransparent Example", BoxLayout.y());
hi.getToolbar().setUIID("Transparent");
hi.setUIID("FormBackground");

Container cnt = FlowLayout.encloseIn(new Label("Half Transparent Text", "BigLabel"));
// .setSize() and .revalidate(), in this case, are necessary to use the .image() method
cnt.setSize(new Dimension(hi.getContentPane().getWidth(), CN.convertToPixels(8, false)));
cnt.revalidate();
hi.add(cnt.toImage().modifyAlpha((byte) 125));

hi.show();

和 CSS:

#Constants {
    includeNativeBool: true; 
}

Transparent {
    background-color: transparent;
}

FormBackground {
    background-image: url("background.jpg");
}

BigLabel {
    font-size: 6mm;
    font-family: "native:MainRegular";
    color: red;
    background-color: transparent;
}

感谢您的回答。根据以上提示,最简单的代码解决方案如下:

component.getAllStyles().setOpacity(128);

那么使用 CSS 有什么好处?

让设计师负责所有的格式和样式,我已经讨厌在代码中做一些部分而在设计器中做一些。那么,为什么要通过引入 CSS 使其变得更加复杂呢? CSS 支持应该取代设计师吗?