1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kotlin + android.animation で矢印をヒュンヒュン動かす

Last updated at Posted at 2018-05-09

AnimatorSet とか android.animation とかを使ってKotlin で矢印を動かしてみました

コード


import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AnimationUtils
import android.widget.ImageView

fun setTranslateArrow(right: ImageView, left: ImageView): AnimatorSet {

    val moveCloser = AnimatorSet()
    val rightMove = ObjectAnimator
            .ofFloat( right, "translationX", -40f, 20f)
    val  leftMove = ObjectAnimator
            .ofFloat(  left, "translationX", 40f, -20f)

    rightMove.repeatCount  = ObjectAnimator.INFINITE
     leftMove.repeatCount  = ObjectAnimator.INFINITE
    rightMove.duration     = 700
     leftMove.duration     = 700
    rightMove.interpolator = AccelerateDecelerateInterpolator()
     leftMove.interpolator = AccelerateDecelerateInterpolator()
    rightMove.repeatMode   = ObjectAnimator.REVERSE
     leftMove.repeatMode   = ObjectAnimator.REVERSE

    moveCloser.playTogether(rightMove, leftMove)
    return moveCloser
}

private val _moveCloser by lazy 
       { setTranslateArrow(_rightImageView, _leftImageView) }
...
_movecloser.start

なぜAnimatorを使うのか

android.animator (Property Animator) は以下のような利点があります

  • 軽い
  • API Level 11 から使える(現実的)
  • 記述が簡単

デメリットとしては

  • 止めて表示など細かい制御が効かない
  • 既定のアニメーション以外は難しい

まあ使えるものを使えということですね。
使えるアニメーションとAPIはAndroidのアニメーション処理に関して - Qiita@glayashによくまとまっています。

Kotlin だから難しかったこと

ありません。 むしろgetter/setter の記述簡素化とby lazy の利便性でコーディングがはかどること間違いなし

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?