Monday, May 19, 2014

31 Android Translate Animation Useful For You (android.view.animation.TranslateAnimation)

The following code examples are extracted from open source projects.

Code Example 1:

Source AnimationHelper.java
  1. public static Animation inFromRightAnimation() {
  2.  
  3. Animation inFromRight =
  4. new TranslateAnimation(
  5. Animation.RELATIVE_TO_PARENT
  6. , +1.0f
  7. , Animation.RELATIVE_TO_PARENT
  8. , 0.0f
  9. , Animation.RELATIVE_TO_PARENT
  10. , 0.0f
  11. , Animation.RELATIVE_TO_PARENT
  12. , 0.0f);
  13. inFromRight.setDuration(350);
  14. inFromRight.setInterpolator(new AccelerateInterpolator());
  15. return inFromRight;
  16. }

Code Example 2:

Source LayoutAnimation2.java
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4.  
  5. setListAdapter(new ArrayAdapter(this,
  6. android.R.layout.simple_list_item_1, mStrings));
  7. AnimationSet set = new AnimationSet(true);
  8. Animation animation = new AlphaAnimation(0.0f, 1.0f);
  9. animation.setDuration(50);
  10. set.addAnimation(animation);
  11. animation = new TranslateAnimation(
  12. Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
  13. Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
  14. );
  15. animation.setDuration(100);
  16. set.addAnimation(animation);
  17. LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
  18. ListView listView = getListView();
  19. listView.setLayoutAnimation(controller);
  20. }

Code Example 3:

Source UIFactory.java
  1. @Override
  2. public void run() {
  3. Animation a = null;
  4. if (this.recording == false) {
  5. a = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
  6. Animation.RELATIVE_TO_SELF, 0,
  7. Animation.RELATIVE_TO_SELF, -1,
  8. Animation.RELATIVE_TO_SELF, 0);
  9. } else {
  10. a = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
  11. Animation.RELATIVE_TO_SELF, 0,
  12. Animation.RELATIVE_TO_SELF, 0,
  13. Animation.RELATIVE_TO_SELF, -1);
  14. }
  15. a.setDuration(250);
  16. a.setInterpolator(AnimationUtils.loadInterpolator(this.activity,
  17. android.R.anim.anticipate_overshoot_interpolator));
  18. View stopRecView = activity.findViewById(R.id.TapToStopRecordingTextView);
  19. stopRecView.startAnimation(a);
  20. }

Code Example 4:

Source SlidingPanel.java
  1. public void toggle() {
  2. TranslateAnimation anim=null;
  3. isOpen=!isOpen;
  4. if (isOpen) {
  5. setVisibility(View.VISIBLE);
  6. anim=new TranslateAnimation(0.0f, 0.0f,
  7. getHeight(),
  8. 0.0f);
  9. }
  10. else {
  11. anim=new TranslateAnimation(0.0f, 0.0f, 0.0f,
  12. getHeight());
  13. anim.setAnimationListener(collapseListener);
  14. }
  15. anim.setDuration(speed);
  16. anim.setInterpolator(new AccelerateInterpolator(1.0f));
  17. startAnimation(anim);
  18. }

Code Example 5:

Source SlidingPanel.java
  1. public void toggle() {
  2. TranslateAnimation anim=null;
  3. AnimationSet set=new AnimationSet(true);
  4. isOpen=!isOpen;
  5. if (isOpen) {
  6. setVisibility(View.VISIBLE);
  7. anim=new TranslateAnimation(0.0f, 0.0f,
  8. getHeight(),
  9. 0.0f);
  10. }
  11. else {
  12. anim=new TranslateAnimation(0.0f, 0.0f, 0.0f,
  13. getHeight());
  14. anim.setAnimationListener(collapseListener);
  15. set.addAnimation(fadeOut);
  16. }
  17. set.addAnimation(anim);
  18. set.setDuration(speed);
  19. set.setInterpolator(new AccelerateInterpolator(1.0f));
  20. startAnimation(set);
  21. }

