TextView animation and remove - android

When I swipe right on a TextView, I want to start the animation and remove it, but the TextView is being removed without the animation. Any ideas on how to fix this, and why this would not work? Thank you.
public void onSwipeRight() {
textview.animate().translationX(500).alpha(0).setDuration(150).start();
linearlayout.removeView(textview);
}

You need to wait for the animation to complete before you remove the view:
textview.animate()
.translationX(500)
.alpha(0)
.setDuration(150)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
linearlayout.removeView(textview);
}
})
.start();
Note: Since alpha(int) is deprecated you need to use:
textview.animate()
.translationX(500)
.alpha(0f) //float value
.setDuration(150)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
linearlayout.removeView(textview);
}
})
.start();

Related

How to place a view below other just animated?

i started learning animation on android today, and i'm having trouble positioning views after animations end.
When app starts, It shows an search EditText in the center of the screen, and when you search for something, this EditText should slide to top using this code below
public void slideToTop(View view){
view.animate()
.translationY((-parent.getHeight()/2) + view.getHeight())
.setInterpolator(new AccelerateInterpolator())
.setDuration(500)
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
layoutContent.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
fadeOutAndHideImage(logo);
}
How can I show the results below the EditText that now is on top of the layout?
I tried to use layout_below attribute from RelativeLayout, the set the visibility when the animation ends, but it seems that the position is calculated before the animation
You have to set position of your view programatically to (-parent.getHeight()/2) + view.getHeight() using Layout Params when the animation ends, this might help you to solve your problem.

Animations don't work

I don't know what's happening, but most of my animations don't work. For example:
nothingToShow.animate().alpha(1f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
nothingToShow.setAlpha(0f);
nothingToShow.setVisibility(View.VISIBLE);
super.onAnimationStart(animation);
}
});
deleteAll.animate().alpha(0f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
deleteAll.setVisibility(View.GONE);
super.onAnimationEnd(animation);
}
});
searchImg.animate().alpha(0f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
searchImg.setVisibility(View.GONE);
super.onAnimationEnd(animation);
}
});
nothingToShow appears, but the other two Views don't dissapear. Most of the animations which aren't working are from alpha 1f to 0f, but not all of them. Some are working. That is very strange. Any idea?
.setDuration() takes a time in milliseconds, not a resource ID (docs)
getResources().getInteger(android.R.integer.config_shortAnimTime)

Android FAB Animation when new Activity starts or Resume to previous Activity

I want to animate my FAB (Floating Action Button).
My goal is presented in this youtube video:
Video
Does anybody know how I can animate my FAB and how will look like the animation in the xml file?
Thanks.
public void hideView(final View view) {
view.animate()
.translationY(view.getHeight()+YOUR MARGIN)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
public void showView(final View view) {
view.animate()
.translationY(0)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
EDITED:
for i sec wait call it from :
handler.postDelayed(new Runnable(){
#Override
public void run() {
showView(...)
}
}, 1000);
you might have to use runOnUiThread(new Runnable(...)) inside run
Re Edit:
public void hideView(final View view) {
view.animate()
.translationY(-10)
.setDuration(30)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.animate()
.translationY(view.getHeight()+YOUR MARGIN + 10)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
});
}

android animation with LinearInterpolator

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 )
{
....
}
}
}
);

How do I do something when an animation finishes?

I have an ImageView that I use to show progress via an AnimationDrawable. When I want to show my progress spinner, I do this:
animDrawable.start();
ObjectAnimator.ofFloat(view, "alpha", 1.0f).setDuration(300).start();
When I want to hide the spinner, I do this:
ObjectAnimator.ofFloat(view, "alpha", 0.0f).setDuration(300).start();
animDrawable.stop();
However, this has the effect that the animation stops immediately. I would like it to stop only after the ObjectAnimator has completely faded to 0.0 alpha. Is there a way I can setup something along the lines of an "AnimationCompleted" callback?
The more modern way of doing this is to use the ViewPropertyAnimator:
view.animate()
.alpha(0f)
.withEndAction(new Runnable() {
#Override
public void run() {
// Do something.
}
})
.start();
Or, if you're using RetroLambda:
view.animate()
.alpha(0f)
.withEndAction(() -> {
// Do something.
})
.start();
To your original question about the ObjectAnimator object you can set up an Animator.AnimatorListener object which defines several animation state callbacks. You want to override public void onAnimationEnd(Animator animation)
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(VideoEditorActivity.this, "animation ended", Toast.LENGTH_LONG).show();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
With androidx you can use doOnEnd method which invoked when the animation has ended
val anim = ObjectAnimator.ofFloat(eva_image, View.TRANSLATION_Y, 0f, 500f)
anim.setDuration(500)
anim.doOnEnd { Toast.makeText(this, "Anim End", Toast.LENGTH_SHORT).show() }
anim.start()
You could also look into postOnAnimation(Runnable)
Link to Docs: postOnAnimation(java.lang.Runnable)

Categories

Resources