Animate and move a view - android

I have a layout containing several buttons. I want to move this layout to a different position on screen using an animation. When I use a TranslateAnimation, the layout appears to move however the button press events are still only triggered from their old location. I have attempted to then adjust the view's LayoutParams in the onAnimationEnd event - this only resulted in some horrible looking snapping effect when the animation completed. No other answers seem to give a definitive solution to this problem.
How can I move a layout using an animation whilst also ensuring their onClick events are triggered from their new location?

You have to use ObjectAnimator it will actually move your Button with listener attached to their new positions
ObjectAnimator moveButtonAnimator = ObjectAnimator.ofFloat(layout, "translationX", 0, 200f);
moveButtonAnimator.start();
moveButtonAnimator.setDuration(1000);

Related

How to change the position of view after a translate animation using NineOldAndroids?

I tried searching around and found that you have to set a listener to change the actual position of the view when the animation ends. But the thing is, I don't know how I can get the end values from the ObjectAnimator I am using.
Isn't there an easy way to do this aside from setting listeners to all of my Animators, there are like 9 of them. Something like a setFillAfter(true) I always see that but I can't seem to find what Animator object uses it.
It turns out you have to have an AnimatorListener attached to the Animators that will do this for you. But that is too much for me because I have multiple Animators. So what I did, I positioned them on the layout in what they would be after the end animation, then I made the Animator to animate from the start position to their end position. That way I don't have to fret over the actual positions of the views

Android view animation looses position of view

For my current project I'm building a pulldown view with a handle and a content.
When I move the handle by dragging it everything is fine. Then I let it go and start an animation to fully open or close the pulldown.
First: without the animation in it everything is fine, but off course not that smooth...
So I've added this for the animation:
ObjectAnimator animHandle = ObjectAnimator.ofFloat(mHandleContainer, "y", newHandlePosition);
ObjectAnimator animContent = ObjectAnimator.ofFloat(mContent, "y", newContentPosition);
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animHandle, animContent);
animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
animatorSet.setDuration(MAX_ANIMATION_DURATION);
animatorSet.start();
Everything is just fine utill here.
But then, when I press the handle (mHandleContainer) again, I want to know where the handle is positioned so I do a
mHandleContainer.getTop()
And that is where it all goes wrong.
When I initialize the custom component I set the top of the handle to 0.
So far so good, then I start dragging, when I let go I see that I will animate to a position of 1134 pixels. Which is fine (meaning the pull down will be entirely open).
But then I press the handle again and get the top (getTop()) and it still returns me 0.
What I want is that my view's getTop() function returns me 1134 again.
Is that possible?
Or should I do something in the onAnimationEnd method of my AnimatorListener?
I already tried to do a mHandleContainer.layout(..) in there and then my getTop() is ok, but then my layout is screwed...
And tricks here for letting the animation end on the correct position (as it does) and persist the position in the view?
I already had a look on SO (and Google) but didn't find something useful. The only thing close to my problem that I could find was this: NineOldAndroids, not clickable view after rotation or move
But didn't help either...
This is because your are animating the "y" property while trying to read the property top, and they aren't the same.
Top is the top position of the view relative to its parent.
Y is the visual y position of the view which is equivalent to translationY + the current top property.
So in your case:
Either animate "top" like this:
ObjectAnimator animHandle = ObjectAnimator.ofFloat(mHandleContainer, "top", 200);
And read it via
mHandleContainer.getTop()
Or "y":
ObjectAnimator animHandle = ObjectAnimator.ofFloat(mHandleContainer, "y", 200);
And read it via
mHandleContainer.getY();
I did solve the issue using the basic ValueAnimator which each time called my custom drawing method for moving all my views.

Android - Animation on widgets

I started with a simple animation on button,
I am successful to an extent,
The problem i am facing is, I am able to animate the button but the position of listener,
Even after button is animated the clickable area is not animated , It is resting at the original postion
Could some one help me with an useful code snippet or suggestion
Regards
Android animations can not change the layout of buttons or view in general. Only their image will appear to move, but they will stay in the same place. After the animation is finished, get the Layout parameters through View.getLayoutParams() and change them.

UI animation in android

I have view with several components on it. I use Linear layout with vertical orientation
When user unchecks some checkbox I need to hide one EditText and other components moves up
edtPhone.setEnabled(false);
works well, but everything happens immediately and i want to do it slowly and animated
How can i do it in android?
Create a TranslateAnimation, assign an AnimationListener to it and in onAnimationEnd() you should set the visibility of your EditText to View.GONE (or View.INVISIBLE, it depends on your needs) and do whatever you want when the animation has stopped.
Remember to assign the TranslateAnimation to the other components that should move up. In order to get your TranslateAnimation to move slowly, use android:duration in XML or Animation.setDuration() in code, with a defined value in milliseconds (maybe 500 milliseconds?).

Struggling with view animations

I've got an absolute layout. In that layout is a custom view that takes up the left 3rd of the screen. I put a button in the layout that I want to cause the custom view to slide on and off of the screen. I've tried using animation resources (translates... "slidein" and "slideout") and the function startAnimation on the custom view, but I can't get the behavior I am looking for.
OK... I start with the custom view visible and in onCreate I find the view and animate it off screen using my slideout animation. That works fine. I figured out that I need to set "fillAfter" in the animation so that the custom view stays off screen.
Now, when I press my button I want to cause the custom view to slide back on the screen, so I trigger my slidein animation using startAnimation again but with slidein. BUT... that causes the view to first jump back to its original position AND THEN slide to the right... causing it to finish in the middle of the screen.
How do I get the animation to use the view's current position as the animation starting position, not its original position?
Thanks
I also experienced the flicker described in this question. My solution was to use the improved Honeycomb animation APIs. There is a convenient library that ports these all the way back to Android 1.0:
http://nineoldandroids.com/
For more on Honeycomb Animation APIs see:
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
In my case I had 2 overlapped LinearLayouts inside a RelativeLayout. I wanted to slide the top LinearLayout off the screen and reveal the bottom LinearLayout below. Then I wanted to slide to top LinearLayout back on screen to its original position so the top layout covered the bottom layout again. Using the old animation APIs I was seeing a flicker before the second animation (offscreen -> onscreen) was starting.
With the new APIs this task turned out to be trivial:
// Slide out (add to button handler)
ObjectAnimator.ofFloat(mTopLayout, "translationY", mTopLayout.getHeight()).start();
// Slide back in (add to button handler)
ObjectAnimator.ofFloat(mTopLayout, "translationY", 0).start();
The Honeycomb Animation APIs actually move objects around on the screen (rather than pretending to move them like the older animation APIs), so there is no need to fool around with filleAfter, fillBefore, etc.
Look into setting the fillAfter property to keep the end animation state

Categories

Resources