I have this ObjectAnimator:
cloudAnim2 = ObjectAnimator.ofFloat(cloud2ImageView, "x",500, 1000);
cloudAnim2.setDuration(3000);
cloudAnim2.setRepeatCount(ValueAnimator.INFINITE);
cloudAnim2.setRepeatMode(ValueAnimator.RESTART);
cloudAnim2.start();
cloudAnim2.addListener(new AnimatorListener() {
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
#Override
public void onAnimationStart(Animator animation) {}
});
As you can see, the cloud will start of position 500 and will animate to position 1000, and then it will repeat the animation.
The problem is that the animation is becoming slower as it is near his finish. I mean, the speed of the movement is not allways the same.
I want that the speed becomes allways the same. How can this be done?
thanks
The default interpolator is AccelerateDecelerateInterpolator, so you will need to set it manually to the LinearInterpolator.
animation.setInterpolator(new LinearInterpolator());
Related
I have a question of RotationAnimation.
First.
Is there a Listener to during animation?
start, start animation
repeat, repeat animation
stop, stop animation.
but there are no animation listener to check the on going.
second,
is any other get a current rotate angle of image?
I think, ImageView is rotate by rotationAnimation function.
so I made a timer thread and run 1second
'''
timer = new Timer();
timerTask = new TimerTask() {
public void run(){
Log.e("LOG", " [angle]: " + String.format("%3.1f", rotateImage.getRotation());
}
};
timer.schedule(timerTask, 0, 1000);
'''
but, I can't see the changed value during rotate.
how can I get the current angle during rotate?
thanks.
For Rotation Animation, Yes There is as shown below:
RotateAnimation rotateAnimation = new RotateAnimation();
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You can also use Object Animators to animate Rotation:
ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(targetView, View.ROTATION, startAngle, endAngle);
rotateAnimation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
To get the angle of your ImageView simply use imageView.getRotation(); this will give you an int value of the current angle of rotation.
You also don't need Timer because both ObjectAnimator and rotateAnimator provide time control for you:
rotateAnimation.setDuration(1000); // run animation for 1000 milliseconds or 1 second
rotateAnimation.setStartDelay(1000); // delay animation for 1000 milliseconds or 1 second
Finally, To get rotation Angle DURING the time animation is running there is a listener method called addUpdateListener :
rotateAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int) animation.getAnimatedValue(); // dynamic value of angle
}
});
I have a login screen with some EditText TextView and Button what I want to do is when the login screen is created I want to animate the first EditText from top to bottom then the second EditText from top to bottom after the first EditText is animated, so it should look like all the views are animating from top to bottom one by one.
This is kinda ugly but it'll work.
float pixels = 20f;
view1.animate().translationY(pixels).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
view2.animate().translationY(pixels).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
view3.animate().translationY(pixels);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
Step 1
Animation scaleDown = AnimationUtils.loadAnimation(youContext, R.anim.scale_down);
ImagView v = findViewById(R.id.your_view);
v.startAnimation(scaleDown);//Start Animation
Step 2 Set Animation listener
Step 3 On Animation end start animated next view like above
so on.....
I'm trying to animate a button to fade away, and then set its visibility to VIEW.GONE in order for it to not take up space on the page.
The code is as follows
myContinueButton.animate().setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
myContinueButton.setEnabled(false);
myContinueButton.setVisibility(View.GONE);
}
}).alpha(0f);
The issue is that the button will fade away, but then it will briefly appear in full when the visibility is set in side the onAnimationEnd.
Ive tried setting alpha before the listener, but the result is always the same:
Object fades away, then appears again on screen, then hides as it is set to View.Gone
Try to use this code
myContinueButton.animate().alpha(0f).setDuration(500).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
myContinueButton.setAlpha(0f);
myContinueButton.setEnabled(false);
myContinueButton.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
}).start();
I am trying to achieve something like that
I have white circle image views in Stack currently I am doing like this to animate but it's not working properly please help me what am I doing wrong
this my code for animating but my imageViews falls down all together viewPasscodeis Linear layout to which I add imageViews pragmatically please I need little help
private void animPass() {
float bottomOfScreen = getResources().getDisplayMetrics()
.heightPixels - (viewPasscode.getHeight() * 4);
//bottomOfScreen is where you want to animate to
for (final ImageView imageView1 : passViewsStack) {
imageView1.animate()
.translationY(bottomOfScreen)
.setInterpolator(new AccelerateInterpolator())
.setInterpolator(new BounceInterpolator())
.setDuration(2000).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
viewPasscode.removeView(imageView1);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
All is good. Just keep some difference of duration for respective views i.e. kepp a difference of 300-500 in .setDuration(2000). e.g.,
for view 1: .setDuration(2000)
for view 2: .setDuration(1500)
so on.
you will get expected results.
I have some spots, which are buttons, animating vertically from top to the bottom of the screen. I have implemented the below:
spot.animate().x(x2).y(y2).scaleX(SCALE_X).scaleY(SCALE_Y).setDuration(animationTime).setListener
(
new AnimatorListenerAdapter()
{
#Override
public void onAnimationStart(Animator animation)
{
animators.add(animation);
}
public void onAnimationEnd(Animator animation)
{
animators.remove(animation);
if (!gamePaused )
{
....
}
}
}
);
Question:
I discover that the buttons animating with accelerating speed at the start and decelerating speed upon end of animation.
How could the code be modified with a LinearInterpolator such that the animation is having uniform speed throughout its journey?
Thanks!!
Did you try to use setInterpolator(TimeInterpolator interpolator) method from ViewPropertyAnimator class? So your code would look like:
spot.animate().x(x2).y(y2).scaleX(SCALE_X).scaleY(SCALE_Y).setInterpolator(new LinearInterpolator()).setDuration(animationTime).setListener(
new AnimatorListenerAdapter()
{
#Override
public void onAnimationStart(Animator animation)
{
animators.add(animation);
}
public void onAnimationEnd(Animator animation)
{
animators.remove(animation);
if (!gamePaused )
{
....
}
}
}
);