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.
Related
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.:)
I'm quite new to animation transition. I want to add an animation in the home screen. I don't know which way to go, is Lottie fit for this or official android libraries.
The animation take about 2-3 seconds and should animate every time I go to that specific screen. Please help, some explanations would be great Here's some screenshots:
You can use ObjectAnimator and AnimatorSet.
Translate that blocks to bottom in order and with some delay.
The code you need is something like that:
ObjectAnimator translateAnimator1 = ObjectAnimator.ofFloat(view1, "translationY", 0, 500).setDuration(2500);
ObjectAnimator translateAnimator2 = ObjectAnimator.ofFloat(view2, "translationY", 0, 500).setDuration(2500);
translateAnimator2.setStartDelay(500);
ObjectAnimator translateAnimator3 = ObjectAnimator.ofFloat(view3, "translationY", 0, 500).setDuration(2500);
translateAnimator3.setStartDelay(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(translateAnimator1).with(translateAnimator2).with(translateAnimator3);
animatorSet.start();
ObjectAnimator and AnimatorSet has some extra properties like setRepeatCount etc. that maybe useful.
I want to display a buttons with animations like fade-in along with the translate animation programmatically. I have to start translate animation from a specific position on the screen. So i need to pass the x and y values for the translate animation to start from.
Im using the following code for translate animation but how to add fade-in animation to the existing translate animation?
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", maxX),
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, 100);
tanim.setDuration(3000);
I've used Animations several times before and they always left me scratching my head. Some things I would recommend you should look into are Object Animators and Animator Sets. They're really useful, as they allow you to animate any property of the given view. As long as the view has a setter method for that property, you can add it to the animator set by passing in the string name of the property to the Animator constructor. The AnimatorSet class also allows you to easily choreograph multiple animations.
In your case, you could do something roughly like the following:
AnimatorSet animations = new AnimatorSet();
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue);
xAnim.setDuration(3000);
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue);
yAnim.setDuration(3000);
Animator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", finalAlphaValue);
alphaAnim.setDuration(alphaDuration);
//Play all the animations together
animations.play(xAnim).with(yAnim).with(alphaAnim);
You can string together as many animators as you want this way, and you can also set animators to be played in a sequence using the .before() and .after() methods rather than the .with() method.
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();
I want to change the imageview (x,y) values by using the setX(),setY(). It is directly going like an visible/gone. I just wants to keep this in a small animation so that it will go slowly to the given cordinates.
Is there any basic animation for performing this.
You should use ObjectAnimator and AnimatorSet to achieve this.
Use Following code:
ObjectAnimator oaX = ObjectAnimator.ofFloat(view, "translationX", 0, xValue);
oaX.setDuration(1000);
ObjectAnimator oaY = ObjectAnimator.ofFloat(view, "translationY", 0, yValue);
oaY.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(oaX, oaY);
animatorSet.start();
pass your coordinate values to xValue and yValue, these are relative to your image's actual coordinates.
The easiest way to move your imageview is here:
imageview.animate().translationX(imageview.getWidth()).setDuration(1000).setInterpolator(new AccelerateDecelerateInterpolator());