Code Example 6:

Source Util.java
  1. public static Animation slideOut(View view, int to) {
  2. view.setVisibility(View.INVISIBLE);
  3. Animation anim;
  4. switch (to) {
  5. case DIRECTION_LEFT:
  6. anim = new TranslateAnimation(0, -view.getWidth(), 0, 0);
  7. break;
  8. case DIRECTION_RIGHT:
  9. anim = new TranslateAnimation(0, view.getWidth(), 0, 0);
  10. break;
  11. case DIRECTION_UP:
  12. anim = new TranslateAnimation(0, 0, 0, -view.getHeight());
  13. break;
  14. case DIRECTION_DOWN:
  15. anim = new TranslateAnimation(0, 0, 0, view.getHeight());
  16. break;
  17. default:
  18. throw new IllegalArgumentException(Integer.toString(to));
  19. }
  20. anim.setDuration(500);
  21. view.startAnimation(anim);
  22. return anim;
  23. }

Code Example 7:

Source Util.java
  1. public static Animation slideIn(View view, int from) {
  2. view.setVisibility(View.VISIBLE);
  3. Animation anim;
  4. switch (from) {
  5. case DIRECTION_LEFT:
  6. anim = new TranslateAnimation(-view.getWidth(), 0, 0, 0);
  7. break;
  8. case DIRECTION_RIGHT:
  9. anim = new TranslateAnimation(view.getWidth(), 0, 0, 0);
  10. break;
  11. case DIRECTION_UP:
  12. anim = new TranslateAnimation(0, 0, -view.getHeight(), 0);
  13. break;
  14. case DIRECTION_DOWN:
  15. anim = new TranslateAnimation(0, 0, view.getHeight(), 0);
  16. break;
  17. default:
  18. throw new IllegalArgumentException(Integer.toString(from));
  19. }
  20. anim.setDuration(500);
  21. view.startAnimation(anim);
  22. return anim;
  23. }

Code Example 8:

Source MainMenu.java
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. analyticsTracker();
  5. setContentView(R.layout.main);
  6. prepareMenu();
  7. String[] keys = actions.keySet().toArray(new String[actions.keySet().size()]);
  8. setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, keys));
  9. AnimationSet set = new AnimationSet(true);
  10. Animation animation = new AlphaAnimation(0.0f, 1.0f);
  11. animation.setDuration(50);
  12. set.addAnimation(animation);
  13. animation = new TranslateAnimation(
  14. Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
  15. Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
  16. );
  17. animation.setDuration(100);
  18. set.addAnimation(animation);
  19. LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
  20. ListView listView = getListView();
  21. listView.setLayoutAnimation(controller);
  22. }

Code Example 9:

Source LiqoidMainActivity.java
  1. public Animation inFromRightAnimation() {
  2.  
  3. Animation inFromRight = new TranslateAnimation(
  4. Animation.RELATIVE_TO_PARENT, +1.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f,
  7. Animation.RELATIVE_TO_PARENT, 0.0f);
  8. inFromRight.setDuration(ANIMATION_DURATION);
  9. inFromRight.setInterpolator(new AccelerateInterpolator());
  10. return inFromRight;
  11. }

Code Example 10:

Source LiqoidMainActivity.java
  1. public Animation inFromLeftAnimation() {
  2. Animation outtoLeft = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, -1.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. outtoLeft.setDuration(ANIMATION_DURATION);
  8. outtoLeft.setInterpolator(new AccelerateInterpolator());
  9. return outtoLeft;
  10. }

Code Example 11:

Source LiqoidMainActivity.java
  1. public Animation outToRightAnimation() {
  2.  
  3. Animation inFromRight = new TranslateAnimation(
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 1.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f,
  7. Animation.RELATIVE_TO_PARENT, 0.0f);
  8. inFromRight.setDuration(ANIMATION_DURATION);
  9. inFromRight.setInterpolator(new AccelerateInterpolator());
  10. return inFromRight;
  11. }

