Android(API 7)-动画:onAnimationRepeat
我的应用程序有一个问题:我想在重复动画时修改ImageView,但没有调用Android(API 7)-动画:onAnimationRepeat,android,Android,我的应用程序有一个问题:我想在重复动画时修改ImageView,但没有调用onAnimationRepeat函数(来自AnimationListener类) 这是一个复制它的示例: 创建一个新的项目“test”(安卓2.1,API 7)(我使用com.test.test作为包名) 在res文件夹内创建一个文件夹anim,并用这些行创建fade_in.xml 活动代码: package com.test.test; import android.app.Activity; import a
onAnimationRepeat
函数(来自AnimationListener
类)
这是一个复制它的示例:
- 创建一个新的项目“test”(安卓2.1,API 7)(我使用
作为包名)com.test.test
- 在
文件夹内创建一个文件夹res
,并用这些行创建anim
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
颜色不变……您能帮我吗?要重复动画,您需要设置动画的重复模式和重复次数
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(5);
方法
AnimationUtils.loadAnimation()
返回一个动画数组
必须在此数组的项上设置事件
可以声明动画集
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) {}
});
这将起作用。我也面临同样的问题,并阅读了大量资源和动画问题答案。每个人都在抱怨动画集和重复计数问题。因此,我从XML中删除了标记,它成功了。OnRepeat和onEnd现在都被调用。请检查计数器变量值,可能它不会重置log.i(“repeat”,String.valueOf(repeatNumber));=>它从未出现在logcat中。repeatNumber仅为示例创建。我尝试使用代码隐藏而不是xml文件,它不会改变任何东西。android:repeatCount=“3”在xml文件中工作,但侦听器中的onAnimationRepeat函数未被调用。animation=(AnimationSet)AnimationUtils.loadAnimation(这是R.anim.fade_in);我确认,它工作得非常完美(我会将您的解决方案提交给Ed Lalouche!)。非常感谢你!