首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓(API 7) -动画: onAnimationRepeat

安卓(API 7) -动画: onAnimationRepeat
EN

Stack Overflow用户
提问于 2012-12-08 11:30:24
回答 3查看 4.8K关注 0票数 2

我的应用程序有一个问题:当动画被重复时,我想修改一个ImageView,但是没有调用onAnimationRepeat函数(来自AnimationListener类)。

这是一个复制它的样本:

  • 创建一个新项目"test“(Android2.1,API 7) (我使用com.test.test作为包名)
  • anim文件夹中创建一个文件夹res,并使用以下行创建fade_in.xml

活动代码:

代码语言:javascript
复制
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);}
        });

    }
}

以及布局:

代码语言:javascript
复制
<?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你帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-10 20:38:58

方法AnimationUtils.loadAnimation()返回一个动画数组。

您必须在此数组的项上设置事件。

您可以声明一个AnimationSet

代码语言:javascript
复制
AnimationSet animation = null;

所以在指示之后

代码语言:javascript
复制
animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);

你可以写:

代码语言:javascript
复制
    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) {}
        });

这会管用的。

票数 3
EN

Stack Overflow用户

发布于 2012-12-08 11:50:10

对于重复动画,您需要设置动画的重复模式和重复出现的次数。

代码语言:javascript
复制
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(5);
票数 0
EN

Stack Overflow用户

发布于 2013-05-29 11:08:49

我面对同样的问题,阅读了很多资源和动画问题的答案。每个人都在抱怨AnimationSet和重复计数问题。因此,我从XML中删除了标记,它起了作用。OnRepeat和onEnd现在都在接到电话。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13777039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档