is there a TranslateAnimation onComplete? - android

I am animating a view to move off the screen, on complete i would like to call another function but I cant seem to find a onComplete method.
int originalPos[] = new int[2];
icons.getLocationOnScreen( originalPos );
layoutMoved = originalPos[0]+icons.getWidth();
TranslateAnimation anim = new TranslateAnimation( 0, -layoutMoved , 0, 0);
anim.setDuration(500);
anim.setFillAfter( true );
icons.startAnimation(anim);
icons.setVisibility(View.GONE);

Set an AnimationListener, you'll have access to OnAnimationEnd.
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// do your stuff
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Related

TextView fade out and disappear

I have a TextView in my activity, I would like to show the text, then fade out and hide it completely whenever the activity is started.
I have a wired situation, my code sometimes working (the TextView is fade out and gone), but most of times, it is not working (it could not reach the onAnimationEnd function).
Here is my code for it:
protected void onResume() {
fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ;
fadeOut.setDuration(5000);
//fadeOut.setFillBefore(true);
fadeOut.setFillAfter(true);
//fadeOut.setStartOffset(1000);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.d(TAG, "fade out start");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "fade out end");
textRotateHint.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
Log.d(TAG, "fade out repeat");
}
});
textRotateHint.setVisibility(View.VISIBLE);
textRotateHint.setText(R.string.rotation_hint);
textRotateHint.startAnimation(fadeOut);
super.onResume();
}
You can use a simple value animator.
ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0f);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float alpha = (float) animation.getAnimatedValue();
mTextView.setAlpha(alpha);
}
});
valueAnimator.start();

How to fix view position after moving it

With the code below, the TextView variable tvAppName slides up successfully, but after 1,200 milliseconds the view goes back to its position. How can I fix the view after moving it?
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
TranslateAnimation anim = new TranslateAnimation(0, 0, 100, 0);
anim.setDuration(500);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tvAppName.startAnimation(anim);
}
}, 1200);
}
From the documentation: You need use setFillAfter method.
anim.setFillAfter( true );
If fillAfter is true, the transformation that this animation performed
will persist when it is finished
Hope it helps.
You should use anim.setfillafter(true)
the transformation that this animation performed will persist when it
is finished
If you did not use code below.you can Delete That
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You should use NineOldAndroid library. This is a really good library. It will help you with a lot of animations.

view slide down animation not working

I am animating my relative layout with some images in my app. When i write the code to animate the layout on button click, for the first time it is just displaying not animating. Then on the same button click i am sliding up the layout. After that if i click the button sliding down animation works fine.Actually i have to write this sliding down animation inside onCreate itself instead of button click. Given below is my code. Can anyone tell me why it s not showing the animation?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fadeview);
iconLayout=(RelativeLayout)findViewById(R.id.icon_ayout);
iconLayout.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
iconLayout.setVisibility(View.VISIBLE);
SlideToDown();
}
}, 500);
}
public void SlideToAbove() {
Animation slide = null;
slide=new TranslateAnimation(0.0f, 0.0f, 0.0f,-iconLayout.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
iconLayout.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iconLayout.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
iconLayout.getWidth(), iconLayout.getHeight());
// lp.setMargins(0, 0, 0, 0);
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iconLayout.setLayoutParams(lp);
}
});
}
public void SlideToDown() {
Animation slide = null;
slide=new TranslateAnimation(0.0f, 0.0f, -iconLayout.getHeight(), 0.0f);
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
iconLayout.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iconLayout.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
iconLayout.getWidth(), iconLayout.getHeight());
lp.setMargins(0,0, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iconLayout.setLayoutParams(lp);
}
});
}
This is because you are starting animation in onCreate() and in this time the view is available but it is not actually drawn(doesnt have width and height). Here the value of iconLayout.getHeight() will return value 0.
Solution is to setup a Layoutlistener to the view and the listener will notity you when it is drawn. After that you can start the animation.
ViewTreeObserver loadingObserver = iconLayout.getViewTreeObserver();
loadingObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
iconLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
SlideToDown();
}
});

Swapping two images doesnt work correctly using relative layout

