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);
}
});
Related
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)
I am confused with a problem:
Animation animation = new TranslateAnimation(0, 0, 200, 0);
animation.setDuration(800);
animation.setFillAfter(true);
btnView.startAnimation(animation);
btnView.setVisibility(View.VISIBLE);
I run this code work right but when I add the below code:
btnView.clearAnimation();
The animation can't show, how to clear animation after this?
if you want to clear the animations for the view once the animations i finished
you have to override Animation Listener of view and then clear the animation
if you want your view to move back to intial postion just setFillafter(false);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO set params of the view to required position
}
});
To clear animation after it is finished set setFillAfter to false.
use this way:
// start animation
TranslateAnimation anim = new TranslateAnimation( 0, 100 , 0, 100);
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
// end animation or cancel that
view.getAnimation().cancel();
view.clearAnimation();
cancel()
Cancel the animation. Canceling an animation invokes the animation
listener, if set, to notify the end of the animation.
If you cancel an animation manually, you must call reset()
before starting the animation again.
clearAnimation()
Cancels any animations for this view.
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.
I am trying to perform 2 text animations in a row with my TextView under Android
first:
TextView target = (TextView)findViewById(R.id.textanimation);
target.setText(text1);
target.startAnimation(animationSet1);
animationSet1 is a set of alpha and translate animations:
Animation alpha = new AlphaAnimation(0.0f, 1.0f);
Animation a = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0);
alpha.setDuration( 1000 );
a.setDuration( 1000 );
When onAnimationEnd happens in animationSet1 I immediately start another animation
TextView target = (TextView)findViewById(R.id.textanimation);
target.setText(text2);
target.startAnimation(animationSet1);
However in the 2nd animation I can see on a split of second a fully opaque text, however it should start with 0 alpha. How to avoid this situation? Will appresiate any answer.
P.S. Even if I have 2 separate textviews with the prepared text in both - same bug is seen.
If I put some delay between the animations both of them become smooth.
Sergey
I'm not sure how you are triggering the start of the 2nd animation following the termination of the 1st animation. I'm guessing you're checking to see if the animation on a view is null. This isn't the best or correct way to do it. Instead you should you an AnimationListener
Animation animation1 = new Animation();//Replace with your animation
animation1.setDuration(DURATION_1);
animation1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationEnd(Animation arg0) {
Animation animation2 = new Animation();
animation2.setDuration(DURATION_2);
view2.startAnimation(animation2);
}
});
view1.startAnimation(animation1);
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 ;)