Android(API 7)-动画 : onAnimationRepeat

标签 android

我的应用程序存在问题:我想在重复动画时修改 ImageView,但未调用 onAnimationRepeat 函数(来自 AnimationListener 类) .

这是重现它的示例:

  • 创建一个新项目“test”(Android 2.1,API 7)(我使用 com.test.test 作为包名称)
  • res 文件夹内创建一个文件夹 anim,并使用这些行创建 fade_in.xml

fade_in.xml

Activity 代码:

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 颜色没有改变...你能帮我吗?

最佳答案

方法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) {}
        });

这会起作用。

关于Android(API 7)-动画 : onAnimationRepeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777039/

相关文章:

Android约束布局: Views not respecting margins

android - 如何将 onClickListener 设置为 kotlin 中的按钮

android - 关于如何从 URI 获取 Exif 数据的最终答案

android - 如果反复启动/停止,为什么 AsyncTask 不会启动?

android - 为什么我不能为同一个包名称创建多个 OAuth 2.0 客户端 ID?

java - 在 WebView 在线表单中填写没有 id 的文本字段

android - 工具栏不接触屏幕的两侧

Android:将额外的 Activity 从一个 Activity 传递到另一个 Activity

java - Pdf Renderer API Android 来自 URL

java - 如何使用自定义按钮(不是 Android 设备按钮)控制音量