如何将类似的方法合二为一

时间:2012-12-05 10:49:19

标签: java android refactoring

我想在一种方法中结合移动手的方法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);
}

7 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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;
}

答案 2 :(得分: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);

但你可以连接两个这样的方法 -

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

答案 3 :(得分: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);
    }
}

答案 4 :(得分: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);
}

希望有所帮助。

答案 5 :(得分:0)

我检查了你的2个方法,除了参数名称之外没有发现任何差异。如果我是对的,只需调用此方法run(int time)并按时间替换所有使用minutesseconds的地方。在javadoc中提到时间可能是几分钟或几秒钟。

答案 6 :(得分: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);
}