How to start activity on animation end - android

This is my first app and i need to start new activity when the animation ends. what do I need to do? My code:
package com.lineage.goddess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class LineageSplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
startAnimation();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void startAnimation() {
// TODO Auto-generated method stub
TextView logo1= (TextView) findViewById(R.id.TextView1);
Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2= (TextView) findViewById(R.id.TextView2);
Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo2.startAnimation(fade2);
TextView logo3= (TextView) findViewById(R.id.TextView3);
Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo3.startAnimation(fade3);
TextView logo4= (TextView) findViewById(R.id.TextView4);
Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2);
logo4.startAnimation(fade4);}
public void onAnimationEnd() {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
;
}
}

Set an AnimationListener to the animation you want to use to start your Activity.
myAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
}
}
So, your code will be like this:
package com.lineage.goddess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class LineageSplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
startAnimation();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
private void startAnimation() {
// TODO Auto-generated method stub
TextView logo1= (TextView) findViewById(R.id.TextView1);
Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2= (TextView) findViewById(R.id.TextView2);
Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo2.startAnimation(fade2);
TextView logo3= (TextView) findViewById(R.id.TextView3);
Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo3.startAnimation(fade3);
TextView logo4= (TextView) findViewById(R.id.TextView4);
Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2);
face4.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
Intent i = new Intent( LineageSplashActivity.this, LineageMenuActivity.class );
LineageSplashActivity.this.startActivity( i );
}
}
logo4.startAnimation(fade4);
}
}

Your code made my eye's bleed, so I fixed it as much as I could:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
public class LineageSplashActivity extends Activity implements AnimationListener {
private static final int NUMBER_OF_ANIMATIONS = 4;
private int animationFinishedCount = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
startAnimations();
}
private void startAnimations() {
Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fade.setAnimationListener(this);
findViewById(R.id.TextView1).startAnimation(fade);
findViewById(R.id.TextView2).startAnimation(fade);
findViewById(R.id.TextView3).startAnimation(fade);
findViewById(R.id.TextView4).startAnimation(fade);
}
#Override
public void onAnimationEnd(Animation animation) {
// When all animations have finished - start the next activity
if(++animationFinishedCount == NUMBER_OF_ANIMATIONS){
Intent intent = new Intent( this, LineageMenuActivity.class );
startActivity( intent );
}
}
#Override
public void onAnimationStart(Animation animation) {
// Nothing
}
#Override
public void onAnimationRepeat(Animation animation) {
// Nothing
}
}
And if it's not a mis-type and you actually need a different animation for the 4th textview you can remove the count check and just add the animation listener to that individual animation.

Related

Splash screen animation

I made a splash activity before my app's main activity. It has a movieview and an animated imageview. I have two problems with the imageview:
Before the fade-in animation starts the image is visible.
During the animation a see the frames. ( And it is not because of the emulator, I also tested it on a Sony Xperia SP.)
My java:
package com.koostamas.tbbt;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.VideoView;
public class SplashActivity extends Activity {
private ImageView circle;
private Animation anim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
VideoView loading = (VideoView)findViewById(R.id.videoView_loading);
circle = (ImageView) findViewById(R.id.imageView_circle);
loading.setVideoPath("android.resource://com.koostamas.tbbt/raw/splash_loading");
loading.setZOrderOnTop(true);
loading.start();
anim = AnimationUtils.loadAnimation(this, R.anim.fade_in);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
circle.startAnimation(anim);
}
Thread thread = new Thread(){
#Override
public void run() {
// TODO Auto-generated method stub
try {
sleep(5000);
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
{
thread.start();
}
}
Please help me solve these problems. Thanks in advance.
Instead of using the onWindowFocusChanged register on the videoView's setOnCompletionListener. there you can start your animation.
To call the next activity use :
new Handler().postDelayed(new Runnable() {
public void run()
{ startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish(); }
} , animationTime);

How to call onclicklistner of animated image android

i am working on android app
in which i have an animated image.
my code is
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth()/2;
left = new TranslateAnimation(0, hight, width, hight);
left1= new TranslateAnimation( 480, 10, 0, 10);
left.setDuration(2000);
left.setAnimationListener(this);
b1 =(ImageView)findViewById( R.id.balloon);
b1.setOnClickListener(this);
b1.startAnimation(left);
#Override
public void onClick(View v) {
Toast.makeText(this, "Clicked", 27).show();
}
using this code i am able to animate ballon or my picture but i the onclick lisnter only works when animation is completed i want onclicklistner should work during animation how to do this.
sorry for bad english
Override onAnimationStart and onAnimationEnd functions in your AnimationListener. Keep a variable to check whether animation is playing when the image clicked.
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView imageView;
private boolean animationPlaying;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TranslateAnimation animation = new TranslateAnimation( 480, 10, 0, 10);
animation.setDuration(2000);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
animationPlaying = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
animationPlaying = false;
}
});
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.startAnimation(animation);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(animationPlaying) {
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
} else {
Log.d("ANIMATION", "click missed because animation was not playing");
}
}
});
}
}

