showDialog after animation android - android

I created an animation that simulates an explosion: a "booom" image with this animation:
explosion.xml HyperspaceExplosion on Activity
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator="#android:anim/bounce_interpolator"
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.5"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:fillBefore="false"
android:duration="3000"
/>
</set>
when a player clicks on the bomb explosion begins.
At the end of explosion I want open a Dialog.
the simple code for bomb behaviour:
getBombImage().setOnClickListener(
new View.OnClickListener() {
MediaPlayer mp = null;
#Override
public void onClick(View v) {
getExplosionImage().setVisibility(View.VISIBLE);
if(!isSoundOff()){
mp = MediaPlayer.create(getApplicationContext(), R.raw.explosion);
mp.start();
}
getExplosionImage().startAnimation(getHyperspaceExplosion());
getExplosionImage().setVisibility(View.INVISIBLE);
showDialog(1);
}
}
);
The problem is that the explosion and the dialog are in conflict in terms of time and the explosion continues after the dialog is open.
I want sincronize two events: before the explosion. At the end of explosion, I want open the dialog.
Anybody ca help me?
Thanks in advice.

Use an AnimationListener and open your dialog inside onAnimationEnd().
For example like this:
Animation a = getHyperspaceExplosion();
a.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
showDialog(1);
}
// ..other listener methods here..
});
getExplosionImage().startAnimation(a);

Related

Android infinite animation

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatMode="restart"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
>
</rotate>
final Animation animation = AnimationUtils.loadAnimation(context , R.anim.rotation);
animation.setRepeatCount(Animation.INFINITE);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!animation.isInitialized()){
imageButton.startAnimation(animation);
}else{
imageButton.clearAnimation();
animation.reset();
animation.cancel();
}
}
});
Hi, I have this code with an onclick set up that would start rotating the button on click and stop if the button is clicked again.. however, only first click that starts the animation is working here. Animation just resets on clicking the button when animation is running.
Tried it in combination with animation.reset().
You create a new Animation object every time you click on the button. Try initializing it outside of the onClick function and just handle the checking if it is currently running inside the onClick.

Animation will NOT stop running

i have implemented an animation on one of my image views. My problem is that the animation will NOT stop at all. I call everything clearanimation i set it to null set it to cancel and it still wont stop.
public void tiltani(){
ImageView vault = (ImageView)findViewById(R.id.vault2) ;
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.startAnimation(tilt);
}
public void stopani() {
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="6"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="infinite"/>
<rotate
android:fromDegrees="6"
android:toDegrees="-2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
here is where i start it
Intent intent1 = getIntent();
if (intent1.hasExtra("id1")) {
tiltani();
and i try to stop/cancel everything in an onclick method
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.chestopen));
stopani();
update
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
if (intent1.hasExtra("id1")) {
vault.startAnimation(tilt);
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,
R.drawable.chestopen));
vault.setAnimation(null);
You can just use vault.startAnimation(tilt); to start your animation, and use vault.setAnimation(null); to stop your animation.
You have initialized Animation a second time in stopani() method. Your view initialization and animation initialization should be global.
Your code should be like this.
ImageView vault = (ImageView)findViewById(R.id.vault2); //Global variable
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt); //Global variable
public void tiltani(){
vault.startAnimation(tilt);
}
public void stopani() {
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
Hope, It will help you.. :)

Detect a click on an animated button

i'm making a program on android : the user has to push on an animated button to make the button invisible and increase score. But, while testing, the animated button is disabled during animation : the animation is perfectly done but we can't use animated button during animation. I've searched answers but i haven't found.
MainActivity :
Public int score = 0;
Public Button button1 = (Button) findViewById(R.id.button1);
Public Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.translate_up);
button1.startAnimation(anim1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score++;
button1.setVisibility(View.INVISIBLE);
}});
Translate_up, XML animation :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:shareInterpolator="false">
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
android:startOffset="0"
android:toXDelta="0"
android:toYDelta="-900"
/>
Sorry for my bad English, i hope you understand me.
Thanks !
The button itself is actually working on it's original position. So although it looks like your button is sliding to the top, you can still click on the empty space in it's original position and trigger the onClick event.
I am afraid an animation won't work here. If it is a game then you should create a gameloop and update the position of your views (e.g. your button) in the gameloop method.

Animation is not working in Emulater

i am enable to load animation in Emulater..Its working fine with any real device..
public class MainActivity extends Activity {
private ImageView imgView;
private Animation animation;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.grow);
animation.setRepeatCount(50); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE);
imgView = (ImageView) findViewById(R.id.imgView);
imgView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent it = new Intent(getApplicationContext(), MyWebView.class);
startActivity(it);
}
}, 5000);
}
});
}
and my anim xml file is following
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50" />
I think you Might have Disabled Animations in Emulator:
Check that:
Settings>Display>Animation..
Hope that helps your Problem
For anyone running into similar issues when using ObjectAnimator, in my case I had to enable the developer settings on the emulator and then go into "Settings" > "Developer options" > "Drawing" section
In this "Drawing" section you'll find different options for each animation type, in my case the "Animator duration scale" was off, after setting it to "1x" I started seeing the animations on the emulator.

Spinning wheel in Android

How can I spin a image wheel in an activity on android with the help of touch event? I need some guideline or link of any tutorial.
This is typically done with a couple pieces. This is how I do it in one of my apps. *Note: This is not a smooth wheel, so much as it starts and stops at the top (which was intentional). You can lookup more about Animation on the dev site.
Main XML that has an image:
<ImageView
android:id="#+id/anim_example"
android:src="#drawable/loading_circle"
android:layout_width="30sp"
android:layout_height="30sp"
android:onClick="RunAnimation" />
Here are the parts in code that run the animation
public void RunAnimation(View v)
{
//The onClick method has to be present and must take the above parameter.
StartLoading();
//This will delay the stop for 5 seconds
//Normally you would want to actually have this run based on some other input/data.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
StopLoading();
}
}, 5000);
}
public void StartLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
Animation rotateLoading = AnimationUtils.loadAnimation(this, R.anim.rotate);
refreshImage.clearAnimation();
refreshImage.setAnimation(rotateLoading);
}
public void StopLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
if (refreshImage.getAnimation() != null)
{
refreshImage.clearAnimation();
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
}
}
anim.rotate:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="359"
android:duration="2000"
android:repeatMode="restart"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%">
</rotate>

Categories

Resources