Android RotateAnimation bug - android

I have a RotateAnimation attached to an ImageButton which upon click rotates it and using OnAnimationEnd starts a new Activity.
Problem is its not working. After I close my application and come back, I am inside the new Activity(..) and when I go back, then the animation executes. I want the animation to happen and then start the new Activity.
For some reason, it was working absolutely fine before using the same code but I don't know, some trivial change may have affected it.
Here's the code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu);
ImageButton amazingPicsButton = (ImageButton) findViewById(R.id.amazingPics),
setViewOnClick(amazingPicsButton, new Intent("com.jasfiddle.AmazingInterface.AMAZINGPICS"));
}
/**
* Generic OnClick setter method for giving various View objects a click listener
* #param b
* #param intent
*/
private <B> void setViewOnClick(B b, final Intent intent){
((View) b).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
amazingPicsSound = createRandButSound();
amazingPicsSound.start();
rotateAndNewActivity(v, intent);
}
});
}
/** function that produces rotation animation on the View v.
* Could be applied to button, ImageView, ImageButton, etc.
*/
private void rotateAndNewActivity(View v, final Intent intent){
// Create an animation instance
Animation an = new RotateAnimation(30, 360, v.getWidth()/2, v.getHeight()/2);
an.setDuration(50); // duration in ms
an.setRepeatCount(3); // -1 = infinite repeate
/*we override the Animation an object to include the start of an new Activity
at the end of animation */
an.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
//start the activity onAnimationEnd
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
startActivity(intent);
}
});
// Set the animation's parameters
v.setAnimation(an);
}

setAnimation only sets the next animation to play on the View. To start an animation immediately, use startAnimation
In your case, use v.startAnimation(an);

Related

how to clear aftereffects of animation

I have 2 views that are being animated by sliding in and out of the screen simultaniously
this is the code for it: How to animate a slide in notification view that pushes the content view down
However, when the animation slides in, i must clear the animation, in order for the click events to work properly, otherwise they are also translated (which is not what i want)
What do i need to put in onAnimationEnd in the listener in order to clear the animation and have the view remain the way it looks after it has been animated
this is my current code:
greenViewSlideIn(1500, 0, new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
greenView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(blueView.getWidth(), blueView.getHeight());
layoutParams.setMargins(blueView.getLeft(), blueView.getTop() - greenViewHeight, blueView.getRight(), blueView.getBottom());
blueView.setLayoutParams(layoutParams);
greenView.clearAnimation();
// blueView.clearAnimation();
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
Two things you can do:
Make sure the setFillAfter flag on the animation is set to false. Then there's no need to an AnimationListener
Once the animation ends, call view.setAnimation(null);
Hope this helps :)

Allow an animation to finish before starting a new activity