XML Animation executing synced

I've made some simple animations which I called breath because the object slowly get bigger and smaller in a loop.
Then I associated this animation to 3 imagebuttons and everything works ok, but when I interact with a button, which do another "vibrate" animation, all of them restart the animation together, while the expected behaviour was that only the pressed button would have restarted the animation.
I would like to understand that a bit better, and maybe how to achieve that every button's animation have its own lifecycle.
That's the code:
breath.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<scale
android:duration="1500"
android:startOffset="0"
android:fromXScale="0.9"
android:fromYScale="0.9"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
and that's the java code:
package com.anesoft.android.citmania;
import com.anesoft.android.citmania.models.Defines;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MenuActivity extends Activity implements OnClickListener, AnimationListener {
ImageButton opzioni;
ImageButton play;
ImageButton espansioni;
ImageView logo;
TextView title,title2;
Animation breath,rotate_r,rotate_l,vibrate;
int flag = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
logo = (ImageView)findViewById(R.id.logo);
title = (TextView)findViewById(R.id.title);
title2 = (TextView)findViewById(R.id.title2);
opzioni = (ImageButton)findViewById(R.id.options);
play = (ImageButton)findViewById(R.id.play);
espansioni = (ImageButton)findViewById(R.id.expansions);
breath = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.breath);
rotate_r = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_right);
rotate_l = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_left);
vibrate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.vibrate);
vibrate.setAnimationListener(this);
Typeface tf = Typeface.createFromAsset(getAssets(), "font/Canter_Light.otf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "font/Canter Bold.otf");
title.setTypeface(tf);
title2.setTypeface(tf2);
title.setText("CIT");
title.setTextSize(Defines.TITLE_SIZE);
title2.setText(".MANIA");
title2.setTextSize(Defines.TITLE_SIZE);
opzioni.startAnimation(breath);
play.startAnimation(breath);
espansioni.startAnimation(breath);
logo.setOnClickListener(this);
opzioni.setOnClickListener(this);
play.setOnClickListener(this);
espansioni.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.options) {
Intent i = new Intent(this, OptionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if (id == R.id.play) {
Intent i = new Intent(this, LevelPickerActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if(id == R.id.expansions){
espansioni.startAnimation(vibrate);
Toast.makeText(this, "Implementando", Toast.LENGTH_SHORT).show();
//espansioni.startAnimation(bounce);
}else if(id == R.id.logo){
if(flag==0){
logo.startAnimation(rotate_r);
flag=1;
}else{
logo.startAnimation(rotate_l);
flag=0;
}
}
}
#Override
public void onAnimationEnd(Animation arg0) {
if (arg0 == vibrate) {
espansioni.startAnimation(breath);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
You can see how I start the vibrate animation and then when IT finishes I restart the breath one only in the button being tapped, but all of them restart the animation in a synced way.
Thanks in advance
If you want the animation to work independently on all 3 buttons, you'll need to create 3 instances of the animation.
Once the animation is halted, it gives its views a new state. If you gave 3 views the same animation, they will all receive the new state.

Android - RotateAnimation onClick button restart itself

this is the java code to rotate an image when click on button. Image rotate it's perfect but if I click button again when rotation is not ended the animation restart itself, not end the animation. How can I wait the end of the animation? I found Animation.AnimationListener, I think that onAnimationEnd works great for me, but I'm not be able to integrate it in my code... please help me :-)
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Rotation_test extends Activity {
private float statdegree = (float) 0.0;
private float enddegree = (float) 90.0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotation_test);
// ---------------------------------------------------------------------------------------------
Button buttonRotateCenter = (Button) findViewById(R.id.rotatecenter);
final ImageView floatingImage = (ImageView) findViewById(R.id.floatingimage);
// AnimationRotation
final Animation animationRotateCenter = new RotateAnimation(statdegree,
enddegree, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animationRotateCenter.setDuration(5000L);
animationRotateCenter
.setInterpolator(new AccelerateDecelerateInterpolator());
// \AnimationRotation
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
floatingImage.startAnimation(animationRotateCenter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rotation_test, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have to take a boolean variable which tracks whether animation is in progress or not. then use this variable in animation listener and button click as below code
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!anim_in_progress)
floatingImage.startAnimation(animationRotateCenter);
}
});
animationRotateCenter.setAnimationListener(new anim_listener());
}
boolean anim_in_progress=false;
class anim_listener implements AnimationListener
{
#Override
public void onAnimationEnd(Animation arg0) {
anim_in_progress=false;
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
anim_in_progress=true;
}
}
Never tried this, but might work
buttonRotateCenter.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
if(!floatingImage.hasTransientState())
floatingImage.startAnimation(animationRotateCenter);
}
});

