安卓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);
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值