0

我正在尝试做一些非常基本的事情。我只是想在单击按钮时从 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);
                    }
                }
            } ); 

        }

    }
4

5 回答 5

0

解决方案:

spin.xml与您的 xml 保持相同。
在按钮的OnClickListener.

ImageView iv = (ImageView) findViewById(R.id.imageView1);
Animation an = AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);
iv.startAnimation(an);
于 2012-06-27T19:50:02.663 回答
0

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

此外,您可以使用 NineOldAndroids 将优越得多的 ViewPropertyAnimator 方法反向移植到 pre-Honeycomb。

于 2013-06-25T20:30:47.993 回答
0

您是否尝试过将 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" />
于 2014-04-12T00:09:32.347 回答
0

尝试这个

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);
                    }
                }
            } ); 
于 2012-06-27T18:28:26.660 回答
0

使用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);
于 2012-06-27T18:41:48.423 回答