38

非常に単純なアルファアニメーションを実行したいのですが、有効な方法が見つかりません。

アイデアは、ビュー上でこのアニメーションを実行することです。

  1. 1秒の0から1までのアルファ
  2. アルファを1で5秒間保持します
  3. 1秒の1から0までのアルファ
  4. アルファを0で5秒間保持します。
  5. 1からやり直してください。

私はAnimationSetでそれを次のように実装しようとしました:

AnimationSet animationSet = new AnimationSet(true);

Animation animation1 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
animation1.setDuration(1000);

Animation animation2 = new AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animation2.setDuration(1000);
animation2.setStartOffset(5000);

Animation animation3 = new AlphaAnimation(0.0f, 0.0f);
animation3.setDuration(4000)
animation3.setStartOffset(6000);

animationSet.add(animation1);
animationSet.add(animation2);
animationSet.add(animation3);

等..

しかし、3番目のアニメーションがすべてのアルファアニメーションを混乱させることは明らかです。これにより、Androidがこのタイプのアニメーションを管理する方法に内部的な矛盾が生じると思います。

何か案が?

ありがとうございました。

4

3 に答える 3

109

これを解決するには、これらの2つのポイントに注意してください


  • 1.0f to 0.0f1 秒のアニメーション持続時間で 5 秒後にアニメーション化する場合、これは最終的に 5 秒の一時停止を伴う 1 秒のアニメーションになります。

    これを達成するには:

    1. setDuration(1000)(持続時間は1秒です)
    2. setStartOffset(5000)(5秒後に始まります)

  • 永久にループする 2 つのアニメーションだけが必要です。

    1. 0.0f to 1.0f5 秒間の一時停止と 1 秒間の持続時間

    2. 1.0f to 0.0f5 秒間の一時停止と 1 秒間の持続時間


コードは次のとおりです。

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    textView.startAnimation(animation1);

AnimationListenerただし、repeatCount にはバグがあるため、永久にループするには使用します。

    animation1 = new AlphaAnimation(0.0f, 1.0f);
    animation1.setDuration(1000);
    animation1.setStartOffset(5000);

    //animation1 AnimationListener
    animation1.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation2 when animation1 ends (continue)
            textView.startAnimation(animation2);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    animation2 = new AlphaAnimation(1.0f, 0.0f);
    animation2.setDuration(1000);
    animation2.setStartOffset(5000);

    //animation2 AnimationListener
    animation2.setAnimationListener(new AnimationListener(){

        @Override
        public void onAnimationEnd(Animation arg0) {
            // start animation1 when animation2 ends (repeat)
            textView.startAnimation(animation1);
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

    });

    textView.startAnimation(animation1);
于 2011-08-15T16:18:05.790 に答える
20

これにはもっと簡単な解決策があります。

ビューの状態が GONE であると仮定しましょう。可視性に合わせてアニメーション化するには:

yourView.setVisibility(View.VISIBLE);
yourView.animate().alpha(1).setDuration(300);

同じ方法で、アニメーション リスナーを追加できます。

これは、スケールと移動のアニメーションでも機能します。

于 2016-08-30T17:40:27.690 に答える