【问题标题】:Tween animation not working for Android补间动画不适用于 Android
【发布时间】:2014-04-12 09:21:53
【问题描述】:

我正在尝试做一些非常基本的事情。我只是想在单击按钮时从 Android 应用程序的 xml 资源文件运行一个简单的补间动画。运行应用程序时动画不会启动。我要疯了,试图找出原因。

这是 res/anim/spin.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http:schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="360" />

</set>

这是我的活动课:

    package jorge.jorge.jorge;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageView;

    public class Assignment5Activity extends Activity {
        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);



            final Button btnSpin = (Button) findViewById(R.id.button1);
            btnSpin.setText("Start");
            btnSpin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {


                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    Animation an =    AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);

                    iv.startAnimation(an);

                    if (an.hasStarted())
                    {
                        btnSpin.setText("Stop");
                    }
                    else
                    {
                        iv.startAnimation(an);
                    }
                }
            } ); 

        }

    }

【问题讨论】:

  • 还有什么我们应该知道的吗?这是这个类的完整代码吗?
  • 是的。我使用的是这个活动类、spin xml 资源文件和我的主布局文件。

标签: android eclipse animation tween


【解决方案1】:

试试这个

final ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    Animation an =        AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);

 btnSpin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {




                    iv.startAnimation(an);

                    if (an.hasStarted())
                    {
                        btnSpin.setText("Stop");
                    }
                    else
                    {
                        iv.startAnimation(an);
                    }
                }
            } ); 

【讨论】:

    【解决方案2】:

    使用 RotateAnimation,将轴心点设置为图像的中心。

    RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
    anim.setInterpolator(new LinearInterpolator());
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(700);
    
    // Start animating the image
    final ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.startAnimation(anim);
    
    // Later.. stop the animation
    iv.setAnimation(null);
    

    【讨论】:

    • 那也没用。我不确定为什么。您是否想在清单 xml 文件中定义动画资源?
    • @rocklandcitizen 使用此代码,您无需在 xml 中声明动画!如果你想在 xml 中使用动画,把它放在你的 res/anim 文件夹中
    • 我第一篇文章中的原始代码使用了 xml 动画资源,它位于 res/anim 目录中。我在没有请求 xml 资源的情况下尝试了上面的代码(只是按照呈现的方式以编程方式进行)。由于某种原因,这超出了我的范围,它也不起作用。我在想也许我需要以编程方式加载位图资源,而不是在我的 main.xml 布局文件中加载它。虽然理论上这应该不重要。我不知道。
    【解决方案3】:

    解决方案:

    spin.xml 与您的 xml 保持一致。
    在按钮的OnClickListener 中运行此代码。

    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    Animation an = AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);
    iv.startAnimation(an);
    

    【讨论】:

      【解决方案4】:

      给动画一个持续时间。我相信默认情况下它是 0(与默认情况下约为 300 毫秒的属性动画相反)。

      此外,您可以使用 NineOldAndroids 将优越得多的 ViewPropertyAnimator 方法反向移植到蜂窝之前。

      【讨论】:

        【解决方案5】:

        您是否尝试过将 android:toDegrees 从 360 更改为 359?对于 android,0 度与 360 度相同,因为数字序列从 0 而不是 1 开始。所以本质上,在当前设置为 360 的情况下,告诉它从 0 度到 360 度就像告诉它保持原样。

            <rotate
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:repeatCount="infinite"
                android:toDegrees="359" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-22
          相关资源
          最近更新 更多