我的应用程序有一个问题:当动画被重复时,我想修改一个ImageView,但是没有调用onAnimationRepeat函数(来自AnimationListener类)。
这是一个复制它的样本:
com.test.test作为包名)anim文件夹中创建一个文件夹res,并使用以下行创建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的颜色不change...Can你帮我吗?
发布于 2012-12-10 20:38:58
方法AnimationUtils.loadAnimation()返回一个动画数组。
您必须在此数组的项上设置事件。
您可以声明一个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-08 11:50:10
对于重复动画,您需要设置动画的重复模式和重复出现的次数。
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(5);发布于 2013-05-29 11:08:49
我面对同样的问题,阅读了很多资源和动画问题的答案。每个人都在抱怨AnimationSet和重复计数问题。因此,我从XML中删除了标记,它起了作用。OnRepeat和onEnd现在都在接到电话。
https://stackoverflow.com/questions/13777039
复制相似问题