Code Example 12:

Source LiqoidMainActivity.java
  1. public Animation outToLeftAnimation() {
  2. Animation outtoLeft = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, -1.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. outtoLeft.setDuration(ANIMATION_DURATION);
  8. outtoLeft.setInterpolator(new AccelerateInterpolator());
  9. return outtoLeft;
  10. }

Code Example 13:

Source PassView.java
  1. private Animation inFromRightAnimation() {
  2. Animation inFromRight = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, +1.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. inFromRight.setDuration(ANIMATION_DURATION);
  8. inFromRight.setInterpolator(new AccelerateInterpolator());
  9. return inFromRight;
  10. }

Code Example 14:

Source PassView.java
  1. private Animation outToLeftAnimation() {
  2. Animation outtoLeft = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, -1.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. outtoLeft.setDuration(ANIMATION_DURATION);
  8. outtoLeft.setInterpolator(new AccelerateInterpolator());
  9. return outtoLeft;
  10. }

Code Example 15:

Source PassView.java
  1. private Animation inFromLeftAnimation() {
  2. Animation inFromLeft = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, -1.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. inFromLeft.setDuration(ANIMATION_DURATION);
  8. inFromLeft.setInterpolator(new AccelerateInterpolator());
  9. return inFromLeft;
  10. }

Code Example 16:

Source PassView.java
  1. private Animation outToRightAnimation() {
  2. Animation outtoRight = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, +1.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. outtoRight.setDuration(ANIMATION_DURATION);
  8. outtoRight.setInterpolator(new AccelerateInterpolator());
  9. return outtoRight;
  10. }

Code Example 17:

Source PassView.java
  1. private Animation inFromBottomAnimation() {
  2. Animation inFromBottom = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, +1.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. inFromBottom.setDuration(ANIMATION_DURATION);
  8. inFromBottom.setInterpolator(new AccelerateInterpolator());
  9. return inFromBottom;
  10. }

Code Example 18:

Source PassView.java
  1. private Animation outToTopAnimation() {
  2. Animation outtoTop = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, -1.0f);
  7. outtoTop.setDuration(ANIMATION_DURATION);
  8. outtoTop.setInterpolator(new AccelerateInterpolator());
  9. return outtoTop;
  10. }

Code Example 19:

Source PassView.java
  1. private Animation inFromTopAnimation() {
  2. Animation inFromTop = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, -1.0f,
  6. Animation.RELATIVE_TO_PARENT, 0.0f);
  7. inFromTop.setDuration(ANIMATION_DURATION);
  8. inFromTop.setInterpolator(new AccelerateInterpolator());
  9. return inFromTop;
  10. }

Code Example 20:

Source PassView.java
  1. private Animation outToBottomAnimation() {
  2. Animation outToBottom = new TranslateAnimation(
  3. Animation.RELATIVE_TO_PARENT, 0.0f,
  4. Animation.RELATIVE_TO_PARENT, 0.0f,
  5. Animation.RELATIVE_TO_PARENT, 0.0f,
  6. Animation.RELATIVE_TO_PARENT, +1.0f);
  7. outToBottom.setDuration(ANIMATION_DURATION);
  8. outToBottom.setInterpolator(new AccelerateInterpolator());
  9. return outToBottom;
  10. }

Code Example 21:

Source GalleryEx.java
  1. private Animation genAnimation() {
  2. TranslateAnimation translate = new TranslateAnimation(mTouchEndX,
  3. mTouchEndX, 0, 0);
  4. translate.setDuration(25);
  5. translate.setFillAfter(true);
  6. return translate;
  7. }

Code Example 22:

