I'm having a real serious problem.
I want to implement UI where some view is moving on the screen and the user can touch it.
I know that regular API-1 Animation framework move just the graphics of the view, and API-11 Animator framework move the view itself, but the problem is that it support only API-11+ which is very high.
I thought that instead of putting click listener on the view itself - I will identify which item was clicked by getting its position.
So this is what I will do:
* User touch the screen.
* Stop all the animations.
* Get all the animated views to the position where the animation was ended. (This is were I need your help).
* Iterate over the views and find out if the click was in the area of one of them.
But I can't find a way to move the view to the animation's end position.
If someone knows a way, I will be very thankful!
Thanks in advance,
Elad
But I can't find a way to move the view to the animation's end position.
Modify its LayoutParams to move it to the end position. Use getLayoutParams() on the View, cast it to the appropriate type based on its container, modify the LayoutParams object, then call setLayoutParams() on the View to commit the changes.
Related
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
I am using animators (ObjectAnimator) to animate few properties (scale, rotate) of a view.
Target view is animating properly when ObjectAnimators are set to it.
But there is an added requirement to get view to the original position (reset) after a while.
I tried to cancel() the animator but it only cancels the animation and doesn't reset the view.
Possible solution : creating another animator that does just opposite of the initial animator.
Is there any other way to reset it ?
See the solution I came up with as I had a similar issue with animations inside views in a recycler view, so I had to find a way to reset them:
Trying to reset values from Property Animator to be used in recycler view
Good news in Android O these will be better supported
I met the same problem, because I animate the view use ViewPropertyAnimator, addView() and removeView() again and again for not creating new view, but a view can be shown once, when you remove the view, you call addview() again, it is no show, but you see the property visibility is visible and animationListener also be called. it is strange.
I had the Same problem with my views . so I came over this problem with this trick .
So We assume we have a view that applying an animate after clicking on it .
viewLayout.arrow_back.setOnClickListener {arrow ->
arrow.animate().rotationXBy(50F) // this is a just simple anim not so usefull
.start()
arrow.isEnabled = false
}
I hope it'll Help someone .
Both ObjectAnimator.cancel() and ObjectAnimator.end() don't cancel the effects the animation had on the animated object, but rather just cancel its execution at the point in which the methods were called (see ValueAnimator's documentation for more details).
Now, I can think of three main ways to accomplish what you wanted:
Put a listener on the ObjectAnimator, and when OnAnimationEnd/OnAnimationCancel is called, manully reset the properties of the view.
If the properties of the view you want to reset are those given to it in the XML file, you can re-inflate it. For more details, check out this answer.
Use the ObjectAnimator.reverse() method to reverse the execution of the animation, effectively reseting the properties of the view. Preferably, you would want to call start the reverse when the animation ends, so you'd probably need to put a listener on the animator and check when OnAnimationEnd with the animtor to reverse is called.
Do you mean stop a running animation? If so, call clearAnimation() to remove animations from the views that you called startAnimation();
If what you mean is to reset the view to its original appearance after the animation is over, always setFillAfter(false); to the animations.
I'm working on a custom ViewGroup.
This ViewGroup has a bunch of children. I need to animate a few and change their position. I understand that Android animations move just the bitmap and not the real object. I've been trying to MOVE them by following various resources but have failed.
What I'm doing with ViewGroup so far:
Measure children and the ViewGroup
Position children in onLayout
What I'm trying to do further
Use a custom animation to move a small subset of the children. I'm using a custom Animation object because I need to move a bunch of Views and I'm applying translationX on all of them together. The other option that I know is to start a separate Animation on all of them and the thought of which makes me think it's gonna be unoptimized.
Problem
Views animate fine, but their real position remains unchanged. So the next time I'm trying to do the same kind of animation, but on the new co-ordinates, it doesn't work. Because, their positions haven't updated.
What did I try
Use onAnimationEnd to layout each of the children to the new left, top, right and bottom position. All views vanished
On onAnimationEnd, reset translationX to zero and then start re-positioning the views. No effect of calling view.setTranslationX(0f)
Can someone please help me with the correct way of doing this? Thanks
when animating call layout() on your child Views
I have a view which contains 2 image view that is using the ondraglistener to move them around the screen and that all works fine. I am now trying to animate one of the image views but I'm not sure if this effect is possible.
The animation works correctly but I can't get it to stick with the container view while it is being dragged around and still animate at the same time. It looks like once you start the dragging the view just freezes until the drag is finished. Any pointers on how to accomplish this or if it is even possible? Thanks for advice.
EDIT: It looks like I will have to override the DragShadowBuilder so I guess the next question is whether you can send in an animatable view to the DragShadowBuilder...
EDIT: I guess you can't do it with DragShadowBuilder at all so attempting to do it by overriding onTouch...
So a solution here is to override onTouch and kick off the animation when the click is first 'touched down' and then on touch move just update the margins (or use an x/y translation) based of the motion event's getX and getY and then when 'touch up' just stop the animation. Also the on touch has to be on a wrapper view rather than on the view where you are resetting the layout parameters. I can post the code if anyone is looking for a more concrete explanation.
I want to include a simple animation in my app where a view widget moves to another view widget.
The code I have written is:
TranslateAnimation animate = new TranslateAnimation(view1.getTranslationX(), view1.getTranslationY(), view2.getTranslationX(), view2.getTranlationY());
animate.setDuration(500);
view1.startAnimation(animate);
The problem is that view1 is a custom view inherited from ViewSwitcher whereas view2 is a Button. getTranslation() gives no such method exception for both.
How can I get the view's position on screen and is this the right way to do the animation ?
Note: The ViewSwitcher is part of a ListView item so it also needs to cross over its ListView boundary to get to the Button(just in case that makes a difference).
Methods like getTranslationX() and getTranslationY() simply return an offset value that may be applied to the actual view position, and not the position itself. You will want to use methods like getLeft() and getTop() to get the x/y position value of a view relative to its parent (in this case, the ListView).
If you need more global coordinates, use getLocationInWindow() or getLocationOnScreen() to get the view's position relative to the global display hierarchy. These methods do not return the position, but rather fill it into the int[] you provide as a parameter.
All of these methods can be called on any View.
HTH
Basically, I'm not sure, if you did everything correct..
As for me, the easiest way to implement animation in Android is to use ViewFlipper. Maybe you should consider wrapping your views with it.
Here you have some post about using translation and alpha animations with ViewFlipper in case you follow my suggestion http://kevinrohling.wordpress.com/2011/01/25/using-a-slide-transition-with-viewflipper/