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();
}
Related
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
}
I want to click the Button it will show the TextViewis visible(perform slidedown animation), then want to click the button again it will perform another animation(slideup). after that don't need to show the TextView.
How do i fix it?
please anyone have an answer to help me.
bclickss.setOnClickListener(new View.OnClickListener() {
boolean visible;
#Override
public void onClick(View v) {
if( visible = !visible) {
tv2.setVisibility(visible ? View.VISIBLE : View.GONE);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
tv2.startAnimation(anim);
}
else {
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
tv2.startAnimation(anim);
tv2.setVisibility(View.GONE);
}
}
});
use this before startAnimation
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
tv2.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
In second animation you need something like this:
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
try this:
//You can add Animation listener to your animation object like
anim .setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
tv2.setVisibility(View.GONE);
}
});
You should use clearAnimation before setting the visibility to View.GONE
anim .setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
tv2.clearAnimation();
tv2.setVisibility(View.GONE);
}
});
I also have the same issue. Please follow below lines:
Animation slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
Animation slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
for making view visible :
view.setVisibility(View.VISIBLE);
view.startAnimation(slide_down);
for hiding it:
view.startAnimation(slide_up);
view.setVisibility(View.GONE);
If works please accept the answer.
I have one suggestion for you to use YOYO Animation lib for android.
It provide verious animations along with call back methods so you can hide your textview once your animation get finished in call back
check here
In my program, I have an ObjectAnimator which moves an ImageView from left to right. I am trying to set up a listener which will execute a task when the ObjectAnimator is finished running. Here is the relevant section of code which I am currently using to try to accomplish this:
if (num == 350) {
nAnim = ObjectAnimator.ofFloat(gamePiece, "translationX", 0, num);
nAnim.setDuration(2125);
nAnim.start();
nAnim.addListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animator a) {
startGame(level);
}
#Override
public void onAnimationStart(Animator a) {
}
#Override
public void onAnimationCancel(Animator a) {
}
#Override
public void onAnimationRepeat(Animator a) {
}
});
When I try to run this in Android Studio, I am getting the error: MainActivity is not abstract and does not override abstract method onAnimationStart() in MainActivity. What do I have to do to fix this error?
Since you implemented AnimatorListener in your MainActivity, you must include all its abstract methods, and change nAnim.addListener(new Animat.... to nAnim.addListener(this)
#Override
public void onAnimationStart(Animator animation){
}
#Override
public void onAnimationEnd(Animator animation){
startGame(level)
}
#Override
public void onAnimationRepeat(Animator animation){
}
#Override
public void onAnimationCancel(Animator animation){
}
You should use AnimatorListener class instead of AnimationListener like below
nAnim.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
Or switch to Kotlin and put animator.start() as the last line. Offcourse you don't have to implement all animations methods
val gamePiece = Button(this)
var num = 0
val animator = ObjectAnimator.ofFloat(gamePiece, View.TRANSLATION_X, 0f, num.toFloat())
animator.duration = 2125
if (num ==350){
animator.addListener(object : AnimatorListenerAdapter(){
override fun onAnimationStart(animation: Animator?) {
super.onAnimationStart(animation)
}
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
startGame(level)
}
})
}
//animator.start() should be the last line
animator.start()
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);
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) {
}
});