I would like to perform an animation on an image before switching to a new activity. I also want to make sure that the animation is fully completed before the new activity starts. Below is the code that I am using to accomplish the task.
public boolean onTouch(View view, MotionEvent event) {
Animation shake = AnimationUtils.loadAnimation(ViewReadingActivity.this, R.anim.shake);
imgAries.startAnimation(shake);
Intent intent = new Intent(ViewReadingActivity.this, ShowHoroActivity.class);
startActivity(intent);
return true;
}
The problem with this is that the animation does not run to completion before the new activity is presented and the animation will continue to run from where it was left. Is there a better way to accomplish this?
use like this here you can have animation listner
shake.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
Intent intent = new Intent(ViewReadingActivity.this, ShowHoroActivity.class);
startActivity(intent);
}
});
do this:
1. animation.setAnimationListener(), implement the methods
2. onanimationend method , write in the code to start a new activity
m late :(
Animation Listener Already Contains the Method called as onAnimationEnd #override it on your
imgAries Animation Listener
onAnimationEnd(Animation anim){
//Start you Activity here using Intent
Intent i=new Intent(this,//Your next Activity);
startActivity(i);
}
That it...Now you can go to Next Activity on Animation End...Thks

How to enable Animation in android

I want to enable animations in my app dynamically as we do manually from Settings>Display>Animation>All Animation.
I have tried appended code but to no avail,
Settings.System.putInt(getContentResolver(), Settings.System.WINDOW_ANIMATION_SCALE, 1);
Settings.System.putInt(getContentResolver(), Settings.System.TRANSITION_ANIMATION_SCALE, 1);
Please help
Imran
i think., you can do it for your app., but if you are taking it for your device means you can read the flags., if it is disabled you can intent the setting panel to open to enable it by the user.
source:
animation enable
// declare animation
Animation animationSlideInLeft, animationSlideOutRight;
// Now we are giveing image view animated
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
animationSlideInLeft = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
animationSlideOutRight = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
animationSlideInLeft.setDuration(1000);
animationSlideOutRight.setDuration(1000);
animationSlideInLeft.setAnimationListener(animationSlideInLeftListener);
animationSlideOutRight.setAnimationListener(animationSlideOutRightListener);
curSlidingImage = image1;
image1.startAnimation(animationSlideInLeft);
image1.setVisibility(View.VISIBLE);
// Create animation listener
AnimationListener animationSlideInLeftListener
= new AnimationListener(){
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if(curSlidingImage == image1){
image1.startAnimation(animationSlideOutRight);
}else if(curSlidingImage == image2){
image2.startAnimation(animationSlideOutRight);
}else if(curSlidingImage == image3){
image3.startAnimation(animationSlideOutRight);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}};
// and paus clear the listener
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
image1.clearAnimation();
image2.clearAnimation();
image3.clearAnimation();
}
Reference from here
2.Same this one you can do with add xml into res/anim/ folder

A view after an scale animation, have not resize to new position

A view after an scale animation, have not resize to new position.
I using a android animation (ScaleAnimation) to make a layout(screen size when start) scale to 0.95 times size by itself and the scale reference the screen central point.
final AnimationListener al2 = new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
}
};
public void animScale(){
ScaleAnimation sa = new ScaleAnimation(1,0.95f,1,0.95f, topLayout.getMeasuredWidth()/2, topLayout.getMeasuredHeight()/2);
sa.setDuration(500);
sa.setFillEnabled(true);
sa.setFillAfter(true);
sa.setAnimationListener(al2);
topLayout.startAnimation(sa);
}
public void onCreate(Bundle savedInstanceState) {
scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
animScale();
}
});
}
after this animation my layout have been changed to new position, but when i click again the button
the layout always from screen size scale to 0.95 times.
that show me the layout never change the actual size through the animation.
what code i need to add in the animation listener animation end?
i hope to achieve when i click the button it will do that,
screen size -> screen size *0.95 -> screen size *0.95^2 ->........
thanks a lot.
The problem occurs because you are using Animation class. With Animation you only change the visible appearance.
In your case you need to use Animator which also updates positions and dimensions.
The difference between Animation and Animator is explained in this question.
ScaleAnimation doesn't changes layout's parameters. It animates without changing parameters for a smoother animation.
You can create a Boolean instance variable
Boolean animationDone = false;
and set it to true in onAnimationEnd to indicate if the animation is done
public void onAnimationEnd(Animation arg0) {
animationDone=true;
}
and check it in animScale before animating to prevent multiple animations....
public void animScale(){
if ( animationDone ) return;
//..... animation code here...
}

android temporarily use setEnabled(false)

I am animating an ImageView so that when you click the image the animation occurs (then later it resets) but my problem is that if you click again where the image sat originally - the animation starts from the beginning right away with out finishing (it just resets and starts again.
so I tried using
setEnabled(false)
which works great, the animation continues on its path up perturbed by any random clicking, now the only problem is getting the ImageView enabled again - at about the same time as the animation stops
here's what i have
stopImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mpButtonClick.start();
stopImage.setEnabled(false);
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
stopImage.startAnimation(anim);
now is there an easy way i can call setEnabled(true) after a some time has passed?
You could try using an AnimationListener and then calling setEnabled(true) from onAnimationEnd().
Something like this:
stopImage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
mpButtonClick.start();
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
anim.setAnimationListener(new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
stopImage.setEnabled(false);
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
stopImage.setEnabled(true);
}
});
stopImage.startAnimation(anim);
}
}
Here's the documentation for AnimationListeners.

Categories

Resources