Custom Animation:1、Zoom
2、Scale
3、Slide Up
public abstract class CustomAnimation extends BaseActivity { private CanvasTransformer mTransformer; public CustomAnimation(int titleRes, CanvasTransformer transformer) { super(titleRes); mTransformer = transformer; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the Above View setContentView(R.layout.content_frame); getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, new SampleListFragment()) .commit(); SlidingMenu sm = getSlidingMenu(); setSlidingActionBarEnabled(true); sm.setBehindScrollScale(0.0f); sm.setBehindCanvasTransformer(mTransformer); } }
主界面三个Demo使用的是一样的:在CustomAnimation设置Above View。
sm.setBehindCanvasTransformer(mTransformer);//Sets the behind canvas transformer.
动画效果主要是靠传入CanvasTransformer transformer这个接口来决定的。
public interface CanvasTransformer { /** * Transform canvas. * * @param 画布 * @param open的百分比 */ public void transformCanvas(Canvas canvas, float percentOpen); }
ZOOM效果:
public class CustomZoomAnimation extends CustomAnimation { public CustomZoomAnimation() { super(R.string.anim_zoom, new CanvasTransformer() {//让父类来处理 @Override
//实现了接口 public void transformCanvas(Canvas canvas, float percentOpen) { float scale = (float) (percentOpen*0.25 + 0.75); canvas.scale(scale, scale, canvas.getWidth()/2, canvas.getHeight()/2); } }); } }
canvas.scale(scale, scale, canvas.getWidth()/2, canvas.getHeight()/2):
public final void scale(float sx, float sy, float px, float py) { translate(px, py); scale(sx, sy); translate(-px, -py); }
Scale:
public class CustomScaleAnimation extends CustomAnimation { public CustomScaleAnimation() { super(R.string.anim_scale, new CanvasTransformer() { @Override public void transformCanvas(Canvas canvas, float percentOpen) { canvas.scale(percentOpen, 1, 0, 0); } }); } }
Slide up:(不同)
public class CustomSlideAnimation extends CustomAnimation { private static Interpolator interp = new Interpolator() { @Override public float getInterpolation(float t) { t -= 1.0f; return t * t * t + 1.0f; } }; public CustomSlideAnimation() { // see the class CustomAnimation for how to attach // the CanvasTransformer to the SlidingMenu super(R.string.anim_slide, new CanvasTransformer() { @Override public void transformCanvas(Canvas canvas, float percentOpen) { canvas.translate(0, canvas.getHeight()*(1-interp.getInterpolation(percentOpen))); } }); } }
Interpolator():参考http://blog.csdn.net/jason0539/article/details/16370405
/** * interpolator定义了动画的刷新速度. 这就给动画实现加速,减速,重复等提供了可能 */ public interface Interpolator extends TimeInterpolator { // A new interface, TimeInterpolator, was introduced for the new android.animation // package. This older Interpolator interface extends TimeInterpolator so that users of // the new Animator-based animations can use either the old Interpolator implementations or // new classes that implement TimeInterpolator directly. }
AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置
如果android定义的interpolators不符合你的效果也可以自定义interpolators