2

アプリに問題があります。アニメーションが繰り返されるときに ImageView を変更したいのですが、 (クラスonAnimationRepeatからの) 関数が呼び出されません。AnimationListener

これはそれを再現するためのサンプルです:

  • 新規プロジェクト「test」を作成(Android 2.1、API 7)(com.test.testパッケージ名に使用)
  • フォルダーanim内にresフォルダーを作成fade_in.xmlし、これらの行で作成します

fade_in.xml

活動コード:

package com.test.test;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class TestActivity extends Activity {

    Button button = null;
    ImageView image = null;

    Animation animation = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button)findViewById(R.id.button1);
        image = (ImageView)findViewById(R.id.imageView1);

        animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        animation.setAnimationListener(new AnimationListener() {            

            int repeatNumber = 0;

            @Override
            public void onAnimationStart(Animation animation) {Log.i("start", "start");}

            @Override
            public void onAnimationEnd(Animation animation) {Log.i("end", "end");}

            @Override
            public void onAnimationRepeat(Animation animation) {
                repeatNumber++;
                Log.i("repeat", String.valueOf(repeatNumber));
                switch(repeatNumber)
                {
                case 1:image.setColorFilter(Color.RED, Mode.MULTIPLY);break;
                case 2:image.setColorFilter(Color.BLUE, Mode.MULTIPLY);break;
                case 3:image.setColorFilter(Color.YELLOW, Mode.MULTIPLY);break;
                default:break;
                }
            }
        });

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {image.startAnimation(animation);}
        });

    }
}

そしてレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

色が変わらない…手伝ってImageViewくれる?

4

3 に答える 3

3

メソッドAnimationUtils.loadAnimation()は Animation の配列を返します。

この配列のアイテムにイベントを設定する必要があります。

AnimationSet を宣言できます

AnimationSet animation = null;

なので、指導後

animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);

あなたは書ける :

    for (Animation a : animation.getAnimations())
        a.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.i("repeat", "repeat");
            }

            @Override
            public void onAnimationEnd(Animation animation) {}
        });

これは機能します。

于 2012-12-10T20:38:58.283 に答える
0

アニメーションを繰り返すには、アニメーションの繰り返しモードと繰り返し回数を設定する必要があります。

anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(5);
于 2012-12-08T11:50:10.307 に答える
0

私は同じ問題に直面しており、多くのリソースとアニメーションの問題の回答を読みました。誰もが AnimationSet と繰り返し回数の問題について不平を言っていました。そのため、XML からタグを削除したところ、機能しました。OnRepeat と onEnd の両方が呼び出されるようになりました。

于 2013-05-29T11:08:49.003 に答える