android 带阻尼的周期运动

public void animate(View view) {
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofFloat("y", view.getY(), view.getY()+70);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, propertyValuesHolder);
        objectAnimator.setInterpolator(new CycleInterpolator(2));
        objectAnimator.setDuration(400);
        objectAnimator.start();
    }

这个是y轴方向的一个周期运动(简谐运动) 

我想让震幅越来越小我重写了CycleInterpolator

public class MyCycleInterpolator extends CycleInterpolator {

    private  float cycles;

    public MyCycleInterpolator(float cycles) {
        super(cycles);
        this.cycles=cycles;
    }

    public MyCycleInterpolator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public float getInterpolation(float input) {
        // return (float)(Math.sin(2 * cycles * Math.PI * (input)));
        return (float)(Math.sin(2 * cycles * Math.PI * (input)))*(1f-input);
    }
}
根据简谐运动公式 
android 带阻尼的周期运动_第1张图片

我根据 时间因子 input确定 振幅 A=(1-input) 越来越小

实现了有阻尼上下震动的效果

  public void animateY(View view) {
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofFloat("y", view.getY(), view.getY()+70);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, propertyValuesHolder);
        objectAnimator.setInterpolator(new MyCycleInterpolator(2));
        objectAnimator.setDuration(400);
        objectAnimator.start();
    }

还有 放大缩小的跟果冻似的效果

    public void animateScale(View view){
        PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("scaleX",1f,1.5f);
        PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("scaleY",1f,1.5f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, propertyValuesHolder1,propertyValuesHolder2);
        objectAnimator.setInterpolator(new MyCycleInterpolator(2));
        objectAnimator.setDuration(400);
        objectAnimator.start();
    }


你可能感兴趣的:(Android)