error in activity class regarding AnimationListener

I am following the Sams Teach Yourself Android Applications workbook and have filled in the following for QuizSplashActivity class, as instructed in the book. However, I get an error in the code that I have put in bold **, where it says AnimationListener():
package com.androidbook.triviaquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class QuizSplashActivity extends QuizActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TextView logo1 = (TextView) findViewById(R.id.textViewTopTitle);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
logo1.startAnimation(fade1);
TextView logo2 = (TextView) findViewById(R.id.textViewBottomTitle);
Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
logo2.startAnimation(fade3);
Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
LayoutAnimationController controller = new LayoutAnimationController(spinin);
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.setLayoutAnimation(controller);
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
fade2.setAnimationListener(new **AnimationListener()** {
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
});
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// stop the animation
TextView logo1 = (TextView) findViewById(R.id.textViewTopTitle);
logo1.clearAnimation();
TextView logo2 = (TextView) findViewById(R.id.textViewBottomTitle);
logo2.clearAnimation();
// ... stop other animations
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
for (int i = 0; i < table.getChildCount(); i++) {
TableRow row = (TableRow) table.getChildAt(i);
row.clearAnimation();
}
}
}
The error is:
"The type new Animation.AnimationListener(){} must implement the inherited abstract method Animation.AnimationListener.onAnimationStart(Animation)".
The book does not mention anything about this and just wondered if someone can help.
Thanks in advance.
Moses
AnimationListener is an interface. That means that when you put it in your code, you have to implement all the methods.
As seen in the API, there are three methods:
onAnimationStart(Animation a)
onAnimationEnd(Animation a)
onAnimationRepeat(Animation a)
You have only implemented one: onAnimationEnd.
You have to implement the others, even if you put nothing in them.
Here would be your revised code:
fade2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
QuizSplashActivity.this.finish();
}
public void onAnimationStart(Animation a) { }
public void onAnimationRepeat(Animation a) { }
});
Note the new methods in the class.
I hope this helped!

Categories

Resources