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

7 回答 7

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);
}
于 2012-12-05T10:53:35.413 回答
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;
}
于 2012-12-05T10:53:51.357 回答
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);
于 2012-12-05T10:52:52.823 回答
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);
    }
}
于 2012-12-05T10:53:28.610 回答
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);
}

希望有帮助。

于 2012-12-05T10:54:18.470 回答
0

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

于 2012-12-05T10:54:32.123 回答
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);
}
于 2012-12-05T10:55:37.067 回答