2012-12-05 4 views
0

Я хочу совместить методы runSec и runMin перемещая руки одним способом. Эти методы перемещают минутную и секундную стрелки на лицевой стороне часика, спасибо.Как совместить подобные методы в одном

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

Ваши методы уже почти идентичны. Просто перейдите в другой аргумент, который будет принимать значение csecond или cminute в зависимости от ситуации.

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) и замените все места, где вы используете minutes и seconds по времени. Упоминание в 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); 
} 
Смежные вопросы