Android .cancel() doesn't cancel the animation - android

I'm trying to cancel an animation with a very simply way just to see whether if .cancel() function doing what it is intended to do.
But it seems it just does not work.
Here is the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.my_text_view);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animation.cancel();
}
}, 2000);
}
}
The animation is running forever. It just doesn't stop after 2 seconds.
If I call it from the view:
textView.getAnimation().cancel();
It doesn't work either.
What do you think what makes this? I need a proper way to stop an animation.

Related

Move next Activity after animation is finished in Android

I am using RotatingTextWrapper in android for text animation. It is working well. But I want to move next Activity after animation is finished automatically by giving time interval. I have used Thread for this purpose and placed animation inside of thread function. Unfortunately Animation is not working but after few second second Activity is opened automatically. Please Help me.
Here is Source Code
public class MainActivity extends AppCompatActivity {
TextView tv;
Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.txt);
final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
final Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Reckoner_Bold.ttf");
tv.setTypeface(typeface2);
timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
RotatingTextWrapper rotatingTextWrapper = (RotatingTextWrapper) findViewById(R.id.custom_switcher);
rotatingTextWrapper.setSize(30);
rotatingTextWrapper.setTypeface(typeface2);
Rotatable rotatable = new Rotatable(Color.parseColor("#af030f"), 3000, "Xafa", "Hazil", "Uzr","Sizni","Juda");
rotatable.setSize(30);
rotatable.setTypeface(typeface);
rotatable.setInterpolator(new AccelerateInterpolator());
rotatable.setAnimationDuration(600);
Rotatable rotatable2 = new Rotatable(Color.parseColor("#123456"), 3000, "qimoqchimasdim", "Ediku", "So`rayman","Yoqtiraman","ham");
rotatable2.setSize(30);
rotatable2.setTypeface(typeface);
rotatable2.setInterpolator(new DecelerateInterpolator());
rotatable2.setAnimationDuration(600);
rotatingTextWrapper.setContent("? ?", rotatable, rotatable2);
Intent i=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(i);
finish();
}
},4000);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//logic
}
}, 4000);
}
Put the animation in the onResume() stage of the activity lifecycle. According to Android documentation for Activity lifecycle onStart() "...makes activity visible to the user..." and onResume() "...app interacts with the user...".
Since you want your animation to be visible to the user, I would place it in onStart() and use the logic of the design to make onResume() handle your animation.
public class MainActivity extends AppCompatActivity{
#OnCreate()
// instantiations class member variables
#OnStart()
// timer code for animation
#OnResume()
Intent i=new Intent(getApplicationContext(),Main2Activity.class);
this.startActivity(i);
}

TextView keeps popping up

I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}
If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);
You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);
You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.

how to check if an application is killed during an animation in android

i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer

Hide and make visible button every 5 second interval

Can it possible to make a button on screen which automatically visible and gone every 5sec interval?
By using this
b.setVisibility(View.VISIBLE);
we can visible and
b.setVisibility(View.GONE);
we can hide it.But I can't manage to make it by usin the time interval.
Any idea?please share.
There are a few different ways, one is a Handler and Runnable:
public class Example extends Activity {
private Handler mHandler = new Handler();
private Runnable alternate = new Runnable() {
public void run() {
// Alternate visible and not
mHandler.postDelayed(alternate, 5000);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler.postDelayed(alternate, 5000);
}
}
Use this
new CountDownTimer(9000000, 5000) {
public void onTick(long millisUntilFinished) {
if(b.getVisibility() == View.GONE)
b.setVisibility(View.VISIBLE);
else
b.setVisibility(View.GONE);
}
public void onFinish() {
//Restart timer if you want.
}
}.start();

Android, Can't get MediaPlayer class to work

I would like to add sound to an animation that I have created. Everytime the animation starts, supposedly a sound has to start as well, but I can't manage to start the sound.
All is ok with the animation, here's the code piece:
public class TestActivity extends Activity {
AnimationDrawable anim;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playAnimation(R.id.frameLayout1,R.drawable.anim2,R.raw.bang);
}
public void playAnimation(int FrameLayoutAddress, int animationXMLAdress, int soundAddress)
{
mp = MediaPlayer.create(this.getApplicationContext(), soundAddress);
mp.start(); // error here
FrameLayout imgView = (FrameLayout)findViewById(FrameLayoutAddress);
imgView.setBackgroundResource(animationXMLAdress);
anim = (AnimationDrawable) imgView.getBackground();
imgView.post(new Runnable()
{
#Override
public void run()
{
anim.start();
}
});
}
}
Can anyone point out my mistake ? Thanks in advance for your time.
You should call mp.prepare() before mp.start(). Also it's suggested to reset the MediaPlayer before calling mp.prepare().

Categories

Resources