Android property animation relative to view's height - android

I initialize ObjectAnimator like this;
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", 0f, view.getHeight());
But when the first time animation runs, view.getHeight() method returns 0. So it does not animate. Second time i run the animation, it animates right. Is there a way to translate views relative to their width or height properly?

Related

How to make animation using ObjectAnimator?

My application is used in landscape orientation. What I want to do is move an element from off the screen at all until it enters the screen and then when pressing a button, it leaves the screen again by scrolling to the left.
The animation starts from the default position of the view (i.e the position you have specified in your xml file). if you do not what the animation to start from that position, you can either:
change the initial position of the view in the xml file.
or set the x and y coordinates of the view at run time and then start your animation like so:
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
All you need is a TranslationX animation, see below, or check out this link. https://developer.android.com/training/animation/reposition-view
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(view, "translationY", deltaY);
If you have any question, please feel free.:)

moving and rotating view in android

I'm using this code to moving view from x Position to another x Position :
int xStart=100,xEnd=500;
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(view, "translationX", xStart, xEnd);
objectAnimator.setDuration(duration);
objectAnimator.start();
but i need moving and rotating .
how can Rotate and move the view together ?
It is possible ?
You can use ObjectAnimator to animate any property of a View- essentially any property with set___() and get___() methods.
For rotation, you can use "rotation", "rotationX", and "rotationY" as appropriate.
It sounds like you already have translation working correctly, so I'm not sure what else you are looking for in "moving" the View.
To play multiple animations together, you can use an AnimatorSet. To move and rotate at the same time, you might do this:
AnimatorSet animations = new AnimatorSet();
ObjectAnimator translationAnim= ObjectAnimator.ofFloat(view, "translationX", 100, 500);
ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(view, "rotation", 0, 90);
animations.play(rotationAnim).with(translationAnim);
animations.start();
For more information see the Property Animation documentation.

Android: Shrink animation using animator

I am animating in the following way,
There are 2 layouts (both are relative layouts), layout1, which will be hidden initially, layout2 will be shown to the user initially.
once the animation starts layout1, will be made visible and layout2 will be scaled using the animator as follows,
Animator scalexAnimator = ObjectAnimator.ofFloat(layout2, "scaleX", 1f, 0f);
Animator scaleyAnimator = ObjectAnimator.ofFloat(layout2, "scaleY", 1f, 0f);
Animator pivotxAnimator = ObjectAnimator.ofFloat(layout2, "pivotX", <maxWidth>);
Animator pivotyAnimator = ObjectAnimator.ofFloat(layout2, "pivotY", <maxheight/2>);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(pivotxAnimator, pivotyAnimator, scalexAnimator, scaleyAnimator);
animatorSet.start();
This starts the animation and the layout2 gets shrinked,but the aspect ratio is never maintained, what I mean to say is the animation follows these steps,
Firstly the height reduces.
Next the width gets reduced.
Height reduces again.
Width again.
this goes on and it will stop once the final given ratio(0.5) is achieved.
This does not give shrinking effect, I tried with animation class as follows,
Animation shrinkAnimation = new ScaleAnimation(1,0.5f,1,05f,Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF, 0.5f);
layout2.startAnimation(shrinkAnimation);
this will work perfectly and I will get a exact shrinking effect, aspect ratio is maintained, but the problem I face in the above implementation is the position of the layout2 is changed, but the button retain their position (layout2 has 4 buttons and button click works on the old button positions, even though the user can't see any buttons there, I read in some posts that this is expected), so I had to disable the buttons manually, this is a overhead so I wanted to avoid using Animation class, and went for ObjectAnimator.
So is there a way in ObjectAnimator to get the same shrinking effect?

ImageView not reseting to original location after animation

I have a swipe recognizer that calls this function when a swipe is detected. The alpha animation works great, but after the animation, the image is not resetting to its original location in the center.
Currently, the image stays on the left.... I've searched all over and it seems that I'm implementing the animation correctly.
I'm using a RelativeLayout and swipeImageView is an empty imageview in the middle of my layout.
swipeImageView.bringToFront();
swipeImageView.setImageDrawable(getResources().getDrawable(R.drawable.skip4x));
ObjectAnimator nextGroupLeft = ObjectAnimator.ofFloat(swipeImageView, "x", -250f);
nextGroupLeft.setDuration(1000);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(swipeImageView, "alpha", 1f, 0f);
fadeAnim.setDuration(1000);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(nextGroupLeft, fadeAnim);
animSetXY.start();
Trying clearing the animations. When this line is executed, the animations should stop and the ImageView should return to its original state.
swipeImageView.clearAnimation();

Android: use animation outside the view area

I have an xml with 12 image. for example 3*4 table, where each cell contains an image.
In the OnCreate I write animation every image:
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.ABSOLUTE, -300.0f, Animation.START_ON_FIRST_FRAME, 0.0f,
Animation.ABSOLUTE, -800.0f, Animation.START_ON_FIRST_FRAME, 0.0f);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setDuration(5000);
I want to start the animation from the top left of the screen and to end the animation to real position. But the animation is only use the image or cell region. Is there any solution to the animation, use the whole screen?
If they're all under the same parent view, you can set the parent's clipChildren attribute to false, which would allow the animation to extend beyond its own constraints (as long as it still remains within the boundaries of its parent):
android:clipChildren="false"

Categories

Resources