How to repeat Android animation in java? - java

i have this code here. I want that the Imageview do the animation 5 times. How i need to set the setRepeatCount? How to inilize it?
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = ((ImageView) findViewById(R.id.imageView));
iv.setRotationY(0f);
//iv.animate().setDuration(10);
iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
iv.animate().rotationY(360f).setListener(null);
}
#Override
public void onAnimationCancel(Animator animation) {
}
});

The method setRepeatCount belongs to Animation class, you are using Animator.
Try this:
int times = 5;
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = findViewById(R.id.imageView);
iv.setRotationY(0f);
//iv.animate().setDuration(10);
final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
viewPropertyAnimator.rotationY(90f);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
times--;
if (times > 0) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
viewPropertyAnimator.rotationY(360f);
viewPropertyAnimator.start(); //Restart
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
});
viewPropertyAnimator.start(); //Init
}

Related

How can I expand/collapse an EditText when I click on it?

I want to create an animation to expand an EditText when I click on it(to a height set by me), and when it loses focus(I click on something else) it should collapse back to one line. This is what I tried, but it kinda does nothing..
public class CollapseDownAnimation extends Animation {
private EditText mDescription;
private boolean mDown;
public CollapseDownAnimation(EditText description, boolean down) {
this.mDescription=description;
this.mDown=down;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation transformation) {
int newHeight;
if(mDown) {
newHeight=(int) (300 * interpolatedTime);
} else {
newHeight=(int) (300 * (1-interpolatedTime));
}
mDescription.getLayoutParams().height=newHeight;
mDescription.requestLayout();
}
#Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
#Override
public boolean willChangeBounds() {
return true;
}
}
EditText mDescription;
mDescription = view.findViewById(R.id.description);
CollapseDownAnimation anim = new CollapseDownAnimation(mDescription, mDescription.hasFocus());
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Log.d(TAG, "the focus is "+mDescription.hasFocus());
mDescription.setAnimation(anim);

Android animation problem - visibility change faster than animation

I have a problem with adjusting the animation to change the visibility of the view.
I've read about various tips, but the recommended solutions do not help me. Animation doesn't work smoothly - what am I doing wrong?
My code looks like this:
childRelativeLayout.setVisibility(View.GONE);
parentRelativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (childRelativeLayout.getVisibility() == View.VISIBLE) {
Animation slide_up = AnimationUtils.loadAnimation(context, R.anim.slide_up);
childRelativeLayout.startAnimation(slide_up);
//-------
childRelativeLayout.getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
//-------
//OR
//-------
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//-------
childRelativeLayout.setVisibility(View.GONE);
} else {
Animation slide_down = AnimationUtils.loadAnimation(context, R.anim.slide_down);
childRelativeLayout.startAnimation(slide_down);
childRelativeLayout.setVisibility(View.VISIBLE);
}
}
});
slide_down.xml
<translate
android:duration="200"
android:fromYDelta="-100%"
android:toYDelta="0" />
slide_up.xml
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="-100%" />
you need to remove thread.Sleep() and add animation listner and set visibility GONE in animation end listner.
slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
childRelativeLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
and for slide_down animation
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
childRelativeLayout.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I use Kotlin but in Java it will look the same
val animatorSet = AnimatorSet()
val positionAnimator = ValueAnimator.ofFloat(tv.x, 0F)
positionAnimator.duration = animationDuration
positionAnimator.addUpdateListener {
tv.x = positionAnimator.animatedValue as Float
}
val alphaAnimation = ValueAnimator.ofFloat(1F, 0F, 1F)
alphaAnimation.duration = animationDuration
alphaAnimation.addUpdateListener {
tv.alpha = alphaAnimation.animatedValue as Float
}
animatorSet.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
//If you need some events
}
})
positionAnimator.interpolator = AccelerateDecelerateInterpolator()
animatorSet.playTogether(positionAnimator, alphaAnimation)
animatorSet.start()
For JAVA. It's only example.
private void animate() {
animatorSet = new AnimatorSet();
ValueAnimator positionAnimator = ValueAnimator.ofFloat(0, 100F);
positionAnimator.setDuration(200);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setX((Float) valueAnimator.getAnimatedValue());
}
});
ValueAnimator alphaAnimation = ValueAnimator.ofFloat(0f, 1F);
positionAnimator.setDuration(200);
positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setAlpha((Float) valueAnimator.getAnimatedValue());
}
});
animatorSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation, boolean isReverse) {
}
#Override
public void onAnimationEnd(Animator animation, boolean isReverse) {
}
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
animatorSet.playTogether(positionAnimator, alphaAnimation);
animatorSet.start();
}

Make something happen when ImageView animation is over

Let's say i have the following code:
img1.animate().alpha(.5f).setDuration(300);
And I want to execute this whenever the animation is over
img1.setImageReasource(R.drawble.img_guitar);
How do I do that??
You can add a listener to your animation:
imageView.animate().setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageView.setImageResource(R.drawable.img_guitar);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You can also use withActionEnd(Runnable):
imageView.animate().withEndAction(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.img_guitar);
}
})

Start Animation repeatedly every minutes in android app

I want my animation start repeatedly every 2 minutes in my android app, as my code it doesn't work. Please help on my code below. Thanks in advance.
TableLayout = (TableLayout) this.findViewById(R.id.table01);
slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
TableLayout.startAnimation(slide_down);
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_up);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Check out repeating animation with Timer
I agree with the accepted answer that a Handler can be used with postDelayed and a Runnable
TableLayout = (TableLayout) this.findViewById(R.id.table01);
slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
TableLayout.startAnimation(slide_down);
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_up);
slide_up.setStartOffset(2000); // delay in ms
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
TableLayout.startAnimation(slide_down);
slide_down.setStartOffset(2000); // delay in ms
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I hope it will work!
Pls use:
TimerTask slideDownTask= new TimerTask() {
public void run() {
TableLayout.startAnimation(slide_down);
});
}};
new Timer().schedule(slideDownTask, 0, 120000);

on end listener for property animation

This question shows how you set listeners for an android view animation, but it doesn't work for property animations.
How can I achieve the same thing with a property animation?
my animation:
ViewPropertyAnimator viewPropertyAnimator = layout.animate().y(integer).setInterpolator(interpolator).setStartDelay(delay).setDuration(duration);
Try this:
viewPropertyAnimator.setListener(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) {
}
});

Categories