Source DeleteZone.java
  1. private void createAnimations() {
  2. if (mInAnimation == null) {
  3. mInAnimation = new FastAnimationSet();
  4. final AnimationSet animationSet = mInAnimation;
  5. animationSet.setInterpolator(new AccelerateInterpolator());
  6. animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f));
  7. if (mPosition == POSITION_TOP) {
  8. animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
  9. Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
  10. Animation.RELATIVE_TO_SELF, 0.0f));
  11. } else {
  12. animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
  13. Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
  14. Animation.RELATIVE_TO_SELF, 0.0f));
  15. }
  16. animationSet.setDuration(ANIMATION_DURATION);
  17. }
  18. if (mOutAnimation == null) {
  19. mOutAnimation = new FastAnimationSet();
  20. final AnimationSet animationSet = mOutAnimation;
  21. animationSet.setInterpolator(new AccelerateInterpolator());
  22. animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
  23. if (mPosition == POSITION_TOP) {
  24. animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
  25. Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  26. Animation.RELATIVE_TO_SELF, -1.0f));
  27. } else {
  28. animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f,
  29. Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  30. Animation.RELATIVE_TO_SELF, 1.0f));
  31. }
  32. animationSet.setDuration(ANIMATION_DURATION);
  33. }
  34. }

Code Example 22:

Source DockBar.java
  1. public void open(){
  2. dispatchDockBarEvent(true);
  3. setClickable(false);
  4. mState=OPEN;
  5. setVisibility(View.VISIBLE);
  6. int x=0;
  7. int y=0;
  8. switch (mPosition) {
  9. case LEFT:
  10. x=getWidth();
  11. break;
  12. case RIGHT:
  13. x=getWidth();
  14. break;
  15. case TOP:
  16. y=-getHeight();
  17. break;
  18. case BOTTOM:
  19. y=getHeight();
  20. break;
  21. }
  22. TranslateAnimation anim=new TranslateAnimation(x, 0,y, 0);
  23. anim.setDuration(ANIM_DURATION);
  24. startAnimation(anim);
  25. }

Code Example 23:

Source AnimationLayout.java
  1. public void open(){
  2. dispatchDockBarEvent(true);
  3. setClickable(false);
  4. mState=OPEN;
  5. setVisibility(View.VISIBLE);
  6. int x=0;
  7. int y=0;
  8. switch (mPosition) {
  9. case LEFT:
  10. x=getWidth();
  11. break;
  12. case RIGHT:
  13. x=getWidth();
  14. break;
  15. case TOP:
  16. y=-getHeight();
  17. break;
  18. case BOTTOM:
  19. y=getHeight();
  20. break;
  21. }
  22. TranslateAnimation anim=new TranslateAnimation(x, 0,y, 0);
  23. anim.setDuration(ANIM_DURATION);
  24. startAnimation(anim);
  25. }

Code Example 24:

Source AnimationLayout.java
  1. public void toggleSidebar() {
  2. if (mContent.getAnimation() != null) {
  3. return;
  4. }
  5.  
  6. if (mOpened) {
  7. /* opened, make close animation*/
  8. mAnimation = new TranslateAnimation(0, -mSidebarWidth, 0, 0);
  9. mAnimation.setAnimationListener(mCloseListener);
  10. } else {
  11. /* not opened, make open animation */
  12. mAnimation = new TranslateAnimation(0, mSidebarWidth, 0, 0);
  13. mAnimation.setAnimationListener(mOpenListener);
  14. }
  15. mAnimation.setDuration(DURATION);
  16. mAnimation.setFillAfter(true);
  17. mAnimation.setFillEnabled(true);
  18. mContent.startAnimation(mAnimation);
  19. }

Code Example 25:

Source TagButton.java
  1. public Animation createTranslateAnimation(long durationMillis) {
  2. if (old_x == getLeft() && old_y == getTop()) {
  3. return null;
  4. }
  5.  
  6. int dx = getLeft() - old_x;
  7. int dy = getTop() - old_y;
  8. Animation a = new TranslateAnimation(-dx, 0, -dy, 0);
  9. a.setFillAfter(true);
  10. a.setDuration(durationMillis);
  11.  
  12. old_x = getLeft();
  13. old_y = getTop();
  14.  
  15. return a;
  16. }

