In my app, I have an animation. I want to wait until the animation finish to continue the rest of the codes. I am trying to use .hasEnded as follows but doesn't work. I know there are some other technics to achieve this but I want to know what's wrong with this code. It is going into the endless loop at while loop
myplayer= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.myanim1);
textView_title.setAnimation(myplayer);
while (!myplayer.hasEnded()){
}
You can use a new Thread to test the hasEnded:
Animation myplayer= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.myanim1);
.....
//start the animation
Thread thread = new Thread(){
#Override
public void run() {
super.run();
boolean flag = true;
while (flag){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "run: " + myplayer.hasEnded());
if(myplayer.hasEnded()){
flag = false;
}
}
}
};
thread.start();
could use the following code to listen of Animation:
Animation myplayer= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.myanim1);
myplayer.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
textView_title.setAnimation(myplayer);
Related
I made an animation (ObjectAnimator)for a text view it work correctly but i have a question.
I wanna know is there any method that i can use immediately after animation duration finished ?
(I mean a method like onFinish when we use CountDownTimer)
I want the rest of methods and codes I have in app run when animation finished.Is there a solution for that?
ObjectAnimator deltaXAnimation = ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
ObjectAnimator deltaYAnimation = ObjectAnimator.ofFloat(winMassage,"scaleY",1f,1.5f);
deltaYAnimation.setDuration(300);
deltaYAnimation.start();
HI there are proper callback methods in which you get Animation Start and end Listeners
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
deltaXAnimation.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) {
}
});
Below code might be understand well, creating a listener for ObjectAnimator
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation a) {
//
}
public void onAnimationRepeat(Animation a) {
}
public void onAnimationEnd(Animation a) {
}
});
We can go also to your ObjectAnimator
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//stop animation after 5 seconds
deltaXAnimation.cancel();
}
}, 5000); //5000 equals to 5seconds
}
Hope this might help you
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);
}
I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.
public Boolean functionFinished = false;
public void runFunction(){
firstFunction();
if(functionFinished = true){
secondFunction();
}
}
public void firstFunction(){
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
initialCount.startAnimation(out);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if(initialCount.getText().equals("3")){
initialCount.setText("2");
initialCount.startAnimation(out);
} else if(initialCount.getText().equals("2")){
initialCount.setText("1");
initialCount.startAnimation(out);
} else if (initialCount.getText().equals("1")){
initialCount.setText("START!");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
functionFinished = true;
}
The second function simply changes its own textView every second counting up from 0.
What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")
public void runFunction() {
firstFunction();
}
public void firstFunction() {
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
...
secondFunction();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
initialCount.startAnimation(out);
}
Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.
If your minimum sdk version is 14:-
textView1.animate()
.alpha(0f)
.setDuration(400)
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
// animate second textview here....
}
});
Because you assign functionFinished = true at the end of the firstFunction().
And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .
initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.
I have a linearlayout that I want it to be animated after 3 seconds delay using Handler.
After 3 seconds have passed, it doesn't even execute the animation, nor did it enter the AnimationListener's methods.
Here is how I do it:
loginBox.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Animation animTranslate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate);
animTranslate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
loginBox.setVisibility(View.VISIBLE);
Animation animFade = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
loginBox.startAnimation(animFade);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
btnContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
});
}
}, 3000);
The run() method works fine when I click btnContinue.
How can I make it work?
you forgot to call
loginBox.startAnimation(animTranslate)
and probably you want loginBox.setVisibility(View.VISIBLE); before starting the TranslateAnimation
I want to create an android project. Which there is a layout that I want it to move smoothly on another layout. I have tried to use thread in order to do this. But in the result of my code, the layout jumps to its final position and do not move smoothly from the offset to its destination.
abs1 = (AbsoluteLayout) findViewById(R.id.absoluteLayout2);
ImageButton ib1 = (ImageButton) findViewById(R.id.imageButton1);
ib1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Runnable r = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
display(100);
display(100);
display(100);
display(100);
display(100);
display(100);
display(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void display(int i) throws InterruptedException {
AbsoluteLayout.LayoutParams param = (AbsoluteLayout.LayoutParams) abs1
.getLayoutParams();
param.x = param.x + 10;
// param.y=param.x+20;
abs1.setLayoutParams(param);
if (Thread.interrupted()) {
throw (new InterruptedException());
}
Thread.sleep(i);
}
};
r.run();
main file:
<AbsoluteLayout
android:id="#+id/absoluteLayout2"
android:layout_width="107dp"
android:layout_height="308dp"
android:layout_x="10dp"
android:layout_y="12dp"
android:background="#color/red" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:background="#null"
android:src="#drawable/calendar" />
EDIT: this is available starting from API 11
Just use an ObjectAnimator for that:
ObjectAnimator moveToSide = ObjectAnimator.ofFloat(myView, "translationX", 0f, 400f);
moveToSide.setDuration(700);
moveToSide.start();
Just try it out with different float values. Now, it starts from it's original position to 400f along the X-axis.
Afterwards it will stay in this position. You could also add an AnimatorListener if you want to do some stuff on the start or at the end.
moveToSide.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator arg0) {
}
});
Your Runnable is actually running on Android's UI thread.
So sleep() will block UI's drawing process, show the view on final position.
Use a Thread to wrap it.
(new Thread(new Runnable() {
#Override
public void run() {
// your code
}
}
})).start();
Then, remember that updating views from non-UI thread is forbidden.
You should use runOnUiThread to update views.
runOnUiThread(new Runnable() {
#Override
public void run() {
display(100);
}
}
Maybe, use Android's Animation class is a better way to do this.