This is the code for swapping based on finger movement. I am able to swap but the images are not replacing the positions. After swapping the swapped image has to take the new position and animation has to set again for that. For this code it is redirecting to the old position and taking animation effect second time.
if (imageendX > imagestartX && imageendY < imagestartY) {
if (imageendX - imagestartX < 60) {
} else {
TranslateAnimation tr = new TranslateAnimation(0,
90, 0, 0);
tr.setDuration(1000);
// tr.setFillAfter(true);
// tr.setFillEnabled(true);
TranslateAnimation tr2 = new TranslateAnimation(0, -90, 0, 0);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
// tr2.setFillEnabled(true);
currentView.startAnimation(tr);
RightView.startAnimation(tr2);
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
} else if (imageendY < -10) {
TranslateAnimation tr = new TranslateAnimation(0, 0, 0, -90);
tr.setDuration(1000);
// tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0,90);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
currentView.startAnimation(tr);
TopView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) TopView
.getLayoutParams();
// pr.topMargin += 90;
pr.topMargin += 90;
TopView.setLayoutParams(pr);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
android.widget.RelativeLayout.LayoutParams pr = (android.widget.RelativeLayout.LayoutParams) currentView
.getLayoutParams();
// pr.topMargin += 90;
pr.bottomMargin += 90;
currentView.setLayoutParams(pr);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
} else if (imageendY > imagestartY && imageendX < imagestartX) {
if (imageendY - imagestartY < 60) {
} else {
TranslateAnimation tr = new TranslateAnimation(0, 0, 0, RM);
tr.setDuration(1000);
// tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, 0, 0, -RM);
tr2.setDuration(1000);
// tr2.setFillAfter(true);
currentView.startAnimation(tr);
BottomView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
} else if (imagestartX >= imageendX) {
if (imagestartX - imageendX < 90) {
} else {
System.out.println("right to left");
TranslateAnimation tr = new TranslateAnimation(0, RM, 0, 0);
tr.setDuration(1000);
tr.setFillAfter(true);
TranslateAnimation tr2 = new TranslateAnimation(0, -RM, 0, 0);
tr2.setDuration(1000);
tr2.setFillAfter(true);
LeftView.startAnimation(tr);
currentView.startAnimation(tr2);
tr2.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
tr.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
// }
}
Using View Animation the views will be in Original Position after the animation, So according to your requirement use Property Animation the views will be in Animated positons after the animation but the only downside is Property Animation supports from API 11
No Problem...Use NineOldAndroids to overcome that Problem..
Sample Code with two image views
Animation between imageOne and imageTwo
ObjectAnimator imageOneAnimator = ObjectAnimator.ofFloat(
imageOne, "X", imageOne.getX(), imageTwo.getX());
ObjectAnimator imageTwoAnimator = ObjectAnimator.ofFloat(
imageTwo, "X", imageTwo.getX(), imageOne.getX());
imageOneAnimator.setDuration(1000);
imageTwoAnimator.setDuration(1000);
AnimatorSet set = new AnimatorSet();
set.playTogether(imageOneAnimator, imageTwoAnimator);
set.start();
Animation between imageThree and imageOne
ObjectAnimator imageOneYAnimator = ObjectAnimator.ofFloat(
imageOne, "Y", imageOne.getY(), imageThree.getY());
ObjectAnimator imageThreeYAnimator = ObjectAnimator.ofFloat(
imageThree, "Y", imageThree.getY(), imageOne.getY());
imageOneYAnimator.setDuration(1000);
imageThreeYAnimator.setDuration(1000);
AnimatorSet setY = new AnimatorSet();
setY.setStartDelay(1000);
setY.playTogether(imageOneYAnimator, imageThreeYAnimator);
setY.start();
You can start the animation in your touch listener according to your calculations

Translate animation & view layout

i'd like to make a translateAnimation in my Android app with the code below :
TranslateAnimation anim = new TranslateAnimation(0,0,-400,0);
anim.setDuration(400);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mlLinearLayout.clearAnimation();
mlLinearLayout.requestLayout();
mlLinearLayout.layout(mlLinearLayout.getLeft(), mlLinearLayout.getTop()+400, mlLinearLayout.getRight(), mlLinearLayout.getBottom());
}
});
anim.setInterpolator(new AccelerateInterpolator());
anim.setFillEnabled(true);
anim.setFillAfter(true);
anim.setFillBefore(false);
mlLinearLayout.startAnimation(anim);
active=false;
but when this animation was done, the LinearLayout back to his start place even if I rebuild my view with the new position. How can I change it please ?
I finally found solution :
TranslateAnimation anim = new TranslateAnimation(0,0,0,400);
anim.setDuration(400);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mlLinearLayout.layout(mlLinearLayout.getLeft(), mlLinearLayout.getTop()+400, mlLinearLayout.getRight(), mlLinearLayout.getBottom());
}
});
anim.setFillEnabled(true);
anim.setFillAfter(false);
anim.setFillBefore(false);
mlLinearLayout.startAnimation(anim);
active=false;
As i rebuild my view after translation, fillafter isn't usefull, Now it's working perfect without lag and jump !

Categories

Resources