Reputation: 6591
I have performed a Translation animation on button ,everything work as i excepted ,but only the problem is after the animation the button not responding to click event please correct me
Button b=(Button)findViewById(R.id.button1);
TranslateAnimation slide=new TranslateAnimation(0, 30, 0,-40);
slide.setDuration(500);
slide.setZAdjustment(TranslateAnimation.ZORDER_TOP);
slide.setFillAfter(true);
b.startAnimation(slide);
Upvotes: 16
Views: 20742
Reputation: 378
ClickListener won't work, but you can use onTouchListener:
view.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
// call you click action here
true // Consume the touch event
} else {
false // Pass the event to other listeners
}
}
Upvotes: 0
Reputation: 24423
I faced this issue and I did fix it just some second ago. So, I think that I should share my solution with you guys.
In animation xml file,I removed android:fillAfter="true"
when keep android:fillEnabled="true"
.
Register Animation listener, then in onAnimationEnd()
method, I call View#Layout()
to change the position of the view.
int newLeft = (int) (layoutContent.getLeft() + layoutContent.getWidth() * 0.8);
layoutContent.layout(newLeft,
layoutContent.getTop(),
newLeft + layoutContent.getMeasuredWidth(),
layoutContent.getTop() + layoutContent.getMeasuredHeight());
In my case, what the animation do is that slides the layoutContent to leftside 80% of width.
It works fine. Hope this helps.
@Update: Today, you can use ObjectAnimator
on android 3.0 +. If you are developing for android under 3.0, you can find it at support library v.4. ObjectAnimator
is bester for animation.
@Update#2: You can use ViewPropertyAnimator on android api higher 12 version. It provides better performance, and fix problem with click events. Example:
mButton.animate()
.setDuration(TIME)
.translationY(VALUE)
.start();
Upvotes: 10
Reputation: 429
I could achieve what you wanted but you will have manage co-ordinates manually. See if it works for you.
public class AnimationTestActivity extends Activity {
private Button mButton;
private LinearLayout.LayoutParams params;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(mClickListener);
params = (LayoutParams) mButton.getLayoutParams();
}
private android.view.View.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 400);
animation.setDuration(2000);
animation.setAnimationListener(mAnimationListener);
v.startAnimation(animation);
}
};
private AnimationListener mAnimationListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
params.topMargin = params.topMargin + 400;
mButton.setLayoutParams(params);
}
};
}
Upvotes: 5
Reputation: 429
This will translate the object in y direction:
ObjectAnimator mover = ObjectAnimator.ofFloat(v, "translationY", 0, 400);
mover.start();
Upvotes: 23
Reputation: 6591
I found an easy solution define the button final position in layout and start the animation from some position ,ie specify the fromX or fromY instead of putting as zero
Upvotes: 1
Reputation: 3095
If you are handling animations at lower level API (below HoneyComb) your animation actually does not moves the button but only its images.Its click will be at same place where you have placed it in your layout.
Upvotes: 22
Reputation: 13101
The animation do not change the View actually Position. even if you setfillAfter(true), The position it can accept click event is the original position.
Upvotes: 4