美文网首页
Android 旋转动画实现抽奖转盘

Android 旋转动画实现抽奖转盘

作者: 5e1b18effb55 | 来源:发表于2016-10-19 15:35 被阅读0次

    项目要求做一个类似转盘抽奖的东西出来,还要求转的过程中要变速,变就变吧,要求阶段函数样式的变速。。。。
    想了好多种办法,写好几段旋转动画拼凑成一圈,然后放到 AnimatorSet中,循环播放这个AnimatorSet,但是找了半天也没找到循环方式,于是在回调中重新开启了一下动画集

    ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(image, "rotation", 0f, 90f).setDuration(500);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(image, "rotation", 90f,180f).setDuration(2000);
    ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(image, "rotation", 180f,270f).setDuration(3000);
    ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(image, "rotation", 270f,360f).setDuration(500);
    final AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playSequentially(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4);
    animatorSet.setInterpolator(new LinearInterpolator());
    //周期间不停顿
    animatorSet.start();
    animatorSet.addListener(new Animator.AnimatorListener() {
        @Override    
        public void onAnimationStart(Animator animation) {}
        @Override    
        public void onAnimationEnd(Animator animation) {
          animatorSet.start();    
        }
        @Override    
        public void onAnimationCancel(Animator animation) {}
        @Override    
        public void onAnimationRepeat(Animator animation) {}
    });
    

    可是这样就不能随时停止动画了。。。。

    那么就只写一个动画,控制速度就好了,但事实是Interpolator回调方法里的参数并不是旋转的速度,而是一个与时间有关的参数,而返回值是旋转的位置。。。。

    那么问题来了,我需要根据参数,自己计算出需要的返回值,经过操作,行不通,好吧,无爱了

    不想说话了,直接上代码吧

    Demo.java

    import android.animation.ObjectAnimator;
    import android.animation.TimeInterpolator;
    import android.animation.ValueAnimator;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    /**
     * Created by jiangtao on 2016/8/16.
     */
    public class Demo extends Activity {
        private ImageView image;    
        private float currentPoint;    
        private Button btn;    
        private ObjectAnimator objectAnimator = null;    
        @Override    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.activity_demo);        
            btn = (Button) findViewById(R.id.btn);        
            btn.setOnTouchListener(new View.OnTouchListener() {
                  @Override            
                  public boolean onTouch(View v, MotionEvent event) {
                      switch (event.getAction()) {
                          case MotionEvent.ACTION_DOWN:
                              objectAnimator.start();                        
                          break;                   
                          case MotionEvent.ACTION_UP:
                              Log.e("TAG", "" + objectAnimator.getAnimatedFraction() + "," + objectAnimator.getCurrentPlayTime() + "," + objectAnimator.getDuration());
                              //                        objectAnimator.pause();                        
                              Log.e("TAG", "" + objectAnimator.getAnimatedFraction() % 1.0f);
                              currentPoint = objectAnimator.getAnimatedFraction() % 1.0f;
                              objectAnimator.cancel();
                              if (currentPoint == 0.25 || currentPoint == 0.5 || currentPoint == 0.75 || currentPoint == 0) {Log.e("TAG","金奖"); 
                                  Toast.makeText(Demo.this, "卧槽,真的假的?!!", Toast.LENGTH_SHORT).show();
                              }else if (currentPoint > 0.245 && currentPoint < 0.255
                                    || currentPoint > 0.495 && currentPoint < 0.505
                                    || currentPoint > 0.745 && currentPoint < 0.755) {
                                  Log.e("TAG","银奖");
                                  Toast.makeText(Demo.this, "你中了~", Toast.LENGTH_SHORT).show();
                              }else {
                                  Toast.makeText(Demo.this, "是你手残,不要怪我~", Toast.LENGTH_SHORT).show();
                              }break;
                      }
                      return false; 
                }
            });
            image = (ImageView) findViewById(R.id.img_view);
            /** 设置旋转动画 */
            objectAnimator = ObjectAnimator.ofFloat(image, "rotation", 0f, 359f).setDuration(500);
            /** 常用方法 */
            objectAnimator.setRepeatCount(-1);
            //设置重复次数
            objectAnimator.setInterpolator(new MyInterpolator());//周期间不停顿
            objectAnimator.setRepeatMode(ValueAnimator.INFINITE);//动画重复模式
            //        animation.setFillAfter(true);
            //动画执行完后是否停留在执行完的状态
            //animation.setStartOffset(long startOffset);
            //执行前的等待时间
        }
      public class MyInterpolator implements TimeInterpolator {
          @Override
          public float getInterpolation(float input) {
          //            Log.e("TAG", "getInterpolation: " + input);
                  float mTension = 2.0f; 
    //              return input * input * ((mTension + 1) * input + mTension) + 1.0f;
    //              return (float)(Math.sin(2 * 1 * Math.PI * input));
                  return (float) (Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    //              return input;
          }
      }
    }
    

    activity_demo.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/disk" />
        <ImageView
            android:id="@+id/img_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/start" />
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="bottom" />
    </RelativeLayout>
    

    是不是感觉很乱?那就对了,不乱我贴给你看?用个屏幕大点的,横屏看吧。。。反正基本抽奖功能实现了。。。。

    贴代码还是很还看的:)

    相关文章

      网友评论

          本文标题:Android 旋转动画实现抽奖转盘

          本文链接:https://www.haomeiwen.com/subject/vcyxuttx.html