码迷,mamicode.com
首页 > 移动开发 > 详细

android-各种动画加速器,各种插值器

时间:2016-05-12 19:17:51      阅读:673      评论:0      收藏:0      [点我收藏+]

标签:



import android.view.animation.Interpolator;

public class BackInterpolator implements Interpolator {

	private int type;
	private float overshot;

	public BackInterpolator(int type, float overshot) {
		this.type = type;
		this.overshot = overshot;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t, overshot);
		} else
		if (type == EasingType.OUT) {
			return out(t, overshot);
		} else
		if (type == EasingType.INOUT) {
			return inout(t, overshot);
		}
		return 0;
	}

	private float in(float t, float o) {
		if (o == 0) {
			o = 1.70158f;
		}
		return t*t*((o+1)*t - o);
	}

	private float out(float t, float o) {
		if (o == 0) {
			o = 1.70158f;
		}
		return ((t-=1)*t*((o+1)*t + o) + 1);
	}
	
	private float inout(float t, float o) {
		if (o == 0) {
			o = 1.70158f;
		}
		t *= 2;
		if (t < 1) {
			return 0.5f*(t*t*(((o*=(1.525))+1)*t - o));
		} else {
			return 0.5f*((t-=2)*t*(((o*=(1.525))+1)*t + o) + 2);
		}
	}
}
</pre><pre name="code" class="java">
</pre><p></p><p></p><p></p><p></p><p></p><pre name="code" class="java">public class BounceInterpolator implements Interpolator {

	private int type;

	public BounceInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float out(float t) {
		if (t < (1/2.75)) {
			return 7.5625f*t*t;
		} else
		if (t < 2/2.75) {
			return 7.5625f*(t-=(1.5/2.75))*t + .75f;
		} else
		if (t < 2.5/2.75) {
			return 7.5625f*(t-=(2.25/2.75))*t + .9375f;
		} else {
			return 7.5625f*(t-=(2.625/2.75))*t + .984375f;
		}
	}

	private float in(float t) {
		return 1 - out(1-t);
	}

	private float inout(float t) {
		if (t < 0.5f) {
			return in(t*2) * .5f;
		} else {
			return out(t*2-1) * .5f + .5f;
		}
	}
}




public class CircInterpolator implements Interpolator {

	private int type;

	public CircInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return (float) -(Math.sqrt(1 - t*t) - 1);
	}
	private float out(float t) {
		return (float) Math.sqrt(1 - (t-=1)*t);
	}
	private float inout(float t) {
		t *= 2;
		if (t < 1) {
			return (float) (-0.5f * (Math.sqrt(1 - t*t) - 1));
		} else {
			return (float) (0.5f * (Math.sqrt(1 - (t-=2)*t) + 1));
		}
	}
}



public class CubicInterpolator implements Interpolator {

	private int type;

	public CubicInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return t*t*t;
	}
	private float out(float t) {
		return (t-=1)*t*t + 1;
	}
	private float inout(float t) {
		t *= 2;
		if (t < 1) {
			return 0.5f*t*t*t;
		} else {
			return 0.5f*((t-=2)*t*t + 2);
		}
	}
}


public class ElasticInterpolator implements Interpolator {

	private int type;
	private float amplitude;
	private float period;

