Roman
Roman

Reputation: 1055

How to combine similar methods into one

I want to combine the methods runSec and runMin of moving the hands in one method. These methods move the minute and second hands on the clock face Help, thank you.

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

Upvotes: 0

Views: 166

Answers (7)

The111
The111

Reputation: 5867

The methods appear identical, and the reason seems easy to guess. Both the second and minute hands on a clock have the same animation per unit (1/60th of a rotation). So, it just comes down to syntax in the end. If you'd like, substitute, the variable name minutesOrSeconds with something else.

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

Upvotes: 0

Anthbs
Anthbs

Reputation: 184

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

Upvotes: 0

AlexR
AlexR

Reputation: 115328

I examined your 2 methods and have not found any difference except name of the argument. If I am right, just call this method run(int time) and replace all places where you use minutes and seconds by time. Mention in javadoc that time may be either in minutes or in seconds.

Upvotes: 0

Try this, just pass the view as parameter, csecond and 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);
}

Hope that helps.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

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

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200158

Your methods are nearly identical already. Just pass in another argument which will take the value csecond or cminute as appropriate.

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

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Basically you do same things in both method so you can replace with any one-

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

and you can call method like

  runMinorSec(seconds);
  runMinorSec(minutes);

But you can concat two method like this -

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

Upvotes: 0

Related Questions