我的應用程序出現問題:我想在重複動畫時修改ImageView,但不調用onAnimationRepeat
函數(來自AnimationListener
類)。Android(API 7) - 動畫:onAnimationRepeat
這是一個樣本,以重現:
- 創建一個新項目 「測試」(Android 2.1系統,API 7)(我用
com.test.test
包名稱) - 創建
res
文件夾內的文件夾anim
和創建fade_in.xml
與這些線
的一個ctivity代碼:
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
顏色不會改變......你能幫助我嗎?
請檢查計數器的變量值,也許它不會重置 – Pratik
Log.i(「repeat」,String.valueOf(repeatNumber)); =>它永遠不會出現在logcat中。 repeatNumber僅爲樣本創建。 – user1887611