2012-12-05 106 views
0

我想結合使用一種方法移動手的方法runSecrunMin。這些方法在時鐘面上移動分鐘和秒針幫助,謝謝。如何將類似的方法合併爲一個

public void settTime(int seconds) { 
    if(isTimer) 
    return; 
    tTime = seconds; 
    int minutes = seconds/60; 
    int hours = minutes/60; 
    minutes = minutes % 60; 
    seconds = seconds % 60;  
    tTimeLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds)); 
     runSec(seconds); 
     runMin(minutes); 
} 

public void runSec(int seconds) { 
RotateAnimation rotateAnimation = new RotateAnimation(seconds * 6, seconds * 6, 
Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
rotateAnimation.setFillAfter(true); 
csecond.startAnimation(rotateAnimation); 
} 

public void runMin(int minutes) { 
RotateAnimation rotateAnimation = new RotateAnimation(minutes * 6, minutes * 6, 
Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
rotateAnimation.setFillAfter(true); 
cminute.startAnimation(rotateAnimation); 
} 
+3

對不起,我不明白問題 – Entreco

回答

1
public void runMin(int minutes) { 
    cminute.startAnimation(createAnimation(minutes*6)); 
} 

public void runSec(int seconds) { 
    csecond.startAnimation(createAnimation(seconds*6)); 
} 

public RotateAnimation createAnimation(int time) { 
    RotateAnimation rotateAnimation = new RotateAnimation(time, time, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    return rotateAnimation; 
} 
0

基本上,你在這兩種方法做同樣的事情,這樣你就可以與任何單

public void runMinorSec(int time) { 
RotateAnimation rotateAnimation = new RotateAnimation(time * 6, minutes * 6, 
Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
rotateAnimation.setFillAfter(true); 
cminute.startAnimation(rotateAnimation); 
} 

更換,並且可以調用方法一樣

runMinorSec(seconds); 
    runMinorSec(minutes); 

但是你可以Concat的二方法一樣這 -

public void runSecOrMin(int time, boolean isSec) { 
if(isSec){ 
    RotateAnimation rotateAnimation = new RotateAnimation(seconds * 6, seconds * 6, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    csecond.startAnimation(rotateAnimation); 
}else{ 
    RotateAnimation rotateAnimation = new RotateAnimation(minutes * 6, minutes * 6, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    cminute.startAnimation(rotateAnimation); 
} 
} 

..

runSecOrMin(seconds,true); 
runSecOrMin(minutes,false); 
0

該方法看起來相同,原因似乎很容易猜到。時鐘上的第二隻手和第二隻手分別具有相同的動畫(1/60轉)。所以,最終歸結爲語法。如果您想用別的東西替換變量名稱minutesOrSeconds

public enum TimeType {SECOND, MINUTE} 

public void runMin(int time, TimeType timeType) { 
    RotateAnimation rotateAnimation = new RotateAnimation(time* 6, time* 6, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    if (TimeType == SECOND) { 
     csecond.startAnimation(rotateAnimation); 
    } else if (TimeType == MINUTE) { 
     cminute.startAnimation(rotateAnimation); 
    } 
} 
+0

其實最後一行是不同 – assylias

+0

@assylias良好的漁獲,我固定它。 – The111

2

你的方法已經幾乎完全相同了。只要傳遞另一個參數,將酌情采用值csecondcminute

public void runHand(int amount, Hand hand) { 
    RotateAnimation rotateAnimation = new RotateAnimation(amount * 6, amount * 6, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    hand.startAnimation(rotateAnimation); 
} 
0

試試這個,只要傳遞視圖參數,csecond和cminute。

public void runMin(int time, View target) { 
    RotateAnimation rotateAnimation = new RotateAnimation(time * 6, time * 6, 
    Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setFillAfter(true); 
    target.startAnimation(rotateAnimation); 
} 

希望有所幫助。

0

我檢查了你的2個方法,除了參數的名字沒有找到任何區別。如果我是對的,只需調用此方法run(int time),並按時間替換您使用minutesseconds的所有地方。在javadoc中提到時間可能在幾分鐘或幾秒鐘內。

0
public void runTime(int seconds, int minutes) 
{ 
RotateAnimation rotateAnimation = new RotateAnimation(seconds * 6, seconds * 6, 
Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
RotateAnimation rotateAnimation = new RotateAnimation(minutes * 6, minutes * 6, 
Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
rotateAnimation.setFillAfter(true); 
csecond.startAnimation(rotateAnimation); 
cminute.startAnimation(rotateAnimation); 
}