FC2ブログ
    01 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.» 03

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【Java】ImageViewのアニメーション 

    こんにちは。大坂です。

    今週も雪予報でしたが、降りませんでしたね。よかった・・・ですが!
    うちは家族がインフルで何やら大変なことになってます・・・。

    さて今週の話題ですが、JavaアプリでのImageViewのアニメーションについてです。
    まぁ使いどころはそれなりにあるのかなということでご紹介をば。

    扱えるアニメーションは以下。
    ・移動【TranslateAnimation】
    ・透明度【AlphaAnimation】
    ・回転【RotateAnimation】
    ・スケール変更【ScaleAnimation】

    と後は上記アニメーションを組み合わせて使用できる【AnimationSet】ですね。

    では簡単に使い方を。

    まずはImageViewの準備

    XmlにImageViewを追加
    <ImageView id="@+id/imageView"
    android:src="@drawable/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    javaコードのXml読み込み後に
    ImageView imageView = (ImageView)findViewById(R.id.imageView);


    といった感じでImageViewを準備します。
    まぁこの辺は基本なのでわかってる方ばかりでしょうが。

    では以下アニメーションの使い方です。
    移動【TranslateAnimation】

    // TranslateAnimation(float fromX, float toX, float fromY, float toY)
    // X方向に10、Y方向に20移動させる
    TranslateAnimation transAnim = new TranslateAnimation(0f, 10f, 0f, 20f);
    // 動作する時間を設定(ms)
    transAnim.setDuration(2000);
    // アニメーションを繰り返す回数を設定。指定しない場合は1回のみ。無限に繰り返す場合は‐1
    transAnim.setInterpolator(new CycleInterpolator(10));
    // 準備したImageViewでアニメーションを開始する。
    imageView.startAnimation(transAnim);



    透明度【AlphaAnimation】

    // AlphaAnimation(float fromAlpha, float toAlpha)
    // Alpha値を1~0.1にする。
    AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.1f);
    // 動作する時間を設定(ms)
    alphaAnim.setDuration(3000);
    // アニメーションを繰り返す回数を設定。指定しない場合は1回のみ。無限に繰り返す場合は‐1
    alphaAnim.setInterpolator(new CycleInterpolator(-1));
    // 準備したImageViewでアニメーションを開始する。
    imageView.startAnimation(alpha);



    回転【RotateAnimation】

    // RotateAnimation(float from, float to, float pivotX, float pivotY)
    // 360度回転。回転の中心X30、Y90の位置
    RotateAnimation rotateAnim = new RotateAnimation(0, 360, 30, 90);
    // 動作する時間を設定(ms)
    rotateAnim.setDuration(1000);
    // 準備したImageViewでアニメーションを開始する。
    imageView.startAnimation(rotate);



    スケール変更【ScaleAnimation】

    // ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
    // XYを2倍にする
    ScaleAnimation scaleAnim = new ScaleAnimation(1,2,1,2, 0, 0);
    // 動作する時間を設定(ms)
    scaleAnim.setDuration(1000);
    // アニメーションを繰り返す回数を設定。指定しない場合は1回のみ。無限に繰り返す場合は‐1
    scaleAnim.setInterpolator(new CycleInterpolator(1));
    // 準備したImageViewでアニメーションを開始する。
    imageView.startAnimation(scale);



    といった感じですね~。ちょいと動かしたい時なんかには使えるんじゃないですかね?

    あと最後に複数のアニメーションを動かせる【AnimationSet】です。

    // AnimationSet(boolean shareInterpolator)
    AnimationSet animSet = new AnimationSet(true);
    // 点滅アニメーションを設定
    animSet.addAnimation(alphaAnim);
    // スケール変化のアニメーションを設定
    animSet.addAnimation(scaleAnim);
    // アニメーションを繰り返す回数を設定。指定しない場合は1回のみ。無限に繰り返す場合は‐1
    animSet.setInterpolator(new CycleInterpolator(1));
    // 準備したImageViewでアニメーションを開始する。
    imageViewstartAnimation(set);



    では今週は以上です~。
    また来週!ノシ

    Category: 開発日記(Java)

    tb 0 : cm 2