Actual position of view not changing after setfillafter(true) in android animation - android

I am moving the view via this code, but the actual position of view is not changed, why
TranslateAnimation ta = new TranslateAnimation(0, 0, Animation.RELATIVE_TO_SELF, -mbar4.getHeight());
ta.setDuration(1000);
ta.setFillAfter(true);
v4.startAnimation(ta);

up to version 3 (API 11) of android excluding , all animations don't really change the view , only the way it is shown . not only that, but i think they don't use the GPU at all.
in order to check it out , you can use a button and setOnClickListener for it , and see that no matter which animation you use , the click will work only on its original position and size.
here's a sample code of moving a view using the translateAnimation:
final int deltaXToMove=50;
TranslateAnimation translateAnimation=new TranslateAnimation(0,deltaXToMove,0,0);
int animationTime=1000;
translateAnimation.setDuration(animationTime);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
final Button b=(Button)findViewById(R.id.button);
translateAnimation.setAnimationListener(new AnimationListener()
{
#Override
public void onAnimationEnd(Animation animation)
{
animation.setFillAfter(false);
FrameLayout.LayoutParams par=(LayoutParams)b.getLayoutParams();
par.leftMargin=deltaXToMove;
b.setLayoutParams(par);
}
...
b.startAnimation(translateAnimation);

Because TranslateAnimation only changes where the View is drawn.
Try this:
TranslateAnimation ta = new TranslateAnimation(0, 0, Animation.RELATIVE_TO_SELF, -mbar4.getHeight());
ta.setDuration(1000);
ta.setFillAfter(true);
ta.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
((RelativeLayout.LayoutParams)v4.getLayoutParams()).bottomMargin = mbar4.getHeight();
v4.requestLayou();
}
});
v4.startAnimation(ta);
Change RelativeLayout.LayoutParams to whatever the parent Layout is.

Related

Have scale animation AFTER translate animation from last position

I want to apply a translate animation to the view from bottom right to center THEN scale animation from where it finished translation. Once this is done, I want the view to come back to its original size and position. Any idea how to do that? Here is the code I am using for animation
TranslateAnimation translateAnim = new TranslateAnimation(0, midScreenX, 0, midScreenY);
translateAnim.setDuration(2000);
ScaleAnimation scaleAnim = new ScaleAnimation(1, 4, 1, 4, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,1f);
scaleAnim.setDuration(2000);
use an AnimationListener to chain the animations and use setFillAfter(true)
TranslateAnimation translateAnim = new TranslateAnimation(0, midScreenX, 0, midScreenY);
translateAnim.setDuration(2000);
// user setFillAfter to make sure that we scale from the last position it was animated to.
translateAnim.setFillAfter(true);
// make scaleAnim final so we can use it in the AnimationListener.onAnimationEnd method
final ScaleAnimation scaleAnim = new ScaleAnimation(1, 4, 1, 4, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,1f);
scaleAnim.setDuration(2000);
scaleAnim.setFillAfter(true);
translateAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// now that the first animation is done, start the second
someViewObj.startAnimation(scaleAnim);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
// start first animation
someViewObj.startAnimation(translateAnim);
Android Docs : http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)

Button animation doesn't stop

I apply this animation to a button (RelativeLayout) while the user hold or move (while is pressed) its touch:
ScaleAnimation start = new ScaleAnimation(1, 1.2f, 1, 1.2f, 0x1, 0.5f, 0x1, 0.5f);
final ScaleAnimation end = new ScaleAnimation(1.2f, 1, 1.2f, 1, 0x1, 0.5f, 0x1, 0.5f);
end.setDuration(600);
start.setDuration(600);
button.startAnimation(start);
button.postDelayed(new Runnable() {
#Override
public void run() {
button.startAnimation(end);
}
}, 600);
When the user releases the button, another animation must be called:
TranslateAnimation translateOut = new TranslateAnimation(0, -width, 1, 1);
translateOut.setDuration(600);
button.clearAnimation();
button.startAnimation(translateOut);
button.setVisibility(RelativeLayout.INVISIBLE);
It works as it should, but the problem is that the button appears and desappears (flashes) at the users last touch position.
I already tried this codes to try to fix it (and created a flag to know if the start/end animation is running), but none of them worked:
start.cancel();
end.cancel();
start/end.setFillAfter/Before(true);
button.clearAnimation();
Any solutions?
You are setting your button invisible right after you start the animation, so your button is invisible when the animation is running. Try to use an AnimationListener to set the visibility to invisible when the animation is over.
start.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
button.setVisibility(View.INVISIBLE);
}
});

Click Listener behavior when two layouts get animated

I have two buttons.When first button is pressed i am translating my layout layout to top and from the top of the screen another layout will come.But my issue is when came back to first layout the click events of the second layout still get fired at its previous position.So what is the Solution of it?I found a lot here on SO as well as on Google but still cannot get the right solution yet.So Please someone help me for my this issue.Thanks in Advance.
TranslateAnimation tr1 = new TranslateAnimation(0, 0, 0, -1100);
tr1.setDuration(1000);
tr1.setFillAfter(true);
layout_login.startAnimation(tr1);
tr1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
layout_signup.setVisibility(View.VISIBLE);
TranslateAnimation tr2 = new TranslateAnimation(0, 0,
-1100, 0);
tr2.setDuration(1000);
tr2.setFillAfter(true);
tr2.setInterpolator(MainActivity.this,
android.R.anim.linear_interpolator);
layout_signup.startAnimation(tr2);
tr2.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
home_activity_btn_login.setEnabled(true);
}
});
}
});
I think this is a bug with the old animator schemes (I beleive a pretty well known bug, involving fill after not working sometimes). Try using ObjectAnimator instead
Here is an example,
ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", 0, 100f);
oa.setDuration(1000);
oa.start();
If you want to move in the Y direction, you can use translationY. If you want to move in both directions, you need a translationX and translationY, and use an AnimatorSet to play simultaneously.
Check out this comment to this question. Using the old Animation API, apparently dispite fillAfter(true), the buttons click position remains the same. This confirms your issue. So just use the new API, and you should be in good shape.

How to avoid the blink image when save the image after rotate animation?

I am confusing the translate animation and rotate animation. In my game I use this two animations, after completing the animation I am save my image. In translate animation it is fine, but after completing rotate animation my image is blink once. See my code in bellow, please solve my problem……..
Why anyone not respond my question, it is not understand or I am asking any wrong question ? Please tell me reason.................
Thanks.
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.train);
//1)
TranslateAnimation TAnimation=new TranslateAnimation(0, 0, 0,-100);//bottom to start
TAnimation.setInterpolator(new LinearInterpolator());
TAnimation.setDuration(2000);
TAnimation.setFillAfter(false);
TAnimation.setFillEnabled(true);
//TAnimation.setFillBefore(true);
Train.startAnimation(TAnimation);
TAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(x, y, 0, 0);
Train.setLayoutParams(param);
Train.setImageBitmap(bmp);
}
});
//x and y values are exact position of compliting translateanimation position
//2)
RotateAnimation RAnimation=new RotateAnimation(0,90,50,25);
RAnimation.setInterpolator(new LinearInterpolator());
RAnimation.setDuration(2000);
RAnimation.setFillAfter(false);
TAnimation.setFillEnabled(true);
//RAnimation.setFillBefore(true);
Train.startAnimation(RAnimation);
RAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(x, y, 0, 0);//x and y values are exact position of compliting translateanimation position
Train.setLayoutParams(param);
Train.setImageBitmap(bmp);
}
});
i had that problem but it's reaaaaally simple to fix. You don't need to implement the animation listener, simple don't do it (i had your problem because I use that way).
Do your animation and before call the animation method:
setFillAfter(true); //this save view at the end of the animation
Like so:
//my animation
final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_up);
//hide login content
content.setVisibility(View.GONE);
//animContent = AnimationUtils.loadAnimation(getActivity(), R.anim.show_up);
rotation.setFillAfter(true);
//animate the arrow
arrow.startAnimation(rotation);
So, delete the listeners and change your setFillAfter(false) to TRUE. Will works ;)

How to animate a child view and having the parent view to automatically resize?

I've a quick question.
I'm trying to animating a child view to make it disappear. That part is easy, but, during the animation, the parent view is not resized. How can I do to make the parent view automtically resize during the animation?
Here is my current code:
AnimationSet set = new AnimationSet(true);
Animation fade = new ScaleAnimation(1.f, 1.f, 1.f, 0);
Animation fade2 = new AlphaAnimation(1, 0);
set.addAnimation(fade);
set.addAnimation(fade2);
set.setDuration(2000);
set.setFillAfter(true);
bgColor.startAnimation(set);
bgColor is the View I'm making disappear. Here is the final result :
Result
You can see the gap after the animation.
Any ideas?
Thanks in advance!
You may add an AnimationListener and do a change of the layout after the animation is done
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
bgColor.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
Also, be sure to have your stuff together regarding onFillAfter()!

Categories

Resources