Reputation: 523
I've an ImageButton that I want to move when pressed and when animation finish I want that this button stops in the new position.
This is button code:
<ImageButton
android:id="@+id/move_button"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:scaleType="fitCenter"
android:background="@drawable/background_button"
android:src="@drawable/move_button"
android:onClick="MoveButton" />
I've found a code to do that in this site:
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0, 0, 100, 0);
anim.setDuration(300);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
params.topMargin += -100;
view.setLayoutParams(params);
}
});
view.startAnimation(anim);
}
When button it's pressed it start the animation, but when animation is complete button return to initial position and application crashes.
What can be the problem?
Upvotes: 5
Views: 10822
Reputation: 176
Import the necessary libraries:
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
Get the button you need to use:
Button button = findViewById(R.id.button_id);
Now in order to animate button
you will need to use the TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
function which takes 4 float arguments.
Animation Example:
Animation your_animation = new TranslateAnimation(0,100,0,0);
The first argument specifies the start of the x position and the second one the end of the x position, Same thing for the other two but the y position instead.
You will also need to set your animation's duration in milliseconds using
your_animation.setDuration(1000);
Your button will get back to its previous position when the animation ends, in order to make the button stay to its new position, use this:
your_animation.setFillAfter(true)
For more information on animations https://developer.android.com/training/animation
Upvotes: 0
Reputation: 514
Use ObjectAnimator
ObjectAnimator animation = ObjectAnimator.ofFloat(YOUR_VIEW, "translationX", 100f);
animation.setDuration(2000);
animation.start();
This code will move the View
100 pixles to the right, over a period of 2 seconds.
If you need more information go to Developers Guide
Upvotes: 0
Reputation: 1386
Use anim.setFillAfter(true)
to situated the View
at the position where Animation ends.
One thing more you are animating your ImageButton
from 100 to 0 in Y coordinates, thats why your ImageButton
comes to intial position because 0 is its intial position.
Try below code in this code I used anim.setFillAfter(true)
and animate the ImageButton
from 0 to 100 in Y coordinates.
public void MoveButton(final View view) {
TranslateAnimation anim = new TranslateAnimation(0,0,0,100);
anim.setDuration(300);
anim.setFillAfter(true);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(anim);
}
Let me know if this is helpful for you.
Upvotes: 1
Reputation: 3964
This is work Definitely.
Button im= (Button) findViewById(R.id.button);
//set position TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta
final Animation animation = new TranslateAnimation(0,100,0,0);
// set Animation for 5 sec
animation.setDuration(5000);
//for button stops in the new position.
animation.setFillAfter(true);
im.startAnimation(animation);
Upvotes: 5