Android - TranslateAnimate one view to another view - android

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/

Related

How do I make a view expand like a Material "sheet"?

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.

Animating a group of children in a viewgroup

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

Move view itself to animation's end position

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.

Android - How to determinate ViewFlipper children size when not displayed?

I'm actually making an application using a ViewFlipper to display 3 differents custom views. These views are, for each one, in a ScrollView (putting the ViewFlipper in a single ScrollView isn't making my onFling gestures really efficient). And i'm actually trying to synchronize the three Scrollbars position. At this point using a single ScrollView would have been easier but i trying to not use this solution.
I'm using a ScrollListener for each ScrollView to set the others ScrollView scroll position like in this thread : Synchronise ScrollView scroll positions - android.
The problem is that this method will works but the first time. When i'm on a ScrollView, the two others are not drawn already and their height is null. So setting their scroll position isn't working.
/*Instanciate ScollViews*/
scrollViewLeft = (ScrollableScrollView) findViewById(R.id.scroll_prev);
scrollViewCenter =(ScrollableScrollView) findViewById(R.id.scroll_current);
scrollViewRight = (ScrollableScrollView) findViewById(R.id.scroll_next);
/*Fill views with events*/
setDayEvents(calIns.getPrevCal(),leftDayView);
setDayEvents(calIns.getActualCal(),centerDayView);
setDayEvents(calIns.getNextCal(),rightDayView);
scrollViewLeft.setScrollViewListener(this);
scrollViewCenter.setScrollViewListener(this);
scrollViewRight.setScrollViewListener(this);
So finally my question is, should i use another method to find the scroll position of the two others views ? Or can i force the view to be drawn in a ViewFlipper ?
Thanks :)
Clement.
Try saving the positions in global variables before you change the views and then just apply it to your current view.
Thanks Egor for having answered. I think it is a good idea. I already have a Singleton to handle variables. So i add the variable position with getters and setters.
The probleme here is than even if I override the method "onSizeChanged" in my custom ScrollView, this will apply only for the first view shown.
PS: I'm still starting on Android development and I don't really know how to handle with
interactions on views when they aren't ready (for example getting width when they are not drawn yet).
Edit 1 day later :
Ok i found what was the problem, it is because the size of my views inside the scrollviews wasn't defined (the scrollview was drawn before the view inside it). So by setting the scroll inside the "onSizeChanged" method of the views works well.

How to synchronize content of one view depending on scroll position in sibling ScrollView?

I have extended LinearLayout (vertical) to create a custom compound component. This in turn contains two children:
one custom view that is drawn directly onto the view canvas.
one HorizontalScrollView->LinearView(Horizontal)->Multiple custom views.
I would now like to redraw the custom view to match the visible contents of the scroll view. The reason for this is that the long array of custom components in the scroll view are mainly static and suitable to be drawn ahead of time, while the top view is supposed to be highly dynamic and relate to whatever things are visible in the scroll view.
I hope I made the problem/idea somewhat clear. I am not att all confident this is the best approach, and I'd enjoy hearing any suggestions on alternative solutions or perhaps some idea on how to trigger a redraw-event everytime the scroll position changes in the HorizontalScrollView.
Thank!
You can have your activity listen to the scroll view adapter. in the adapter when ever the scroll position changes you execute the delegate in the Activity.
That way the activity can update the rest of the views upon scroll view change.

Categories

Resources