I've write an Animation for make text of a TextView blink, and i would perform an action after animation ends.
It's possible to intercept end of animation in some way?
AnimationListener listener = new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// Do your stuff
}
};
Related
I am using this code to animate childViews in a ViewGroup on screen:
zoom_in.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
viewGroup.getChildAt(i).setVisibility(View.VISIBLE);
viewGroup.getChildAt(i).clearAnimation();
if(i<5)
{
i++;
viewGroup.getChildAt(i).startAnimation(animation);
}
else
{
i=0;
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
zoom_out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
viewGroup.getChildAt(i).setVisibility(View.INVISIBLE);
viewGroup.getChildAt(i).clearAnimation();
if(i<5)
{
i++;
viewGroup.getChildAt(i).startAnimation(animation);
}
else
{
viewGroup.getChildAt(i).startAnimation(zoom_in);
i=0;
}
}
});
The animation zoom_in and zoom_out are simple zoom in and zoom out animations. When I run this code on Android 4.2 or higher it works perfectly. But on lower then it doesnot work similarly. Can anyone explain me or provide any solution for this problem. I want it to work similar on all devices 4.0 and higher.
I need some information why my animation are making devices to lag. The current problem is seen in Samsung Galaxy Tab Pro 8.4'', but in all devices the are slight lags, kaing writing on keyboard heavy. Thank you for your cooperation !!! :)
PS: Thanks Mikael :). Here is some code. Its complicated animation and it contains several parts very similar to this one with combine animation of several views.
CODE :
mTriangleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mLearnMore = (LinearLayout) ((LoginActivity) context)
.findViewById(R.id.learn_more);
if (mAccNameEmail.isShown()) {
// Slider
if (BookshelfApp.isTablet(context)) {
if(getActivity().getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
mDeseretLogo.setVisibility(View.VISIBLE);
mSquigle.setVisibility(View.VISIBLE);
mBorder.setVisibility(View.VISIBLE);
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
}
view.animate().translationX(0);
view.animate().setDuration(500);
mLearnMore.animate().translationX(0);
mLearnMore.animate().setDuration(500);
mLearnMore.animate().setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
mLearnMore.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
//XXX
} else if (!BookshelfApp.isTablet(context)) {
mCreateAccFragment.animate().translationY(0);
mDeseretLogo.setVisibility(View.VISIBLE);
mSquigle.setVisibility(View.VISIBLE);
mBorder.setVisibility(View.VISIBLE);
}
mTextHint1.setVisibility(View.VISIBLE);
mTextHint2.setVisibility(View.VISIBLE);
mTriangleButton.animate().rotation(0);
mTriangleButton.animate().setDuration(500);
mAccPasswords.setVisibility(View.GONE);
mAccNameEmail.setVisibility(View.GONE);
mSignUpButton.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.sign_up_shape));
mSignUpButton.setTextColor(getResources().getColor(
R.color.orange));
mLoginFormView.setVisibility(View.VISIBLE);
mSignInButton.setVisibility(View.VISIBLE);
mForgotPasswordView.setVisibility(View.VISIBLE);
mCreateAccountQuestion
.setText(R.string.acc_question_welcome);
mCreateAccountWord.setText(R.string.acc_welcome);
mHaveAccountButton.setVisibility(View.GONE);
} else {
// Slider
if (BookshelfApp.isTablet(context)) {
if(getActivity().getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
mDeseretLogo.setVisibility(View.GONE);
mSquigle.setVisibility(View.GONE);
mBorder.setVisibility(View.GONE);
imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, 0);
}
view.animate().translationX(-view.getWidth() / 4);
view.animate().setDuration(500);
mLearnMore.animate().translationX(view.getWidth() / 2);
mLearnMore.animate().setDuration(500);
mLearnMore.animate().setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
mLearnMore.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}
//XXX
else if (!BookshelfApp.isTablet(context)) {
mDeseretLogo.setVisibility(View.GONE);
mSquigle.setVisibility(View.GONE);
mBorder.setVisibility(View.GONE);
}
mTextHint1.setVisibility(View.GONE);
mTextHint2.setVisibility(View.GONE);
mLoginFormView.setVisibility(View.GONE);
mSignInButton.setVisibility(View.GONE);
mForgotPasswordView.setVisibility(View.GONE);
mTriangleButton.animate().rotation(180);
mTriangleButton.animate().setDuration(300);
mAccPasswords.setVisibility(View.VISIBLE);
mAccNameEmail.setVisibility(View.VISIBLE);
mSignUpButton.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.sign_in_shape));
mSignUpButton.setTextColor(getResources().getColor(
R.color.white));
mCreateAccountQuestion.setText(R.string.acc_question);
mCreateAccountWord.setText(R.string.acc_done);
mHaveAccountButton.setVisibility(View.VISIBLE);
}
}
});
I managed to move red LinearLayout over another LinearLayout usign TranslateAnimation.
I indeed control start of animation, but not like in YouTube app or other similar apps as I have to wait until the animation finishes.
How can I gain more control over the movement of red LinearLayout?
You have to set an animation listener over your animation, like this
animationRed.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
Here is the situation. My Activity is running, and I call a function to start an animation. Once this animation finishes, I need to wait for another 2 seconds, then continue the process normally. What do you suggest I do?
The OnAnimationEnd does not help my case by the time I reach the end of iteration, my process has already returned from the calling function and continued processing.
What do you think I do? Do thread.sleep (duration of animation + 2 sec)?
Thanks
EDIT:
OK I thought maybe I should explain what I am doing in code
I have the following code
Public class myclassActivity extends Activity{
..
.
.
.
public void runGame(){
player1.startAnimating();
player2.start Animating();
player3.startAnimating();
updateStatus();
runGame();
}
}
So as you can see my activity keeps calling startAnimating for each player. In which some work and then animation for 1 second is done.
I don't want player2.startAnimation to start untill the player1.startAnimation is completed and so on for the rest of the code.
Currently what happens is that the code executes fully and rerun again while the animation is still running. Pleasee any help on this?
observe this code it is help full you.
public class PlayersActivity extends Activity {
ImageView I1,I2;
Button B;
Animation T1,T2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
I1=(ImageView)findViewById(R.id.i1);
I2=(ImageView)findViewById(R.id.i2);
B=(Button)findViewById(R.id.b1);
B.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
T1=new TranslateAnimation(0,100,0,0);
T1.setDuration(1000);
I1.startAnimation(T1);
T1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T2=new TranslateAnimation(0,100,0,0);
T2.setDuration(1000);
T2.setStartOffset(2000);
I2.startAnimation(T2);
T2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
T1.setStartOffset(2000);
I1.startAnimation(T1);
}
});
}
});
}
});
}
}
Suppose you will start this two methods after completed player3, you are placing player3 onAnimationEnd(){ updateStatus(); runGame();}
Use postDelayed function to run something after exact delay. For example (2 sec delay):
LinearLayout.postDelayed(new Runnable() {
public void run() {
//Do something
}
}, 2000);
how about passing caller instance by
player1.startAnimating(this)
and then call method like
myclassActivity#animationFinished(this)
on finishing animation of player1/2? then
void animationFinished(Player p) {
if (p==player1) {
player2.start();
} else if (p==player2) {
player3.start();
}
}
or something
I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?
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) {
}
});
Functionally the same as the accepted answer but in a much more concise way:
// Add/Remove any animation parameter
theView.animate()
.alpha(0)
.setDuration(2000)
.withEndAction(new Runnable() {
#Override
public void run() {
theView.setVisibility(View.GONE);
}
});
Enjoy :)
Simply take your animation object and add animation listener to it.
Here is the example code :
rotateAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
}
});
You can also achieve this using Animation.setFillAfter
Example for Kotlin
var fadeOutImage = findViewById<ImageView>(R.id.fade_out_Image)
val fadeOutAnimation = R.anim.fade_out_animation
val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
fadeOutImage.startAnimation(animation)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(p0: Animation?) {
// not implemented
}
override fun onAnimationRepeat(p0: Animation?) {
// not implemented
}
override fun onAnimationEnd(p0: Animation?) {
fadeOutImage.visibility = View.INVISIBLE
}
})