Androidアニメーション(2)アニメーションコレクション

1.アニメーションコレクション

クラス:

  • AnimationSet
    関数は脆弱であり、セット内のアニメーションを特定の順序で編成して実行するようにカスタマイズすることはできません。
  • AnimatorSet

どちらも重複機能を設定しません。


1、AnimationSet

使用:

class MainActivity : AppCompatActivity() {
    var TAG = "Animation"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button = findViewById<Button>(R.id.btn)
        val animationSet = AnimationSet(true) //true—让这些动画共用AnimationSet设置插值器
        val alphaAnimation = AlphaAnimation(0f, 1f)
        alphaAnimation.duration = 3000
        val rotateAnimation = RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.duration = 3000
        alphaAnimation.repeatCount = Animation.INFINITE
        rotateAnimation.repeatCount = Animation.INFINITE
        animationSet.addAnimation(alphaAnimation)
        animationSet.addAnimation(rotateAnimation)
        animationSet.interpolator = AccelerateDecelerateInterpolator()
        button.startAnimation(animationSet)
    }
}

ループが設定されていると機能しないため、AnimationSetではループを設定できないことに注意してください。
ここに画像の説明を挿入


2、AnimatorSet

AnimatorSetにアニメーションを追加するには2つの方法があります。

  • AnimatorSetのplayTogether(Animator [])またはAnimatorSetのplaySequentially(Animator [])メソッドを呼び出して、アニメーションのセットを一度に追加できます
  • AnimatorSet.Builderクラスのメソッドと組み合わせてAnimatorSetのplay(Animator)メソッドを使用して、アニメーションを1つずつ追加することもできます。

AnimatorSetでループを設定する方法はまだありません
さあ、
playTogether(Animator [])
ここに画像の説明を挿入
はアニメーションを一緒に開始させますが、プロセスと結果は各アニメーションによって定義されます

class MainActivity : AppCompatActivity() {
    var TAG = "Animation"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun click(view: View) {
        val animatorSet = AnimatorSet()
        val button = findViewById<Button>(R.id.btn)
        val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation= ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
        animatorSet.playTogether(backgroundColorAnimator, rotation)
        animatorSet.duration = 3000
        animatorSet.start()
    }
    fun click2(view: View) {
        val animatorSet = AnimatorSet()
        val button2 = findViewById<Button>(R.id.btn2)
        val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation= ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
        animatorSet.playTogether(backgroundColorAnimator, rotation)
        backgroundColorAnimator.duration = 3000
        rotation.duration = 6000
        animatorSet.start()
    }
}

ここに画像の説明を挿入
最初に、2番目のボタンのアニメーションごとに異なる実行時間を設定します

以下のofInt設定色とofArgbの違いがわかります

  • ofIntは、開始色からターゲット色まで少しずつ累積されます。
  • ofArgbは、通常psで設定するグラデーション効果の設定として理解できます。

playSequentially(Animator [])
順次実行
ここに画像の説明を挿入

class MainActivity : AppCompatActivity() {
    var TAG = "Animation"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun click(view: View) {
        val animatorSet = AnimatorSet()
        val button = findViewById<Button>(R.id.btn)
        val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation= ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
        animatorSet.playSequentially(backgroundColorAnimator, rotation)
        animatorSet.duration = 3000
        animatorSet.start()
    }
    fun click2(view: View) {
        val animatorSet = AnimatorSet()
        val button2 = findViewById<Button>(R.id.btn2)
        val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation= ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
        animatorSet.playSequentially(backgroundColorAnimator, rotation)
        backgroundColorAnimator.duration = 3000
        rotation.duration = 6000
        animatorSet.start()
    }
}

ここに画像の説明を挿入
2つのボタン効果により、
       AnimatorSetで期間が設定されている場合、各アニメーションは期間の期間であり、合計期間:n *期間

playSequentiallyループの問題については、上記の例を参考にしてください。最初のアニメーションで無限ループを設定すると、2番目のアニメーションは実行されず、アニメーションでの継続時間の設定も無効になります。手動でキャンセルしない限り

AnimatorSet.Builder
ビルダーは、
一般的に使用される3つの関数です。

  • 一般的な使用法:animatorSet.play().with().before().after()
  1. アニメーション付き
  2. before 先行
  3. playはAnimatorSet.Buliderオブジェクトを返します

それでは、どのアニメーションシーケンスですか?覚えておいてください:

  • Bが実行された後にBAが実行された後
  • Bが実行される前にBAが実行される

確認してください:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun click(view: View) {
        val animatorSet = AnimatorSet()
        val button = findViewById<Button>(R.id.btn)
        val backgroundColorAnimator = ObjectAnimator.ofInt(button, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation = ObjectAnimator.ofFloat(button, "Rotation", 0f, 360f)
        animatorSet.duration = 3000
        animatorSet.play(backgroundColorAnimator).before(rotation)
        animatorSet.start()
    }
    fun click2(view: View) {
        val animatorSet = AnimatorSet()
        val button2 = findViewById<Button>(R.id.btn2)
        val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation = ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
        backgroundColorAnimator.duration = 3000
        rotation.duration = 6000
        animatorSet.play(backgroundColorAnimator).after(rotation)
        animatorSet.start()
    }
}

ここに画像の説明を挿入


これらの複雑なアニメーションシーケンスを繰り返し再生するにはどうすればよいですか?以下のコードを見てください:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun click2(view: View) {
        val animatorSet = AnimatorSet()
        val button2 = findViewById<Button>(R.id.btn2)
        val backgroundColorAnimator = ObjectAnimator.ofArgb(button2, "BackgroundColor", Color.WHITE, Color.BLACK, Color.WHITE)
        val rotation = ObjectAnimator.ofFloat(button2, "Rotation", 0f, 360f)
        backgroundColorAnimator.duration = 3000
        rotation.duration = 6000
        animatorSet.play(backgroundColorAnimator).after(rotation)
        animatorSet.addListener(object : Animator.AnimatorListener{
            override fun onAnimationRepeat(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                animation?.start()
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }
        })
        animatorSet.start()
    }
}

ここに画像の説明を挿入

アニメーションの実行が完了したら、
再度開きます。ここでは、onAnimationEndがアニメーションを渡していますが、すべてのアニメーションを意味しているわけではなく、人間の家の各アニメーションによって合成された大きなアニメーションなので、面倒ではありません。
次のセクションでは、補間推定器とofObject()の使用法について説明
します。

元の記事を公開16件 ・いい ね0 訪問数249

おすすめ

転載: blog.csdn.net/weixin_43860530/article/details/105346998