Animate layout size change - android

I have an activity like below screen.
View 1 have width and height as match_parent respectively. View 2 have width match_parent and fixed (200 dp) height.
On a button click I change the visibility (from visible to gone) of View 2. As soon as I hide View 2, View 1 comes in full screen instantly or with a jerk. I want the View 1 to gradually expand to full screen with animation instead of instantly.
How can I animate this layout size change?

You can set android:animateLayoutChanges="true" on the parent layout. This works from API 11 and onward.

You need to use scale animation for view1 and fade out for view2 and then use Animatorset to run both at the same time. For example for a simple scale animation
ScaleAnimation anim = new ScaleAnimation(fXscale, toXscale, fYscale, tYscale, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);//fix to fit your needs.

Related

New layout sliding in from bottom pushing the entire layout upwards

I am trying to create a screen, on some operation it is required to slide a layout from bottom to the height to match it's length, with a translation animation kind of slide.
Have a look at the screen example:
As you can see the initial screen layout, the new view 5 slides in from bottom, pushing the entire layout upwards with an animation.
I have tried this: https://stackoverflow.com/a/19766034/1085742
Using the above link, the new view is visible with animation but screen is not sliding down to show the new view 5.
Any idea how to do this!!
Thanks in advance.
I also tested this. Apparently an animation with translateY or Y does not change the layout bounds. In other words, the other views do not move up. Neither when using an Animation nor an Animator.
One solution is to animate the margins, if the animated view is in a layout that supports margins. In Kotlin:
val collapse = ValueAnimator.ofInt(0, -someLayoutAndViews1.height)
collapse.addUpdateListener {
val params = someLayoutAndViews1.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = it.animatedValue as Int
someLayoutAndViews1.layoutParams = params
}
collapse.start()

Animating view: set translationY to 0 while also scaling

I have a view that I want to animate- scale it to a bigger size and at the same time move to the very top of the screen, just below the notification bar. The problem is however, at least I think, that the translationY complies with initial view scale and moves it to the top of the screen, but as the view grows, it gets cut off at top.
This is the code I'm using
image.animate()
.alpha(1f)
.translationY(0)
.scaleX(6f)
.scaleY(6f)
.start()
Any simple solution to this? A way I could calculate the real value I should set translationY to (based on scale) in order to be positioned correctly? Any help appreciated!

How Click on Animated View or Object in Android?

I have an ImageButton with TranslateX Animation Right to Left like Merquee, So it animated right to left. Now when i click on that nothing happened. Actually click is not perform and click only perform on real position of imagebutton.
what to do any suggestion? greatly appriciate... Thanks
Use ObjectAnimator(For later version then Honeycomb) for Animatinfg Your Objects, You can use follwing code for references:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout1);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
ObjectAnimator mover = ObjectAnimator.ofFloat(layout, "translationX",
width, -width);
mover.setDuration(10000);
mover.setRepeatMode(Animation.INFINITE);
mover.setRepeatCount(Animation.INFINITE);
mover.start();
If you Are Using Api lower Then the HoneyComb(Like Gingerbread) then Use this Library: http://nineoldandroids.com/
It will Working As its Working in my devices.
As of now, the Animations which alter a View's matrix Only change the co-ordinates where the view is drawn, and not the actual location of View in Layout. So, its just a canvas transform when onDraw() of that View is being called.
So, you can setTranslationX(100) and view will be drawn a 100 pixels to right. But, the click area (getHitRect()) is still on the same place which was assigned to view on layout pass.
Or, you can actually place the view where it should be after animation, and run the animation in reverse.
If you want to actually alter the layout, you will have to alter that view's LayoutParams and change width/height/margin on it. Then you will have to requestLayout() on each frame of animation.
Example : This will animate left margin of a view inside a FrameLayout:
//---assuming animated view is a child of Framelayout---
FrameLayout parent;
View animatedChild;
ValueAnimator animator = new ValueAnimator();
animator.setFloatValues(0,parent.getWidth()); //--slide out to right--
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
FrameLayout.LayoutParams params = animatedChild.getLayoutParams();
Float margin = (Float) valueAnimator.getAnimatedValue();
params.leftMargin = margin.intValue();
animatedChild.requestLayout();
}
});
animator.start();
I run into similar problems before when developing my android app. And I found when animation is running, it is actually making the image layer is flowing around. So you need to click on the original position of the button, which means you may need to click on a black space if the button has started moving.

Change View height programmatically Smoothly

I am trying to change the height of the view programmatically.
What i have tried is -
RelativeLayout rlOne;
rlOne = (RelativeLayout) findViewById(R.id.rlOne);
And on some click event i am changing the height using the LayoutParams.
rlOne.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, NEWHEIGHT));
The height is increased but the transition is not smooth.
How can i achieve a smooth transition from view's original height to view's new height?
Can i slow down the transition ?
Basically the idea is that, you calculate the new height (the height after your view increasing) view first and write an thread, increase the height of you old view (the height of your view before increasing) pixel by pixel(may be 5-10 pixels at a time) until it reaches the new height. Hope the idea could help.
Play with from/to/speed values
ScaleAnimation scaleAnimation = new ScaleAnimation(from, to, from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rlOne.setAnimation(scaleAnimation);
Source:
https://stackoverflow.com/a/1624689/1276374
You could write a for loop, where the view's height is increased by very little, than a pause is made before increasing the counter. In this way, the user will have the impression of a smooth transition.
Use
LinearLayout linearLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

How to position a view off-screen so that it can be Animated to move on-screen?

I have a RelativeLayout filling the screen and a couple of ImageView positioned on it using LayoutParams margins. These ImageView are animated in different ways, and in one case I want an image to "fly in" to the screen from the right.
Unfortunately, if I set leftMargin for that ImageView greater than the width of the screen, it does not appear (or appears cropped if it partially visible at the start of animation).
I tried setting width and height of RelativeLayout to be bigger than screen size - it works, but only partially: if the image is positioned completely off-screen, it does not work, and if the image is partially visible, it is not cropped, but that works only for right and bottom sides.
So, my question is this: how to position several ImageViews on and off the screen so that I can animate them using Animation?
In the end, I used a trick: I combined AnimationDrawable and view animation. I did the following (assuming that the animation has to run T milliseconds):
Position that ImageView on-screen.
Set as its background an AnimationDrawable with following frames:
empty Drawable: 1ms,
normal Drawable: Tms.
Change view's animation to this:
jump to off-screen position (translation with duration=1ms),
do normal animation.
This is probably a good place to start looking for those who would use Fixpoint's solution.
Android — How to position View off-screen?

Categories

Resources