我想在我的Swing应用程序中开发吐司(Android)功能。作为一个独立的,它的工作完美。但是,当将其整合到应用程序中时,其存在问题。Android像秋千吐司
类文件是:
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import net.mindcrew.utils.LayoutHelper.Packer;
public class Toast extends JDialog {
String text;
public Toast(String text) {
this.text = text;
initComponents();
}
private void initComponents(){
setLayout(new GridBagLayout());
addComponentListener(new ComponentAdapter() {
// Give the window an rounded rect shape. LOOKS GOOD
// If the window is resized, the shape is recalculated here.
@Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0,0,getWidth(),getHeight(),50,50));
}
});
setUndecorated(true);
setSize(300,100);
setLocationRelativeTo(null);
getContentPane().setBackground(Color.BLACK);
// Determine what the GraphicsDevice can support.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
final boolean isTranslucencySupported =
gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
//If shaped windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
}
//If translucent windows aren't supported,
//create an opaque window.
if (!isTranslucencySupported) {
System.out.println(
"Translucency is not supported, creating an opaque window");
}
// Set the window to 70% translucency, if supported.
if (isTranslucencySupported) {
setOpacity(0.9f);
}
ImageIcon loading = new ImageIcon(Toast.class.getResource("/net/mindcrew/utils/userinterface/resources/loading-photo.gif"));
JLabel label = new JLabel(text);
label.setForeground(Color.WHITE);
label.setIcon(loading);
Packer packer = new Packer(this);
packer.pack(label).fillboth().west().inset(0, 50, 0, 20);
}
public static Toast showDailog(String textToDisplay){
final Toast toast = new Toast(textToDisplay);
// Display the window.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
toast.setVisible(true);
}
});
thread.start();
return toast;
}
@Override
public void hide(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(false);
dispose();
}
});
}
public static void main(String... args){
Toast toast = Toast.showDailog("Display");
try{
Thread.sleep(5000);
}
catch (Exception e){}
toast.hide();
}
}
可能有一些实施的故障,但是这是基本的东西。
这效果很好。但是当我试图用资源密集型运营的方式来解决它时,它会绊倒。正如在GIF动画中没有显示出来的那样,我认为这意味着它的这种停顿。
的使用方法是:
Toast toast = Toast.showDailog("Generating PDF");
//resource intensive operation. Takes about 3-5seconds to execute
toast.hide();
要添加到我的苦难,“吐司”已被释放后,也将应用变得可怕缓慢。我相当肯定,减速并不是因为有问题的操作,因为如果我放弃了“吐司”,它的工作是完美的。
请问有人可以指出这里有什么错?我想通过this question。但那里的事情远比我所寻找的要复杂得多。我正在寻找的是一个简单的对话框。不是一个需要容纳多个组件的完整吹制框架。
*问候 Binaek萨卡 基金会 httc://www.foundation.....in/*不包括SIGS。或在帖子中呼叫卡片。如果这些信息对您很重要,请将其放入[您的个人资料](http://stackoverflow.com/users/951243/binaek-sarkar)。 – 2012-04-15 10:35:39
糟糕!下次会记住它。感谢您指出。 – 2012-04-15 10:37:13
我在评论前删除了它。 – 2012-04-15 10:38:07