美文网首页
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 旋转动画实现抽奖转盘

    项目要求做一个类似转盘抽奖的东西出来,还要求转的过程中要变速,变就变吧,要求阶段函数样式的变速。。。。想了好多种办...

  • Android超简单实现九宫格抽奖

    目录 前言 如果有小伙伴想实现转盘抽奖效果的话请看我的另一篇文章《Android超简单实现自定义抽奖转盘效果》 效...

  • 随时开始随时结束的抽奖转盘

    公司要实现点击抽奖开始动画,结果返回后缓慢停止到抽奖区域,在网上Google出来的大部分转盘demo都是等抽奖结果...

  • [Nipuream] Android抽奖转盘的实现

    「原文链接」 Nipuream 的博客地址 序言 效果如下: 实现的效果还不错,因为是模拟器加录制,画面可能会有些...

  • iOS 抽奖转盘动画

    用法: 先添加指针视图,转盘背景和开始按钮。按钮的点击事件里设置开始动画,在动画开始的代理方法中让开始按钮不响应点...

  • android 卡通片资源Tween动画

    android 动画资源Tween动画 tween动画 实现目标对象的变换,如移动、旋转、色彩变换、拉伸等。 xm...

  • android自定义View索引

    一:加载动画 1:android仿qq下拉刷新旋转白条加载动画 2:android常用旋转线条加载动画 ...

  • Scratch之Android的Animation动画的四种动画

    旋转动画展示 Android游戏开发Animation动画中的旋转动画 RotateAnimation旋转动画 1...

  • JS实现抽奖转盘

    超级简单的原理:点击转盘指针后随机得到一个数(每个数字对应一个奖项),并确定每个奖项在轮盘上的大概角度,然后调用 ...

  • canvas实现转盘抽奖

    用canvas写了一个简单的转盘抽奖插件, 给大家参考下下。。。 做的时候的想法是,通过传进来的标签以及属性,直接...

网友评论

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

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