Code Example 26:

Source TagButton.java
  1. public Animation createTranslateAnimation(long durationMillis) {
  2. if (old_x == getLeft() && old_y == getTop()) {
  3. return null;
  4. }
  5.  
  6. int dx = getLeft() - old_x;
  7. int dy = getTop() - old_y;
  8. Animation a = new TranslateAnimation(-dx, 0, -dy, 0);
  9. a.setFillAfter(true);
  10. a.setDuration(durationMillis);
  11.  
  12. old_x = getLeft();
  13. old_y = getTop();
  14.  
  15. return a;
  16. }

Code Example 27:

Source AnimateDrawables.java
  1. public SampleView(Context context) {
  2. super(context);
  3. setFocusable(true);
  4. setFocusableInTouchMode(true);
  5.  
  6. Drawable dr = context.getResources().getDrawable(R.drawable.beach);
  7. dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
  8.  
  9. Animation an = new TranslateAnimation(0, 100, 0, 200);
  10. an.setDuration(2000);
  11. an.setRepeatCount(-1);
  12. an.initialize(10, 10, 10, 10);
  13.  
  14. mDrawable = new AnimateDrawable(dr, an);
  15. an.startNow();
  16. }

Code Example 28:

Source LayoutAnimation2.java
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4.  
  5. setListAdapter(new ArrayAdapter(this,
  6. android.R.layout.simple_list_item_1, mStrings));
  7. AnimationSet set = new AnimationSet(true);
  8. Animation animation = new AlphaAnimation(0.0f, 1.0f);
  9. animation.setDuration(50);
  10. set.addAnimation(animation);
  11. animation = new TranslateAnimation(
  12. Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
  13. Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
  14. );
  15. animation.setDuration(100);
  16. set.addAnimation(animation);
  17. LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
  18. ListView listView = getListView();
  19. listView.setLayoutAnimation(controller);
  20. }

Code Example 29:

Source AnimationHelper.java
  1. public static Animation outToLeftAnimation() {
  2. Animation outtoLeft =
  3. new TranslateAnimation(
  4. Animation.RELATIVE_TO_PARENT
  5. , 0.0f
  6. , Animation.RELATIVE_TO_PARENT
  7. , -1.0f
  8. , Animation.RELATIVE_TO_PARENT
  9. , 0.0f
  10. , Animation.RELATIVE_TO_PARENT
  11. , 0.0f);
  12. outtoLeft.setDuration(350);
  13. outtoLeft.setInterpolator(new AccelerateInterpolator());
  14. return outtoLeft;
  15. }

Code Example 30:

Source AnimationHelper.java
  1. public static Animation inFromLeftAnimation() {
  2. Animation inFromLeft =
  3. new TranslateAnimation(
  4. Animation.RELATIVE_TO_PARENT
  5. , -1.0f
  6. , Animation.RELATIVE_TO_PARENT
  7. , 0.0f
  8. , Animation.RELATIVE_TO_PARENT
  9. , 0.0f
  10. , Animation.RELATIVE_TO_PARENT
  11. , 0.0f);
  12. inFromLeft.setDuration(350);
  13. inFromLeft.setInterpolator(new AccelerateInterpolator());
  14. return inFromLeft;
  15. }

Code Example 31:

Source AnimationHelper.java
  1. public static Animation outToRightAnimation() {
  2. Animation outtoRight =
  3. new TranslateAnimation(
  4. Animation.RELATIVE_TO_PARENT
  5. , 0.0f
  6. , Animation.RELATIVE_TO_PARENT
  7. , +1.0f
  8. , Animation.RELATIVE_TO_PARENT
  9. , 0.0f
  10. , Animation.RELATIVE_TO_PARENT
  11. , 0.0f );
  12. outtoRight.setDuration(350);
  13. outtoRight.setInterpolator(new AccelerateInterpolator());
  14. return outtoRight;
  15. }

1 comment: