Simple Android Animation on View GONE/VISIBLE

If you just want a better transition view on GONE / VISIBLE rather than the default Android show/hide, I came across  some simple trick to make a simple animation using TranslateAnimation class.

*Note: For a better transitioning, you can use animationlistener , and setVisibility onAnimationStart() or onAnimationEnd().


// View to be animate (I use ImageView as example here)
ImageView imageView = (ImageView)findViewById(R.id.imageview);

Below are the methods to set view from VISIBLE to GONE


// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from top to bottom
public void slideToBottom(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from bottom to top
public void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

Advertisements

Posted on October 7, 2012, in Education and tagged , . Bookmark the permalink. 8 Comments.

  1. Thanks Colin, nice and clear.

  2. Thanks man!!

  3. Thanks. it is very helpful. 🙂

  4. hi,when i am using the – values in animation the image from bottom to top the image height was cutting means i was lost image heiht.how can i solve the problem.please help me.

  5. Great job mate, helped me a lot, thanks. Keep up the great job..

  1. Pingback: Show and hide a View with a slide up/down animation - QuestionFocus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: