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 :)
Related
I am trying to have a view animated in my app and am using NineOldAndroid for animations.
The desired effect is to fade the view out and then set it's visibility to gone so that it doesn't get clicked while invisible. Here is How I do it.
ViewPropertyAnimator.animate(view).alpha(0).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
The problem here is that the listener above sticks with the view and when I try to fade it in again the listener gets called again resulting the view to be GONE upon appearing.
ViewPropertyAnimator.animate(enterGallery).alpha(1);
How can I clear the listener after the view visibility is set to GONE in the first piece of code?
I found the solution and it would be to pass null as listener when making the view VISIBLE.
ViewPropertyAnimator.animate(view).alpha(1).setListener(null);
I've an activity that loads the resources and shows the message "Please wait, loading", and when all resources are loader, the textview changes to "Ready" and change the views of the activity. The problem is that I want to wait for the animation end, but the views are removed even before they start. How can I wait for that?
tview1.startAnimation(desvanecer); tview2.startAnimation(desvanecer);
tview1.setText(R.string.ready);
tview2.setText(R.string.launcher_continue);
tview1.startAnimation(aparecer);
tview2.startAnimation(aparecer);
layout = (RelativeLayout) findViewById(R.id.launcher_layout);
screen = new Screen(context);
thread = new Thread(screen);
layout.removeAllViews();
layout.addView(screen);
thread.start();
You can perform your removals at the end of the animation by using an animation listener and doing them onAnimationEnd
desvanecer.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//REMOVAL OR OTHER CODE HERE
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
What you need is an AnimationListener and implement the onAnimationEnd method. But have a look at my comment and the doubts I have about your code.
I have been working on trying to get an Activity to not show until data has been selected and is ready for display: Prevent screen display until after fetching data
Finally got it to work with the aid of themes.
Now I have another problem.
I need to animate the transition from one Activity to the next.
I know how to use overridePendingTransition but this won't work here since I am already in the target Activity when I want to do the animation.
The only reason I can see the other one is because the current one is transparent.
I have no problem sliding in the new one:
View view = getLayoutInflater().inflate(R.layout.content_screen, null);
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
setContentView(view);
However, I can't think of any way to get the old one to slide out.
Any ideas anyone?
Create a "slide out left" animation. Then adjust your animation for the new view, so when the animation starts, you apply and start the other animation to the other view. E.g.,
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO - Set and start other animation here
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(animation);
[Edit]
// First, define and infalte the view that will be incoming
View upcomingView = inflater.inflate(...);
// Next, we'll set the animation up
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Animation outAnimation = AnimationUtils.loadAnimation(context,
R.anim.slide_out_left);
upcomingView.startAnimation(outAnimation);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
currentView.startAnimation(animation); // This will start the animation defined above, which will also set and start the animation for the incoming object
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...
}
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.