2012-12-05 7 views
0

Je veux combiner les méthodes runSec et runMin de déplacer les mains en une seule méthode. Ces méthodes bougent les minutes et secondes sur le cadran de l'horloge Aide, merci.Comment combiner des méthodes similaires en une

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

Désolé, je ne comprends pas le problème – Entreco

Répondre

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

Fondamentalement, vous faites même chose dans la méthode que vous pouvez remplacer par une de un

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); 
} 

et vous pouvez appeler la méthode comme

runMinorSec(seconds); 
    runMinorSec(minutes); 

Mais vous pouvez concaténer deux comme méthode ce -

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

Les méthodes semblent identiques, et la raison semble facile à deviner. Les aiguilles des secondes et des minutes sur une horloge ont la même animation par unité (1/60ème de rotation). Donc, cela revient à la syntaxe à la fin. Si vous le souhaitez, remplacez le nom de la variable minutesOrSeconds par autre chose.

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

En fait, la dernière ligne est différente – assylias

+0

@assylias Bonne prise, je l'ai fixé. – The111

2

Vos méthodes sont presque identiques. Passez simplement un autre argument qui prendra la valeur csecond ou cminute selon le cas.

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

Essayez cela, il suffit de passer le point de vue en tant que paramètre, csecond et 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); 
} 

Espérons que ça aide.

0

J'ai examiné vos 2 méthodes et n'ai trouvé aucune différence sauf le nom de l'argument. Si j'ai raison, il suffit d'appeler cette méthode run(int time) et de remplacer tous les endroits où vous utilisez minutes et seconds par heure. Mentionnez dans javadoc que le temps peut être soit en minutes ou en secondes.

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); 
} 
Questions connexes