Animation Interpolator : Animation « Animation « Android






Animation Interpolator

    
//package org.ametro.util;

import android.graphics.PointF;

public class AnimationInterpolator {

  public final static int SCALE = 1;
  public final static int SCROLL = 2;
  public final static int SCALE_AND_SCROLL = 3;
  
  private PointF mStartPoint = new PointF();
  private PointF mEndPoint = new PointF();
  private PointF mNowPoint = new PointF();
  
  private float mStartScale;
  private float mEndScale;
  
  private long mPeriod;
  private long mEndTime;
  private long mNowTime;
  
  private int mMode;
  
  public void begin(PointF startPoint, PointF endPoint, Float startScale, Float endScale, long time){
    this.mNowTime = System.currentTimeMillis();
    this.mEndTime = mNowTime + time;
    this.mPeriod = time;
    this.mMode = 0;
    if(startScale!=null && endScale!=null){
      this.mStartScale = startScale;
      this.mEndScale = endScale;
      this.mMode |= SCALE;
    }
    if(startPoint!=null && endPoint!=null){
      this.mStartPoint.set(startPoint);
      this.mEndPoint.set(endPoint);
      this.mNowPoint.set(startPoint);
      this.mMode |= SCROLL;
    }
  }

  public void begin(float startScale, float endScale, long time){
    begin(null,null,startScale, endScale, time);
  }
  
  public void begin(PointF startPoint, PointF endPoint, long time){
    begin(startPoint, endPoint, null, null, time);
  }
  
  public boolean more(){
    return mNowTime < mEndTime; 
  }
  
  public void next(){
    mNowTime = System.currentTimeMillis();
  }
  
  public PointF getPoint(){
    if(mNowTime < mEndTime){
      float k = getProgress();
      float x = mEndPoint.x - k * (mEndPoint.x - mStartPoint.x);
      float y = mEndPoint.y - k * (mEndPoint.y - mStartPoint.y);
      mNowPoint.set(x, y);
      return mNowPoint;
    }else{
      return mEndPoint;
    }
  }
  
  public float getScale(){
    if(mNowTime < mEndTime){
      float k = getProgress();
      return (mEndScale - k * (mEndScale - mStartScale));
    }else{
      return mEndScale;
    }
  }
  
  private float getProgress() {
    return (float)(mEndTime - mNowTime) / mPeriod;
  }

  public boolean hasScale() {
    return (mMode & SCALE)!=0;
  }
  
  public boolean hasScroll() {
    return (mMode & SCROLL)!=0;
  }
  
}

   
    
    
    
  








Related examples in the same category

1.Animation: fade in, fade out
2.Frame based animation
3.extends Animation to create your own animation
4.Slide out down animation, bounce in down animation
5.XML for Fade in animation
6.Xml for Fade out animation
7.XML for slide out animation
8.Animation Cloning
9.Animation Loading
10.Multi Property Animation
11.Animation: push up in,push up out,push left in,push left out,fade in,fade out,hyperspace in,hyperspace out
12.Using AlphaAnimation class to do animation in code
13.Using TranslateAnimation class
14.Demonstrates the seeking capability of ValueAnimator.
15.Animating by calling invalidate() from draw(),loading and drawing resources, handling onPause() in an animation