🔰 [Kotlin] 基礎 androidstudio アニメーション


「作って楽しむプログラミング Androidアプリ超入門」の勉強をまとめていきます。

環境

java: 1.8.0_281
kotlin:1.5.10

今回はアニメーションについてです。

androidのアニメーション

androidでは、Tweenアニメーションと、Frameアニメーションがあります。

Tweenアニメーション:1つの画像そのものを変化させる。具体的には、画像の移動、フェード、回転、拡大縮小など

Frameアニメーション:少しずつ異なる複数の画像を順番に表示させるアニメーション。パラパラ漫画みたいな。

今回はTweenアニメーションについて見て行きます。
どのアニメーションも「開始時の値」 「終了時の値」 「何ミリ秒かけて移動するか」などの項目の設定が必要である。

クラス名 概要
AlphaAnimation 透過性を変化させる
RotateAnimation 角度を変化させる
ScaleAnimation 大きさを変化させる
TranslateAnimation 位置を変化させる
AnimationSet 複数のアニメーションを合成する

サンプルコード

sample.kt
      //0.1秒の移動を3往復(最初の1回+繰り返しの5回)設定したい
        fun onButtonClick(v:View) {
      //ウィジェットの移動
        val translate = TranslateAnimation(0f,0f,0f,-200f)
      //繰り返しモード
      translate.repeatMode = Animation.REVERSE
        //繰り返し回数
      translate.repeatCount = 5
      //アニメーション時間
        translate.duration = 100
      //アニメーション回転
        val rotate = RotateAnimation(0f, 36f, imageView.width/2f, imageView.height/2f)
        rotate.duration = 200
        //AnimationSetクラスを使ってアニメーションの合成
        val set = AnimationSet(true)
        set.addAnimation(rotate)
        set.addAnimation(translate)

        imageView.startAnimation(set)