TranslateAnimation class inherited from Animation class, is basically an animation that controls the position of an object.
The public constructors are:
- TranslateAnimation(Context context, AttributeSet attrs)
Constructor used when a TranslateAnimation is loaded from a resource.
- TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Constructor to use when building a TranslateAnimation from code
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Constructor to use when building a TranslateAnimation from code
In this post I will show you how to move your view from one point to another on your screen with the help of TranslateAnimation class.
There are basically two ways to use this class which are as follows:
1. You can simple call the constructor, mentioned above to apply your transition as follows:
TranslateAnimation animation = new TranslateAnimation(0, 0, 100, 0);
animation.setDuration(1000);
yourImageView.startAnimation(animation);
(called constructor:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
fromXDelta float: Change in X coordinate to apply at the start of the animation
toXDelta float: Change in X coordinate to apply at the end of the animation
fromYDelta float: Change in Y coordinate to apply at the start of the animation
toYDelta float: Change in Y coordinate to apply at the end of the animation)
2. You can use the following method: (Recommended)
public void moveViewToCenter( View view ) {
RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics( dm );
int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
int originalPos[] = new int[2];
view.getLocationOnScreen( originalPos );
int xDest = dm.widthPixels/2;
xDest -= (view.getMeasuredWidth()/2);
int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
While using the above method just place your view(to be moved) in a full screen layout. By doing so you won't have to worry about overlapping and clipping other views.
The method moveViewToCenter gets the View's absolute coordinates and calculates how much distance it has to move from its current position to reach the center of the screen. The statusBarOffset variable measures the status bar height.
This is how you can use TranslateAnimation and move your views.
That's all.
The public constructors are:
- TranslateAnimation(Context context, AttributeSet attrs)
Constructor used when a TranslateAnimation is loaded from a resource.
- TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Constructor to use when building a TranslateAnimation from code
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Constructor to use when building a TranslateAnimation from code
In this post I will show you how to move your view from one point to another on your screen with the help of TranslateAnimation class.
There are basically two ways to use this class which are as follows:
1. You can simple call the constructor, mentioned above to apply your transition as follows:
TranslateAnimation animation = new TranslateAnimation(0, 0, 100, 0);
animation.setDuration(1000);
yourImageView.startAnimation(animation);
(called constructor:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
fromXDelta float: Change in X coordinate to apply at the start of the animation
toXDelta float: Change in X coordinate to apply at the end of the animation
fromYDelta float: Change in Y coordinate to apply at the start of the animation
toYDelta float: Change in Y coordinate to apply at the end of the animation)
2. You can use the following method: (Recommended)
public void moveViewToCenter( View view ) {
RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics( dm );
int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
int originalPos[] = new int[2];
view.getLocationOnScreen( originalPos );
int xDest = dm.widthPixels/2;
xDest -= (view.getMeasuredWidth()/2);
int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
While using the above method just place your view(to be moved) in a full screen layout. By doing so you won't have to worry about overlapping and clipping other views.
The method moveViewToCenter gets the View's absolute coordinates and calculates how much distance it has to move from its current position to reach the center of the screen. The statusBarOffset variable measures the status bar height.
This is how you can use TranslateAnimation and move your views.
That's all.
No comments:
Post a Comment