代码之家  ›  专栏  ›  技术社区  ›  Think.Smart

LayeredLayout容器作为背景,在Codenameone中单击时变为白色

  •  1
  • Think.Smart  · 技术社区  · 8 年前

    请你帮忙。我有一个非常简单的例子下面的一页,我想有一个2色的背景,对角线分裂,我已经实现了。因为这是一个分层布局,然后我将覆盖我的页面内容到它。

    Screenshot

    在模拟器中一切正常,但当我将应用程序放在android设备上时,当我按下屏幕的任何部分时,左上角的三角形变为白色,然后在取消按下时变回白色。右下角三角形保持原样。

    由于这是一个背景的主页,那么我显然不希望这种颜色的变化发生。 非常感谢

        package com.test;
    
    
    import com.codename1.ui.Button;
    import com.codename1.ui.Component;
    import com.codename1.ui.Container;
    import com.codename1.ui.Form;
    import com.codename1.ui.Graphics;
    import com.codename1.ui.geom.Dimension;
    import com.codename1.ui.layouts.BorderLayout;
    import com.codename1.ui.layouts.BoxLayout;
    import com.codename1.ui.layouts.LayeredLayout;
    
    public class AndroidKeyboard {
        private Form currentForm = new Form("", new BorderLayout());
    
        public void buildPage() {
            Container background = getBackground();
            currentForm.add(BorderLayout.CENTER, background);
            Button button = new Button("Click me");
            currentForm.add(BorderLayout.SOUTH, button);
            currentForm.show();
        }
    
        int topColour = 0x4371A4;
        int bottomColour = 0x70AEEA;
    
        private Container getBackground() {
    
            Container layeredContainer = new Container(new LayeredLayout());
            layeredContainer.setName("BackgroundContainer");
    
            currentForm.getToolbar().getAllStyles().setBgColor(topColour);
            currentForm.getToolbar().getAllStyles().setBgTransparency(255);
            //
            layeredContainer.getAllStyles().setBgColor(topColour);
            layeredContainer.getAllStyles().setBgTransparency(255);
    
            TopTriangleComponent t = new TopTriangleComponent();
            t.setTopColour(topColour);
            layeredContainer.add(t);
    
            BottomTriangleComponent b = new BottomTriangleComponent();
            b.setBottomColour(bottomColour);
            layeredContainer.add(b);
            return layeredContainer;
        }
    }
    
    class TopTriangleComponent extends Component {
    
        private int topColour;
    
        protected void setTopColour(int colour) {
            topColour = colour;
        }
    
        @Override
        protected Dimension calcPreferredSize() {
            return new Dimension(250, 250);
        }
    
        @Override
        public void paint(Graphics g) {
            g.setColor(topColour);
            int[] int_x = new int[] { 0, getWidth(), 0 };
            int[] int_y = new int[] { 0, 0, getHeight() };
    
            g.fillPolygon(int_x, int_y, 3);
        }
    }
    
    class BottomTriangleComponent extends Component {
    
        private int bottomColour;
    
        protected void setBottomColour(int colour) {
            bottomColour = colour;
        }
    
        @Override
        protected Dimension calcPreferredSize() {
            return new Dimension(250, 250);
        }
    
        @Override
        public void paint(Graphics g) {
            g.setColor(bottomColour);
            int[] int_x = new int[] { getWidth(), getWidth(), 0 };
            int[] int_y = new int[] { 0, getHeight(), getHeight() };
    
            g.fillPolygon(int_x, int_y, 3);
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Shai Almog    8 年前

    您正在创建两个不透明容器,但仅绘制其中的一部分。然后,把它们一个放在另一个上面,依靠你只画了一些容器的事实。这适用于某些形式的绘制(从下到上),但当您单击某个区域时,我们只绘制更改。在这一点上,我们检查不透明性,以防止成本画“一切”一遍。

    具体来说,这条线就是问题所在:

    currentForm.getToolbar().getAllStyles().setBgTransparency(255);
    

    应该是的 0 Painter 您可以使用样式将其应用于任何任意组件的背景: setBgPainter(Painter) .

    setBgTransparency(255) 在这一点上会正常工作,是正确的选择,因为你不会有额外的项目在你身后。

        2
  •  0
  •   Think.Smart    8 年前

    public class AndroidKeyboard {
    private Form currentForm = new Form("", new BorderLayout());
    
    public void buildPage() {
        Container background = getBackground();
        currentForm.add(BorderLayout.CENTER, background);
        Button button = new Button("Click me");
        currentForm.add(BorderLayout.SOUTH, button);
        currentForm.show();
    }
    
    int topColour = 0x4822A4;
    int bottomColour = 0x20AEEA;
    
    private Container getBackground() {
        Container cmp = new Container();
        Painter p = new Painter() {
            @Override
            public void paint(Graphics g, Rectangle rect) {
                boolean antiAliased = g.isAntiAliased();
                g.setAntiAliased(true);
    
                g.setColor(topColour);
    
                int[] int_x = new int[] { 0, currentForm.getWidth(), 0 };
                int[] int_y = new int[] { 0, 0, currentForm.getHeight() };
    
                g.fillPolygon(int_x, int_y, 3);
    
                g.setColor(bottomColour);
                int[] int_x2 = new int[] { currentForm.getWidth(), currentForm.getWidth(), 0 };
                int[] int_y2 = new int[] { 0, currentForm.getHeight(), currentForm.getHeight() };
    
                g.fillPolygon(int_x2, int_y2, 3);
                g.setAntiAliased(antiAliased);
            }
        };
        cmp.getAllStyles().setBgPainter(p);
        return cmp;
    }