I have been having a lot of trouble lately trying to get my custom ViewGroup to animate (scale/translate animation) independently it's child views. Worth noting is that my custom ViewGroup is visible and not just a container, thus the need to animate it and not only it's children.
I want my ViewGroup to have 2 states, unexpanded and expanded with the former being able to display 1 view and the later being able to display that view in addition to 4 more (total 5).
The animation my ViewGroup uses to enter/exit the expanded state is done and i am very happy with it. Though when adding a View into the ViewGroup it to will expand with the same animation as my ViewGroup which i definitely do not want.
Ok, that was a lot of explaining (hope you understood half of it) but now comes what i want to achieve. I want to control my ViewGroup with one animator and all the child Views with another independent animator.
I have a feeling i am going at this the wrong way so if anyone has something easier/better to suggest than please do.
Thanks!
I solved my problem by using a ValueAnimator and animating the LayoutParams of my ViewGroup :)
Related
I'm currently trying to design a ViewGroup (specifically a subclass of FrameLayout) that can layout any number of subviews and enable them for drag/drop outside the layout group. It's almost identical to a LinearLayout:
Curently I am able to drag the views outside the ViewGroup and draw them, however after letting go of the view it can't be re-selected and further dragged around.
I'm wondering if there is a way to do this in a way that isolates the logic to the Layout subclass and doesn't involve needing to do much/any extra stuff in consuming fragments/view groups.
I've tried overriding getHitRect(Rect) in my FrameLayout subclass but it never seems to be called. dispatchTouchEvent(MotionEvent) is of course not called either, presumably because the parent ViewGroup has decided not to deliver the touch to it because it was outside the bounds. I've tried implementing a TouchDelegate as well, but I think that needs to be set on the parent view, which means needing to know about this and doing this additional step when using this Layout.
Any ideas on if/how this is possible? On iOS it can be implemented fairly easily by overriding hitTest: to take into account the frames of the child views. Is there a similar method like this on Android?
Your help is greatly appreciated!
Without any code it is very hard to see what you are doing, but best guess would be that each view should a child view of the relative layout.
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 cannot get a view outside the view hierarchy to update it's drawing state. I have a view that i handle the drawing and measuring of as i for a couple of reasons cannot let the container view do this. This works great until i start dealing with views that contain an animation. Even though i repeatedly draw the view it's animation does not change progress. Without knowing that much about the underlying view system i am guessing that this is because animations get updated during traversal of the view tree. because my view is not part of this tree it will not get updated.
The view i am talking about is the "sticky" view in this library https://github.com/emilsjolander/StickyListHeaders
The reason why i handle all drawing of this sticky view myself is because i cannot add i subview to the ListView. Wrapping the ListView together with the sticky view in a FrameLayout has also been tried (fixing this issue but causing other unwanted ones).
So what i want to know is if there is some way to manage all this myself, preferably without the use of reflection. I am also open to suggestions of how to let the underlying view system handle this view!
In Android ViewGroup inherits from View. A ViewGroup is a container which holds Views.
ViewGroup (LinearLayout)
View (TextView)
Why did folks at Android defined this relationship as Inheritance instead of composition. As the ViewGroup contains Views shouldn't it be composition ?
I think you're getting too hung up on the wording.
A "ViewGroup" has every bit as much reason to inherit from a "View" as a "TextView", and "ImageView" or ... more to the point ... a "ScrollView" or a "SurfaceView" (the latter two both "contain things").
Perhaps "View" wasn't necessarily the best choice of terms ... but the class heirarchy makes complete sense. Regardless of what it's subclasses are named :)
IMHO ...
I think this is a great example of the Composite design pattern:
http://en.wikipedia.org/wiki/Composite_pattern
Even though the naming might not be the best...
Reading the official doc is the golden rule.
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.
If you still do not find out what it is, search with Google image:
A ViewGroup is a (subclass of) View because it can serve as a view in important ways:
It can be an element in a layout XML file
It can be displayed on the screen (by displaying its child views, its own background color, etc.)
It gets inflated along with the rest of the view hierarchy
It can serve as an activity's content view (via setContentView())
So it really is a View.
I agree that the classname ViewGroup is a bit confusing, because it sounds like it's a group, not a view. Calling it ViewGroupView might have been more logical, if unwieldy.
Why did folks at Android define this relationship as Inheritance instead of composition? As the ViewGroup contains Views shouldn't it be composition?
In a case like this, inheritance and composition are not mutually exclusive. A ViewGroup is a View (inheritance) and a ViewGroup can contain Views (composition).
Viewgroup inherits properties of views and does more with other views and viewgroup
A ViewGroup is a special view that can contain other views.
- The view group is the base class for layouts and views containers
- For example, RelativeLayout is the ViewGroup that contains TextView(View), and other Layouts also.
refer link for info:
https://developer.android.com/reference/android/view/ViewGroup.html
A View represents the basic building block for user interface components
-It occupies rectangle on the screen and is responsible for drawing and handling events.
- Examples are EditText, Button, TextView etc
refer link for info:
https://developer.android.com/reference/android/view/View.html
All UI elements in an
Android app are built using View
and ViewGroup objects.
View :- A View is an object that draws
something on the screen that the
user can interact with.
ViewGroup :- ViewGroup is used to hold Views
and ViewGroups.
I made a custom viewgroup, and I need to use view animation to rotate some views...
But they are misbehaving, no matter if I put the startanimation inside onDraw, onLayout or onMeasure, my views flicker (you see them briefly rotated, and then briefly non-rotated, and that pattern repeats).
How I fix that?
see Android transfer animation of view in custom viewgroup and Animating Views
view.animate().rotation(180);
Does your animations work properly in a non-custom viewgroup?
I tried to use a custom ViewGroup in order to create an animated background, however, just like you, I had some strange behaviour. I solved it by simply using a FrameLayout and inserted two fill_parent views: a custom one and a LinearLayout. Then the LinearLayout which came on top of my custom view was working as expected.