欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android自定义view实现圆周运动

时间:2021-10-06 09:08:31|栏目:Android代码|点击:

本文实例为大家分享了android自定义view实现圆周运动的具体代码,供大家参考,具体内容如下

思想

自定义Animation,自己定义半径,相当于原来控件的位置为(0,0),按照每个角度区间,计算新的位置,跟着时间变动

逆时针转动

public class VenusCircleAnimation extends Animation {

  private int radii;
  public VenusCircleAnimation(int radii) {
    this.radii = radii;
  }

  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
    //根据取值范围 确定圆周运动的角度范围。360-0
    float d = 360 * interpolatedTime;//interpolatedTime 取值范围 0-1,表示时间
    if (d > 360) { //算法二
      d = d-360;
    }
    int[] ps = getNewLocation((int) d, radii);//
    t.getMatrix().setTranslate(ps[0], ps[1]);
  }

  public int[] getNewLocation(int newAngle, int r) {
    int newAngle1;
    int newX = 0, newY = 0;
    if (newAngle >= 0 && newAngle <= 90) {
      // Math.PI/180得到的结果就是1°,然后再乘以角度得到角度
      newX = (int) ( - (r * Math.cos(newAngle * Math.PI / 180)));
      newY = (int) (r * Math.sin(newAngle * Math.PI / 180));
    } else if (newAngle >= 90 && newAngle <= 180) {// 90-180
      newAngle1 = 180 - newAngle;
      newX = (int) (r * Math.cos(newAngle1 * Math.PI / 180));
      newY = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
    } else if (newAngle >= 180 && newAngle <= 270) {//180-270
      newAngle1 = 270 - newAngle;
      newX = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
      newY = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
    } else if (newAngle >= 270) {//270-360
      newAngle1 = 360 - newAngle;
      newX = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
      newY = (int) ( - (r * Math.sin(newAngle1 * Math.PI / 180)));
    }
    return new int[]{newX, newY};
  }

}

顺时针

public class CircleAnimation extends Animation {

  private int radii;

  public CircleAnimation(int radii) {
    this.radii = radii;
  }

  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
    float d = 360 * interpolatedTime ;
    if (d > 360) {
      d = d - 360;
    }
    int[] ps = getNewLocation((int) d, radii);//
    t.getMatrix().setTranslate(ps[0], ps[1]);
  }


  public int[] getNewLocation(int newAngle, int r) {
    int newAngle1;
    int newX = 0, newY = 0;
    if (newAngle >= 0 && newAngle <= 90) {
      newX = (int) (r * Math.sin(newAngle * Math.PI / 180));
      newY = (int) ( - (r * Math.cos(newAngle * Math.PI / 180)));
    } else if (newAngle >= 90 && newAngle <= 180) {// 90-180
      newAngle1 = 180 - newAngle;
      newX = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
      newY = (int) (r * Math.cos(newAngle1 * Math.PI / 180));
    } else if (newAngle >= 180 && newAngle <= 270) {//180-270
      newAngle1 = 270 - newAngle;
      newX = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
      newY = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
    } else if (newAngle >= 270 && newAngle <= 360) {//270-360
      newAngle1 = 360 - newAngle;
      newX = (int) ( - (r * Math.sin(newAngle1 * Math.PI / 180)));
      newY = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
    }
    return new int[]{newX, newY};
  }
}

使用

CircleAnimation animationw = new CircleAnimation(m);
 animationw.setDuration(d);
 animationw.setRepeatCount(-1);
 animationw.setInterpolator(new LinearInterpolator());
 imageView.startAnimation(animationw);

上一篇:Android Studio 3.5格式化布局代码时错位、错乱bug的解决

栏    目:Android代码

下一篇:Android布局之LinearLayout自定义高亮背景的方法

本文标题:android自定义view实现圆周运动

本文地址:http://www.codeinn.net/misctech/178907.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有