2017-05-17 1 views
1

을 흔들어 나는이 코드를 작성하지만, 할 수 있다는 것을 나에게 보인다 쉽게애니메이션 세트, 내가 애니메이션 내보기를 흔들려고 효과

var animatorSet = AnimatorSet() 
    var objectRotateAnimator = ObjectAnimator.ofFloat(shake, "rotation", -5f, 5f) 
    objectRotateAnimator.apply { 
     repeatMode = ValueAnimator.REVERSE 
     repeatCount = ValueAnimator.INFINITE 
     duration = 70 
     interpolator = LinearInterpolator() 
    } 

    var objectTranslateAnimator = ObjectAnimator.ofFloat(shake, "translate", -5f, 5f) 
    objectTranslateAnimator.apply { 
     repeatMode = ValueAnimator.REVERSE 
     repeatCount = ValueAnimator.INFINITE 
     duration = 70 
     interpolator = LinearInterpolator() 
    } 

    start_shake.setOnClickListener { 
     animatorSet.play(objectRotateAnimator).with(objectTranslateAnimator) 
     animatorSet.start() 
    } 

내가 더 간단하게 할 수있는 방법은?

답변

1

당신은 중복 줄이기 위해 일부 기능과 필드를 추출 할 수 있습니다 : 좋은 방법 이름이 중간 변수에 대한 필요성을 제거했습니다

start_shake.setOnClickListener { 
    AnimatorSet().apply { 
     play(shakeAnimator("rotation")).with(shakeAnimator("translate")) 
     start() 
    } 
} 

: 좋아 그럼 그냥 모양

private val linearInterpolator = LinearInterpolator() 

private fun shakeAnimator(propertyName: String) = 
     ObjectAnimator.ofFloat(shake, propertyName, -5f, 5f).apply { 
      repeatMode = ValueAnimator.REVERSE 
      repeatCount = ValueAnimator.INFINITE 
      duration = 70 
      interpolator = linearInterpolator 
     } 

을 코드를 더 짧게했다.