Translation animation for hiding View - android

I need my listview to hide and show using alternative touches. Hence for hiding the listview on the left side of the screen am using animation
Animation animation = new TranslateAnimation(-100, 0,0, 0);
animation.setDuration(100);
animation.setFillAfter(true);
lv.startAnimation(animation);
lv.setVisibility(0);
and for displaying am using
lv.setVisibility(View.VISIBLE);
My problem is list view is not getting hide. It will go leftside and coming back again. I don't know how to hide listview to the left edge completely on touch. Please help in achieving this

// 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);
}

Finally i find the answer and it is very simple modification in co-ordinate values. And the code is
Animation animation = new TranslateAnimation(0,-200,0, 0);
animation.setDuration(2000);
animation.setFillAfter(true);
listView1.startAnimation(animation);
listView1.setVisibility(0);
Here am setting negative value at second co-ordinate cause from o it is moving twowards negative side which means the view is moving twowards inner left side.

If you want to hide your View, use
View.INVISIBLE // constant value 4
or
View.GONE // constant value 8
You are currently using the value 0 which is the constant value of View.VISIBLE.
I suppose you want to hide the ListView after animating it?
But you are showing the ListView directly after starting the animation. Take a look at AnimationListener and hide the ListView
onAnimationEnd(...)
For example:
// assuming the listview is currently visible
Animation animation = new TranslateAnimation(-100, 0,0, 0);
animation.setDuration(100);
animation.setFillAfter(true);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
lv.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
lv.startAnimation(animation);

for the general understanding of what you don't know, i found you another post that explains it very good!! The View and its animation work a bit different as it might be expected!
https://stackoverflow.com/a/5888969/2074990

Related

Move Image translation using android animation

I need an animation for an image in my application. where two balls size images should coming from the right and left corner and move all over the screen infinity on click of button
please help.
It sounds like you are looking for a TranslateAnimation
TranslateAnimation allows you to create a simple animation from one (x,y) position to another (x,y) position.
If you wanted it to run infinitely, you might do something like this:
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,100,100);
translateAnimation.setRepeatMode(Animation.INFINITE);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
/* Set new from values, then generate new to values here, then restart animation */
}
});
View myView = new View();
myView.startAnimation( translateAnimation);
In my onAnimationRepeat method, I would reset the fromX and fromY values of the animation, and generate new toX and toY values, then start it over again.
This will only move the view from one point to another, if you need it to do multiple different animations at once, you might need to create a custom animation.

How to animate an ImageButton into invisibility

This is a follow up to Animate ImageView from alpha 0 to 1.
I have an ImageButton that I want to animate into view when user does X and then animate out of view when user does Y. Animating into view is working fine. But animating out of view is kind of not working. In fact it is working, in that the animation happens. But after the button fades out of view, it pops back as VISIBLE. So I use this code
AlphaAnimation animation1 = new AlphaAnimation(0.9f, 0.0f);
animation1.setDuration(5000);
animation1.setStartOffset(1000);
animation1.setFillAfter(true);
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationStart: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationRepeat(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationRepeat: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationEnd(Animation animation) {
tokenBtn.setVisibility(View.INVISIBLE);
GVLog.d(TAG,"MAKE INVISIBLE onAnimationEnd: %s",tokenBtn.getVisibility());
}
});
tokenBtn.startAnimation(animation1);
but the call to make the ImageButton invisible is not taking effect. So how do I make it take effect so that the ImageButton remains invisible?
Try like this. These codes always work on my apps.
AlphaAnimation animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1000);
tokenBtn.startAnimation(animation1);
tokenBtn.setVisibility(View.INVISIBLE);

LayoutAnimationController not working when hiding View Group but works when Showing view group

LayoutAnimationController is used to animate children of view group
I used LayoutAnimationController to show elements in LinearLayout with animation effect one by one using following code.
Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.anim_fade_in);
//lnrContactContainer is LinearLayout.
AnimationSet set = new AnimationSet(true);
set.addAnimation(fadeIn);
set.setDuration(500);
controller = new LayoutAnimationController(set, 1f);
lnrContactContainer.setLayoutAnimation(controller);
lnrContactContainer.setVisibility(View.VISIBLE);
But same approach does not work when I use it to show fadeout animation while hiding LinearLayout lnrContactContainer.setVisibility(View.GONE);
Instead of hiding children one by one it hides the parent.
Instead of hiding children one by one it hides the parent.
To hide the parent only after the Animation has been applied to all children, use an AnimationListener:
lnrContactContainer.setLayoutAnimationListener(new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation){}
#Override
public void onAnimationEnd(Animation animation)
{
lnrContactContainer.setVisibility(View.GONE)
}
#Override
public void onAnimationRepeat(Animation animation){}
});
By the way, my fadeout animation needed
set.setFillAfter(true);
to keep the items from popping in again after fading although my animation xml file (in res/anim) already contained android:fillAfter="true".

Using Animation to swipe views

I have a FrameLayout that recognize swipe gestures (up and down).
For example: if a swipe up are performed, I should animate the current view (that is MATCH_PARENT x MATCH_PARENT) to goes up at the same time a new view come from bottom.
I can achieve this with animations?
I solved this way:
private void swipeUp() {
current.currentPage++;
final View hidingView = currentView;
TranslateAnimation hide = new TranslateAnimation(0, 0, 0, -getHeight());
hide.setAnimationListener(new AnimationListenerAdapter() {
#Override
public void onAnimationEnd(Animation animation) {
hidingView.setVisibility(View.GONE);
}
});
hide.setDuration(1000);
hidingView.startAnimation(hide);
TranslateAnimation show = new TranslateAnimation(0, 0, getHeight(), 0);
show.setFillAfter(true);
show.setDuration(1000);
View nextView = getView();
addView(nextView, createLP());
nextView.startAnimation(show);
currentView = nextView;
}
If you want to actually switch views, you need to implement an AnimationListener that takes care of the animation. If you want more complex behavior like a "finger following" scroller between views, you will likely have to use something a bit more complex, but if you're just saying
if(I flicked upwards)
move view up
then AnimationListener is perfect for you. Just make sure you set the listener to the Animation in code.
Hope this helps!

How to animate a child view and having the parent view to automatically resize?

I've a quick question.
I'm trying to animating a child view to make it disappear. That part is easy, but, during the animation, the parent view is not resized. How can I do to make the parent view automtically resize during the animation?
Here is my current code:
AnimationSet set = new AnimationSet(true);
Animation fade = new ScaleAnimation(1.f, 1.f, 1.f, 0);
Animation fade2 = new AlphaAnimation(1, 0);
set.addAnimation(fade);
set.addAnimation(fade2);
set.setDuration(2000);
set.setFillAfter(true);
bgColor.startAnimation(set);
bgColor is the View I'm making disappear. Here is the final result :
Result
You can see the gap after the animation.
Any ideas?
Thanks in advance!
You may add an AnimationListener and do a change of the layout after the animation is done
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
bgColor.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
Also, be sure to have your stuff together regarding onFillAfter()!

Categories

Resources