I’m trying to do an animation which on click right button slides an image from where it is to right of layout, and on click left button slides the image from where it is to left of layout.
I’ve done this so far:
public class DenemeActivity extends Activity implements View.OnClickListener {
Button LeftSlide, RightSlide;
View image;
int width;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LeftSlide = (Button) findViewById(R.id.slideLeft);
RightSlide = (Button) findViewById(R.id.slideRight);
image = (View) findViewById(R.id.imageV);
width = getWindowManager().getDefaultDisplay().getWidth(); ;
LeftSlide.setOnClickListener(this);
RightSlide.setOnClickListener(this);
}
public void onClick(View v) {
runOnAnimation(v.getId(), image);
}
void runOnAnimation(final int id, final View view) {
AnimationSet animation = new AnimationSet(true);
TranslateAnimation translateAnimation = null;
int fromXDelta = view.getLeft();
if(id == LeftSlide.getId())
translateAnimation = new TranslateAnimation(fromXDelta, 0, 0, 0);
else if(id == RightSlide.getId())
translateAnimation = new TranslateAnimation(fromXDelta, width - view.getWidth() - fromXDelta , 0, 0);
translateAnimation.setDuration(1000);
animation.addAnimation(translateAnimation);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation v) {
v.reset();
view.clearAnimation();
// Change view to state B by modifying its layout params and scroll
if(id == LeftSlide.getId())
view.layout(0, 0, view.getWidth(), view.getHeight());
else if(id == RightSlide.getId())
view.layout(width - view.getWidth(), 0, width, view.getHeight());
}
public void onAnimationRepeat(Animation v) {}
public void onAnimationStart(Animation v) {}
});
view.startAnimation(animation);
}
}
I’ve checked the points:
width = 480
fromXDelta = 408 (when click on left)
So it’s not out of border indeed, but when i click on left button, the image comes from out of border ?? What can it cause to this?
Thanks in advance…
I think you got the wrong position type for the View you want to move.
Try to get the position of your View and implement the translation as follows:
This should do the trick.