I am trying to remove some Views from a ScrollView, but do so with a nice animation effect.
Unfortunately, android:animateLayoutChanges does not appear to work inside ScrollView with multiple ViewGroups. More precisely, in that case you can only apply it to a subset view. In such a situation, however, the other views will still adjust by jumping into position.
I have used TranslateAnimation to slide everything inside a ScrollView into place, removing the views onAnimationEnd. This appears only to work if a ScrollView does not scroll (content smaller than ScrollView size).
Simplified code:
Animation animation = new TranslateAnimation(0, 0, 0, -ViewToBeRemoved.getHeight());
animation.setAnimationListener(new AnimationListener() {
...
#Override
public void onAnimationEnd(Animation animation) {
viewToBeRemoved.setVisibility(View.GONE);
}
}
scrollViewChildView.startAnimation(animation);
Is there a way to remove Views from a ScrollView with an animation?
Related
I want to make my Scrollview always scrollable even though it has no element in it.
I tried to extend a LinearLayout out of the screen, but it does not work.
csv=(ScrollView) findViewById(R.id.scrView);
csv.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY= csv.getScrollY();
txtview.setText(String.valueOf(scrollY));
}
I want it because I want to see the image position which is the only one element in the ScrollView.
By this, I can move the image and see its y position.
It can be thought as vertical ViewPager.
I have a LinearLayout (with horizontal orientation) inside a HorizontalScrollView. When the user clicks on an element of the LinearLayout, I want it to always scroll so that the selected element is flush with the left side of the ScrollView.
Right now, the following code in my OnClickListener usually accomplishes what I want:
ReadView readView = (ReadView) view;
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
Except in cases where the element is near the end of the LinearLayout, which makes sense. In an attempt to solve this, I add padding to the right side of the LinearLayout:
ReadView readView = (ReadView) view;
horizontalLayout.setPadding(0,0, 1000, 0);
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
Except...that doesn't work. I've verified that the padding is being added correctly, but the ScrollView isn't scrolling based on the padding.
I want to be able to remove the padding afterward, so that when the user scrolls without clicking, they won't see the extra padding, so I'd prefer to be able to do this in the OnClickListener.
I found a solution: use a ViewTreeObserver.
ViewTreeObserver vto = horizontalLayout.getViewTreeObserver();
vto.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
#Override
public void onDraw() {
vto.removeOnDrawListener(this);
horizontalScrollView.smoothScrollTo(readView.getLeft(), 0);
/* ... */
}
});
horizontalLayout.setPadding(0,0, 1000, 0);
It's easy to add layout transitions with this attribute:
android:animateLayoutChanges="true"
However, the animation you get does not create a pleasing user experience. When elements are added to the layout (I'm using a simple vertical LinearLayout) or change from gone to visible there's a 2-stage process that I think is rather annoying. First, room is prepared for the new element (everything else is pushed down). Then when there's enough room, the new view fades into existence. Likewise, when a view is removed or changes from visible to gone, first it fades out, then the room claimed by it gradually shrinks to zero.
I would really like a way to change the animation to what I really think is the natural way to do it: When adding a view its height gradually changes from zero to its full size, so that first you see just the top, without ever changing the alpha. When removing a view its height gradually changes to its full size to zero, so that near the end of the animation you see just the top, without ever changing the alpha.
How can I accomplish this in Android? (Note: the user can tap on several buttons together and cause several elements to appear / disappear in quick succession, before the animation for the other views ended - or even make something appear while it's still appearing).
Another question that this is perhaps not the place to ask: why isn't this the default?
(And if it's possible, can a slightly different behavior be specified in which first just the bottom of the view appears, rather than the top, like the new view slides down from under the one above it?)
You have to write your own animator and set it.
Code:
final ViewGroup profileParent = (ViewGroup) view.findViewById(R.id.profileParent);
LayoutTransition transition = new LayoutTransition();
Animator appearingAnimation = ObjectAnimator.ofFloat(null, "translationY", 600/*profileParent.getHeight()*/, 0);
appearingAnimation.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setTranslationY(0f);
}
});
Animator disappearingAnimation = ObjectAnimator.ofFloat(null, "translationY", 0, 600/*profileParent.getHeight()*/);
appearingAnimation.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setTranslationY(0f);
}
});
transition.setAnimator(LayoutTransition.APPEARING, appearingAnimation);
transition.setDuration(LayoutTransition.APPEARING, 300);
transition.setStartDelay(LayoutTransition.APPEARING, 0);
transition.setAnimator(LayoutTransition.DISAPPEARING, disappearingAnimation);
transition.setDuration(LayoutTransition.DISAPPEARING, 300);
transition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
profileParent.setLayoutTransition(transition);
i want to create Flip vertical animation in my application but all of my found document are flip horizontal and i can not find any document about flip vertical by xml or java class
You can achieve this by putting two views of the same size one below the other and use the ViewPropertyAnimator like this:
firstView.animate().rotationX(90).setDuration(200).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
firstView.setVisibility(View.GONE);
secondView.setRotationX(-90);
secondView.setVisibility(View.VISIBLE);
secondView.animate().rotationX(0).setDuration(200).setListener(null);
}
});
The first view is visible when starts, and the second one, obviously it's invisible.
I've tried several ways of hiding a view and then removing it from the parent layout:
Call an alpha fade animation followed by a call to setVisibility(GONE);
Call an alpha fade animation followed by a call to setVisibility(GONE) inside of the AnimationListener
Call an alpha fade animation followed by removing the parent layout inside of the AnimationListener.
Each time, the resulting animation fails --- the view disappears twice from the screen. The alpha fade animation works fine but when you change the visibility or remove it from the parent view, it quickly reappears again before disappearing a second time. The result is an unexpected jittery animation.
Example code:
Animation animation = AnimationUtils.loadAnimation(AddTaskActivity.this,
R.anim.fade_out);
final LinearLayout parentView = (LinearLayout) findViewById(R.id.addtask_root);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
parentView.post(new Runnable() {
public void run() {
parentView.removeView(test);
}
});
}
That animation fails---the test view disappears twice from view.
Any ideas?
I guess that if you transparent your view before remove it or change it's visibility,you can do what you want:
#Override
public void onAnimationEnd(Animation animation) {
parentView.post(new Runnable() {
public void run() {
// transparent your view
...
parentView.removeView(test);
}
});
Edit:
I saw android documentation in about animations and it says:
Another disadvantage of the view animation system is that it only
modified where the View was drawn, and not the actual View itself. For
instance, if you animated a button to move across the screen, the
button draws correctly, but the actual location where you can click
the button does not change, so you have to implement your own logic to
handle this.
With the property animation system, these constraints are completely
removed, and you can animate any property of any object (Views and
non-Views) and the object itself is actually modified. The property
animation system is also more robust in the way it carries out
animation. At a high level, you assign animators to the properties
that you want to animate, such as color, position, or size and can
define aspects of the animation such as interpolation and
synchronization of multiple animators.
So I guess that you have to use property animation.