2016-12-31 28 views
1

我被困在一個簡單的動畫代碼,它有一個沒有。容器中的組件從屏幕頂部移動到特定位置。我在包含所有組件的容器中的UIID「AttempTitle」中設置了一個bg圖像集。問題是,所有的組件從頂部移動,bg img在到達特定位置並且最終出現時纔看到。我如何使bg圖像隨組件一起移動。Bg圖像問題 - cn1

我在做這件事時遇到了另一個問題。我在標題容器中有一個bg圖像。當動畫發生時,標題容器的綠色bg圖像也消失了一秒鐘。這是怎麼發生的,我無法弄清楚。

我有一個視頻在youtube上傳incase你不明白問題看看它。

https://youtu.be/6Or26wxnzUY

代碼:

setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER)); 

attemptIcon = theme.getImage("attemptIcon.png"); 
attempt1 = new Label(attemptIcon); 
attempt2 = new Label(attemptIcon); 
attempt3 = new Label(attemptIcon); 
attempt4 = new Label(attemptIcon); 
attempt5 = new Label(attemptIcon); 

attemptContainer = BoxLayout.encloseX(attempt1, attempt2, attempt3, attempt4, attempt5); 

Container titleContainer = new Container(new GridLayout(1)); 
titleContainer.add(FlowLayout.encloseCenterMiddle(attemptContainer)); 
add(BorderLayout.NORTH, titleContainer); 

attemptContainer.getParent().setUIID("AttempTitle"); 
attemptContainer.getParent().setPreferredW(screenWidth/3); 

questionAnswerContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 

Label a = new Label("questin answer"); 
questionAnswerContainer.add(a); 

titleDialog = new Label("Yuppie!"); 
body1 = new Label("Let’s celebrate"); 
body2 = new Label("with another"); 
body3 = new Label("drink"); 
Button ok = new Button(theme.getImage("playIcon.png")); 
ok.addActionListener(e -> { 
    new Test(sm).show(); 
}); 

dialogContainer = (BoxLayout.encloseY(titleDialog, body1, body2, body3, ok)); 
dialogContainer.getAllStyles().setBgImage(theme.getImage("yuppieDialog.png")); 

add(BorderLayout.CENTER, LayeredLayout.encloseIn(questionAnswerContainer, FlowLayout.encloseCenterMiddle(dialogContainer))); 
titleDialog.getAllStyles().setMarginTop((dialogContainer.getPreferredW()/3) + 30); 

dialogContainer.getParent().setVisible(false);//using setHidden(true) gives same issue 

Runnable r = new Runnable() { 
    public void run() { 
     checkIfCorrect(Test.this); 
    } 
}; 
if (timer == null) { 
    timer = new UITimer(r); 
} 
if (timer != null) { 
    timer.schedule(5000, false, Test.this); //4 seconds 
} 
revalidate(); 

checkIfCorrect方法:

public void checkIfCorrect(Form f) { 
    dialogContainer.getParent().setY(-Display.getInstance().getDisplayHeight()); 
    dialogContainer.getParent().setVisible(true); 
    f.animateHierarchyAndWait(1200); 
    f.setTransitionInAnimator(null); 
} 

工具欄代碼:

Toolbar toolbar = new Toolbar(); 
form.setToolbar(toolbar); 
Container containerTitle = new Container(new BorderLayout()); 
toolbar.setTitleComponent(LayeredLayout.encloseIn(containerTitle, FlowLayout.encloseCenter(ruslanLogo))) 
//there r 4 buttons inside containerTitle container 

回答

0

您應該使用animateLayout而不是animateHierarchy首先將你想要的元素設置爲可見,然後顯示。然後將其設置爲Y,將其設置爲可見並執行動畫布局。

問題是由於動畫效果,Container的背景大小/位置無法正常工作。

+0

在代碼中用animateLayout代替animateHierarchy什麼都不做,根本沒有動畫效果。組件剛剛出現。 – beck

+0

確保你在正確的容器上做到這一點。不在內容窗格或右側子窗體上。 –

+0

thankyou shai我製作了一個容器(添加到mainform borderlayout中心)並將其中的所有組件都保存在其中,然後爲該容器設置動畫並使其運行。但組件從工具欄下方滑下。如何使它在工具欄上方生成動畫?我上面也上傳了我的工具欄代碼。 – beck