0

我有一个包含 16 个 JMenuItems 的 JMenu,我希望预先显示 3 个项目,其余 13 个项目以 500 毫秒的延迟淡入。有没有办法在 Java 中制作这个动画?

4

2 回答 2

4

这并不像听起来那么容易。

基本上,我最初认为“我将在弹出菜单中附加一个弹出侦听器,菜单项被添加到”......但显然这不太好用。菜单弹出是根据需要动态构建的。有道理,但还是很痛苦。

所以相反,我发现如果我等待,addNotify我可以简单地启动动画引擎。

动画引擎是一个简单的概念。它有一个javax.swing.Timer以固定间隔滴答作响。再加上开始时间和持续时间,我们就可以计算出动画的进度,并alpha根据需要生成值。

剩下的唯一一件事就是通知所有相关方动画已更改,瞧……

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class FadeMenu {

    private AnimationEngine engine;

    public static void main(String[] args) {
        new FadeMenu();
    }

    public FadeMenu() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                engine = new AnimationEngine();

                JMenuBar mb = new JMenuBar();
                JMenu flip = new JMenu("Flip");

                flip.add("Static 1");
                flip.add("Static 2");
                flip.add("Static 3");

                flip.add(new FadeMenuItem("Fade 1"));
                flip.add(new FadeMenuItem("Fade 2"));
                flip.add(new FadeMenuItem("Fade 3"));
                flip.add(new FadeMenuItem("Fade 4"));

                mb.add(flip);

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(mb);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

    public class FadeMenuItem extends JMenuItem {

        public FadeMenuItem(String text) {
            super(text);
            engine.addTimingListener(new TimingListener() {
                @Override
                public void timingEvent() {
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(engine.getAlpha()));
            super.paintComponent(g2d);
            g2d.dispose();
        }

        @Override
        public void removeNotify() {
            Container parent = getParent();
            if (parent instanceof JPopupMenu) {
                JPopupMenu menu = (JPopupMenu) parent;
                engine.stop();
            }
            super.removeNotify(); 
        }

        @Override
        public void addNotify() {
            super.addNotify();
            Container parent = getParent();
            if (parent instanceof JPopupMenu) {
                JPopupMenu menu = (JPopupMenu) parent;
                engine.restart();
            }
        }
    }

    public interface TimingListener {

        public void timingEvent();
    }

    public class AnimationEngine {

        private Timer fade;
        private float alpha;
        private long startTime;
        private long duration = 1000;
        private List<TimingListener> listeners;

        public AnimationEngine() {
            listeners = new ArrayList<>(5);
            fade = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    long elapsed = System.currentTimeMillis() - startTime;
                    if (elapsed >= duration) {
                        ((Timer) e.getSource()).stop();
                        alpha = 1f;
                    } else {
                        alpha = (float) elapsed / (float) duration;
                    }
                    fireTimingEvent();
                }
            });
            fade.setRepeats(true);
            fade.setCoalesce(true);
            fade.setInitialDelay(500);
        }

        public void addTimingListener(TimingListener listener) {
            listeners.add(listener);
        }

        public void removeTimingListener(TimingListener listener) {
            listeners.add(listener);
        }

        protected void fireTimingEvent() {
            for (TimingListener listener : listeners) {
                listener.timingEvent();
            }
        }

        public void restart() {
            fade.stop();
            alpha = 0;
            fireTimingEvent();
            startTime = System.currentTimeMillis();
            fade.start();
        }

        public float getAlpha() {
            return alpha;
        }

        public void stop() {
            fade.stop();
        }
    }
}

虽然这适用于 Windows,但我担心它可能不适用于其他平台,因为生成菜单的方式(部分)由 UI 委托控制。这可能会变得非常混乱,很快

于 2013-04-09T03:35:06.337 回答
0

启动计时器以触发事件以淡入

于 2013-04-09T03:19:14.930 回答