How to time an animation in android - android

I am animating a loader on start up. How can I work around it so that instead of using ontouch listener to stop the timer, I use a time of say like 3 seconds? Here is my code:
private void showdiag() {
loader = findViewById(R.id.loader);
logo= loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this,R.anim.rotate));
loader.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loader.setVisibility(View.GONE);
}
});
}

You can use View.postDelayed() to run some action after a set delay:
private void showdiag() {
loader = findViewById(R.id.loader);
logo = loader.findViewById(R.id.logo);
logo.setAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate));
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}

logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.setVisibility(View.GONE);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});

Please try following code.
logo.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) {
}
#Override public void onAnimationEnd(Animation animation) {
loader.postDelayed(new Runnable() {
#Override
public void run() {
loader.setVisibility(View.GONE);
}
}, 3000);
}
#Override public void onAnimationRepeat(Animation animation) {
}
});

Related

How to disable/block onClickListener for few seconds?

I made nice looking splash screen and I want to block onclicklistener for a few seconds cause i've got few animations etc and I want the user see it all.
I've got:
ostatni.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
starttap.start();
Intent i = new Intent(Start.this,ActivityMainWallet.class);
startActivity(i);
}
});
EDIT
I've got:
ostatni.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
starttap.start();
ostatni.setEnabled(false);
}
});
wlot.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Intent i = new Intent(Start.this,ActivityMainWallet.class);
startActivity(i);
ostatni.setEnabled(true);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
And it doesn't work. Please help me, thanks!
You can pause the thread it is running on, the main thread (UI Thread):
Thread.sleep(4000); // freeze the thread for 4 seconds
Source: Java, Pausing Execution
SystemClock.sleep(2000)will be better.
sleep 2ms, and this method will not block the thread.
You can also use android animation utils to do this
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_in);
<your imageviews or any other view>.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//start your main activity
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
add this ?
public onCreate(....)
{
......
......
ostatni.setOnClickListener(........);
ostatni.setEnabled(false);
}
#Override
public void onAnimationEnd(Animation animation) {
ostatni.setEnabled(true);
}

Applying sequence of animation on a view

is there a simple way to animate a view so that it does one animation after another.
i tried this:
button.animate().translationX(500).setDuration(300);
button.animate().translationX(0).setDuration(300).setStartDelay(2000);
Something like this:
button.animate().translationX(500).setDuration(300).withEndAction(new Runnable() {
#Override
public void run() {
button.animate().translationX(0).setDuration(300).start();
}
}).start();
Try setting a listener on the first animation so that when it ends, you can start the second animation:
button.animate().translationX(500).setDuration(300).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
button.animate().translationX(0).setDuration(300).start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});

FAB does not animate - test code and screenshot attached

I have prepared a simple test project at GitHub for my question:
I am trying to show/hide a FloatingActionButton (aka FAB) every 5 seconds by the following code in MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFab = (FloatingActionButton) findViewById(R.id.fab);
mInAnimation = AnimationUtils.makeInAnimation(this, false);
mInAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
mFab.setVisibility(View.VISIBLE);
}
});
mOutAnimation = AnimationUtils.makeOutAnimation(this, true);
mOutAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mFab.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
run();
}
The Runnable output ("Toggle animation") appears every 5 seconds in ADB logs, but the FAB is always visible:
#Override
public void run() {
Log.d("MyCoordinator", "Toggle animation");
mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);
mHandler.postDelayed(this, 5000);
}
Does anybody please know, what is missing here?
Also I am curious, if it's possible to define the above animations in the activity_main.xml instead of Java code.
I would rather use mFab.startAnimation(mFab.isShown() ? mOutAnimation : mInAnimation) than mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);. With setAnimation you have to define the start time of your animation (which is probably what you are missing)

Animation Listener not working on Button Click

I am trying to Hide/Show a TableLayout on button click but the animation listener is not working here is the code i am trying
Slide_down = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_down);
Slide_up = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_up);
searchArea = (TableLayout) findViewById(R.id.TableLayout1);
SearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (check_tableView == 0) {
Slide_up.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
searchArea.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
searchArea.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
check_tableView = 1;
} else {
Slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
searchArea.setVisibility(View.GONE);
}
#Override
public void onAnimationEnd(Animation animation) {
searchArea.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
check_tableView = 0;
}
}
});
//just start animation
searchArea = (TableLayout) findViewById(R.id.TableLayout1);
SearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (check_tableView == 0) {
searchArea.startAnimation(Slide_up);
}
});
You just set animation listener outside of onClickListener. No need of setting each time View is clicked. Also make sure that you are calling searchArea.startAnimation(Slide_up) and searchArea.startAnimation(Slide_down) method. Call start animation method inside onClickListener.

Delay while restart of animation

I'm using value animator to animate a image view. I want to add a delay in Animation listener, i.e in "onAnimationRepeat()". How to accomplish this?
Maybe try something like this:
public class Test {
private int repeatNumber == 0;
public void startAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {Log.i("start", "start");}
#Override
public void onAnimationEnd(Animation animation) {Log.i("end", "end");}
#Override
public void onAnimationRepeat(Animation animation) {
Test.this.repeatNumber++;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Test.this.startAnimation();
}, 30000 );
}
textView.startAnimation(animation);
}
}

Categories

Resources