【问题标题】:Easing effect for rotate animation in androidandroid中旋转动画的缓动效果
【发布时间】:2014-08-25 14:24:03
【问题描述】:

我正在为轮盘游戏制作旋转动画,因为我希望轮子像真正的旋转一样旋转(例如,轮子旋转速度应该缓慢开始,然后快速并缓慢停止)

我知道我应用的基本旋转动画,下面是我的动画 XML 文件代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="200"
    android:repeatMode="restart"
    android:repeatCount="-1"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>  

在活动中:

public class RouletteActivity extends Activity {

Button button_spin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_roulette);
   button_spin = (Button)findViewById(R.id.btn_spin);
   button_spin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            StartLoading();

            Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
                public void run() {
                    StopLoading();
                } 
            }, 5000);

        }
    });
}

 public void StartLoading() {
    ImageView img_wheel = (ImageView) this.findViewById(R.id.imageView1);
    img_wheel.setImageDrawable(getResources().getDrawable(R.drawable.wheel));
    Animation rotateLoading = AnimationUtils.loadAnimation(this, R.anim.rotate);
    img_wheel.clearAnimation();
    img_wheel.setAnimation(rotateLoading);
}


 public void StopLoading() {
    ImageView img_wheel = (ImageView) this.findViewById(R.id.imageView1);
    if (img_wheel.getAnimation() != null)
    {
        img_wheel.clearAnimation();
        img_wheel.setImageDrawable(getResources().getDrawable(R.drawable.wheel));

    }
}

请帮我实现我想要的动画,谢谢。

【问题讨论】:

    标签: android animation rotation android-animation easing


    【解决方案1】:

    您可以通过应用 Interpolator 来修改动画的行为。
    AccelerateDecelerateInterpolator 似乎是这里最好的。

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="200"
        android:repeatMode="restart"
        android:repeatCount="-1"
        android:pivotX="50%"
        android:pivotY="50%">
    </rotate>
    </set>
    

    或者更简单:

    yourView.animate().rotation(value).setInterpolator(new AccelerateDecelerateInterpolator()).start();
    

    【讨论】:

    • 是的!尝试移动它而不是旋转它(x(value) 方法)
    • 可以给代码吗?我不知道该怎么做。
    • 检查我编辑的答案。代码在底部,或者这里yourView.animate().rotation(value).setInterpolator(new AccelerateDecelerateInterpolator()).start();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多