【发布时间】:2010-02-09 11:45:19
【问题描述】:
我有一个继承自 JPanel 的类,上面有一个图像,我想设置一个小动画来显示面板/图像,然后在事件触发时将其淡出。
我大概设置了一个线程并触发动画,但我该如何实际进行淡入淡出?
【问题讨论】:
我有一个继承自 JPanel 的类,上面有一个图像,我想设置一个小动画来显示面板/图像,然后在事件触发时将其淡出。
我大概设置了一个线程并触发动画,但我该如何实际进行淡入淡出?
【问题讨论】:
您可以自己进行线程处理,但使用Trident 库来处理它可能更容易。如果您在您的类上创建一个名为(例如,setOpacity)的设置器,您可以要求 trident 在特定时间段内将“不透明度”字段从 1.0 插入到 0.0(这里是 some of the docs 关于如何使用 Trident)。
当您绘制图像时,您可以使用 AlphaComposite 来设置透明度,使用合成的 alpha 参数的更新“不透明度”值。有一个 Sun 教程,其中包含一个 alpha composite example。
【讨论】:
这是一个使用 Alpha 透明度的示例。您可以使用this composite tool 查看使用不同颜色、模式和 alpha 的结果。
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AlphaTest extends JPanel implements ActionListener {
private static final Font FONT = new Font("Serif", Font.PLAIN, 32);
private static final String STRING = "Mothra alert!";
private static final float DELTA = -0.1f;
private static final Timer timer = new Timer(100, null);
private float alpha = 1f;
AlphaTest() {
this.setPreferredSize(new Dimension(256, 96));
this.setOpaque(true);
this.setBackground(Color.black);
timer.setInitialDelay(1000);
timer.addActionListener(this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(FONT);
int xx = this.getWidth();
int yy = this.getHeight();
int w2 = g.getFontMetrics().stringWidth(STRING) / 2;
int h2 = g.getFontMetrics().getDescent();
g2d.fillRect(0, 0, xx, yy);
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_IN, alpha));
g2d.setPaint(Color.red);
g2d.drawString(STRING, xx / 2 - w2, yy / 2 + h2);
}
@Override
public void actionPerformed(ActionEvent e) {
alpha += DELTA;
if (alpha < 0) {
alpha = 1;
timer.restart();
}
repaint();
}
static public void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setLayout(new GridLayout(0, 1));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new AlphaTest());
f.add(new AlphaTest());
f.add(new AlphaTest());
f.pack();
f.setVisible(true);
}
});
}
}
【讨论】:
有一些描述图像透明度的有用信息here。
你的方法是这样的:
Timer 以每 N 毫秒在 Event Dispatch 线程上触发一个 ActionEvent。ActionListener 添加到Timer,它应该在包含Image 的Component 上调用repaint()。Component 的paintComponent(Graphics) 方法以执行以下操作:
Graphics 对象转换为Graphics2D。setComposite 在Graphics2D 上设置AlphaComposite。这控制透明度级别。对于淡出动画的每次迭代,您都将更改 AlphaComposite 的值以使图像更加透明。
【讨论】: