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
Related
I have a card-like view with elevation and a white background that I want to expand to fill up the whole screen. It's a card in a list (my own custom layout, not a CardView), and I want to make it take up the whole screen when it's tapped via a Fragment transition.
My first idea is to create a fake, identical view in the new fragment, place it at the item's position, and animate it to the top, while animating its layout bounds to take up the whole screen. However, I don't think this is the best idea, as Android will have to remeasure the layout every single frame while the animation is running (likely to lag). Is there a way I can get this effect in a clean way? I'm going to be using fragment transitions, so hopefully I can use Animator in some way, but I'll take any solution.
You can try to do the same, but instead of relayouting, do redrawing. Create custom implementation of view's onDraw(), it's not that hard.
View will fill entire screen but at the start of animation will draw only initial part.
Or you can just try your approach, it's easy to implement and may be it is not that slow.
Also, may be directly modifying view's setLeft(), setRight(), etc. with Animators is a valid solution.
I would like to know if there is an easy way of having a grid of elements, where when one of the elements is pressed,
This state:
Changes, to reveal the touched element (center element here)
For a start, I thought manually re-adding the element as over all others, then programmatically setting the position to keep it appear to be centered.
Is there an easier way, or do I have to break a bit of sweat for it?
As far as I know, Android animations don't affect views boundaries. Therefore, you could simply use setScale method of view to scale them up and down. Also, you could considering animations for better UX.
In Android there are two general way for displaying a grid of views:
Grid View
Grid Layout
In both approaches, this trick can be used.
For each cell (individual views) add a touch listener in which whenever the touch is of type down, scale it up and otherwise scale it to normal.
In Grid Views, you should do this in its adapter while for a grid layout you could iterate over its children and apply this.
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.
Does anyone have experience on view animation? I have met a problem, In my application, I have an activity, have two views, left and right, and I want to exchange these two views dynamically(left to right and right to left), when the view from left to right need view animation, I mean I need redraw the view in the process of moving, not just start an animation. Actually, Those means that we can move one view from one position to another position, and have animation effect. Not start an
animation, then removeView andView.....
You want Property Animation - sadly it's still just for Honeycomb and above.
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/