	public ElasticInterpolator(int type, float amplitude, float period) {
		this.type = type;
		this.amplitude = amplitude;
		this.period = period;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t, amplitude, period);
		} else
		if (type == EasingType.OUT) {
			return out(t, amplitude, period);
		} else
		if (type == EasingType.INOUT) {
			return inout(t, amplitude, period);
		}
		return 0;
	}

	private float in(float t, float a, float p) {
		if (t == 0) {
			return 0;
		}
		if (t >= 1) {
			return 1;
		}
		if (p == 0) {
			p = 0.3f;
		}
		float s;
		if (a == 0 || a < 1) {
			a = 1;
			s = p / 4;
		}
		else {
			s = (float) (p/(2*Math.PI) * Math.asin(1/a));
		}
		return (float) (-(a*Math.pow(2,10*(t-=1)) * Math.sin((t-s)*(2*Math.PI)/p)));
	}

	private float out(float t, float a, float p) {
		if (t == 0) {
			return 0;
		}
		if (t >= 1) {
			return 1;
		}
		if (p == 0) {
			p = 0.3f;
		}
		float s;
		if (a == 0 || a < 1) {
			a = 1;
			s = p / 4;
		}
		else {
			s = (float) (p/(2*Math.PI) * Math.asin(1/a));
		}
		return (float) (a*Math.pow(2,-10*t) * Math.sin((t-s)*(2*Math.PI)/p) + 1);
	}
	
	private float inout(float t, float a, float p) {
		if (t == 0) {
			return 0;
		}
		if (t >= 1) {
			return 1;
		}
		if (p == 0) {
			p = .3f*1.5f;
		}
		float s;
		if (a == 0 || a < 1) {
			a = 1;
			s = p / 4;
		}
		else {
			s = (float) (p/(2*Math.PI) * Math.asin(1/a));
		}
		t *= 2;
		if (t < 1) {
			return (float) (-.5*(a*Math.pow(2,10*(t-=1)) * Math.sin((t-s)*(2*Math.PI)/p)));
		} else {
			return (float) (a*Math.pow(2,-10*(t-=1)) * Math.sin((t-s)*(2*Math.PI)/p)*.5 + 1);
		}
	}
}


public class ExpoInterpolator implements Interpolator {

			private int type;

			public ExpoInterpolator(int type) {
				this.type = type;
			}

			public float getInterpolation(float t) {
				if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return (float) ((t==0) ? 0 : Math.pow(2, 10 * (t - 1)));
	}
	private float out(float t) {
		return (float) ((t>=1) ? 1 : (-Math.pow(2, -10 * t) + 1));
	}
	private float inout(float t) {
		if (t == 0) {
			return 0;
		}
		if (t >= 1) {
			return 1;
		}
		t *= 2;
		if (t < 1) {
			return (float) (0.5f * Math.pow(2, 10 * (t - 1)));
		} else {
			return (float) (0.5f * (-Math.pow(2, -10 * --t) + 2));
		}
	}
}



public class QuadInterpolator implements Interpolator {

	private int type;

	public QuadInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return t*t;
	}
	private float out(float t) {
		return -(t)*(t-2);
	}
	private float inout(float t) {
		t *= 2;
		if (t < 1) {
			return 0.5f*t*t;
		} else {
			return -0.5f * ((--t)*(t-2) - 1);
		}
	}
}


public class QuartInterpolator implements Interpolator {

	private int type;

	public QuartInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return t*t*t*t;
	}
	private float out(float t) {
		return -((t-=1)*t*t*t - 1);
	}
	private float inout(float t) {
		t *= 2;
		if (t < 1) {
			return 0.5f*t*t*t*t;
		} else {
			return -0.5f * ((t-=2)*t*t*t - 2);
		}
	}
}



public class QuintInterpolator implements Interpolator {

	private int type;

	public QuintInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return t*t*t*t*t;
	}
	private float out(float t) {
		return ((t-=1)*t*t*t*t + 1);
	}
	private float inout(float t) {
		t *= 2;
		if (t < 1) {
			return 0.5f*t*t*t*t*t;
		} else {
			return 0.5f*((t-=2)*t*t*t*t + 2);
		}
	}
}


public class SineInterpolator implements Interpolator {

	private int type;

	public SineInterpolator(int type) {
		this.type = type;
	}

	public float getInterpolation(float t) {
		if (type == EasingType.IN) {
			return in(t);
		} else
		if (type == EasingType.OUT) {
			return out(t);
		} else
		if (type == EasingType.INOUT) {
			return inout(t);
		}
		return 0;
	}

	private float in(float t) {
		return (float) (-Math.cos(t * (Math.PI/2)) + 1);
	}
	private float out(float t) {
		return (float) Math.sin(t * (Math.PI/2));
	}
	private float inout(float t) {
		return (float) (-0.5f * (Math.cos(Math.PI*t) - 1));
	}
}

android-各种动画加速器,各种插值器

标签:

原文地址:http://blog.csdn.net/u011216417/article/details/51356685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!