How to stop imagebutton at current position when clicked during animation? - android

I have an ImageButton which moves down from the top of the screen. The image animates while moving down to the screen. I want to make it stop WHEREVER and WHENEVER it is clicked on. Basically if the image is clicked in the middle of the animation, I want to it to stop at that current position. My progress can be seen in the code block below, the code makes the image animate and move from top to bottom, when the image is clicked, the image moves straight to the desired position (500), instead of stopping at the current position. Please be really descriptive as I am a beginner in programming. Thank you.
final ImageButton image = (ImageButton)findViewById(R.id.image);
final ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, 0, 500); //where the image should move to
objectAnimator.setStartDelay(2000); //how long to wait before starting
objectAnimator.setDuration(2000); //how long animation lasts
objectAnimator.start();
image.setOnClickListener(
new ImageButton.OnClickListener() {
public void onClick(View v) {
image.setTranslationY(image.getY()); //gets current position of Y?
objectAnimator.end(); //ends the animation
}
}
);

From the documentation of ValueAnimator#cancel it seems that
objectAnimator.cancel();
should solve your problem.

Related

Change object position dynamically using objectAnimator

I am trying out something and my aim is to move a button to a specific point on the screen. I have calculated the y-coordinate of the new position, then moveOn is called to update the position of the button.
The problem I have is that, after every call, button first returns to its initial position before moving. Please help a newbie.
public void moveOn() {
ObjectAnimator animation = ObjectAnimator.ofFloat(button, "translationY", translation_value);
}

Multiple animations within a relative view

I am trying to animate a pointer around the screen in an android application.
I am using an imageview as the pointer inside a relative layout as follows:
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
pointer = (ImageView) findViewById(R.id.pointer);
pointer.animate().setDuration(2000);
I then want to move the pointer up 10 pixels every time I click a button:
// ONCLICK LISTENER FOR LEFT BUTTON
btnUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//get positon of pointer
leftPoint = pointer.getLeft();
topPoint = pointer.getTop();
int xValue = container.getWidth() - pointer.getWidth();
int yValue = container.getHeight() - pointer.getHeight();
pointer.animate().x(leftPoint).y(topPoint-10);
}
});
This works the first time I click the button but will not move it any subsequent times. I have tried making the int points static but this did not help.
Any help greatly appreciated.
This might sound counter-intuitive, but animating a view doesn't change its location. Your code is demonstrating it. When you do this line:
topPoint = pointer.getTop();
You get the same value every time.
You need to change pointer's position at the end of the animation.
This answer shows how someone else has done it.
Other Considerations
You could use object animator, if you are only targeting SDK 11 and above. Here is a little intro to it.
If you want to interrupt your animation, you can check its status, and find out its current y offset. You can add that to the position of the view before starting the animation. Here is an example.

Animate an ImageView by saving starting and ending position

I have an ImageView which can be moved by finger with a touchListener. I want the user to save two positions of the Imageview by pressing a button and after that, when he press to play the animation, to start from the first position and finish to the second.
How this can be possible? I try by saving in an array the two position by using getX and getY and after put them in a TranslateAnimation, but it doesn't work
public void get1Cos(){
x[1] = player.getX();
y[1] = player.getY();
}
public void get2Cos(){
x[6] = player.getX();
y[6] = player.getY();
}
anim = new TranslateAnimation(x[1], x[6], y[1],y[6]);
anim.setFillAfter(true);
anim.setDuration(2000);
player.startAnimation(anim);
Try this:
player.startAnimation(animation);

Buttons on an animated view respond to clicks even when off screen?

I have a LinearLayout, I'm applying a translation animation to it. I'm filling the animation before and after. Visually it works fine. The animation ends by translating the view off screen. But if I click an x,y coordinate on screen that happens to be where the view was at some point during its animation, a button on the view has its click listener fire.
The only solution I've found is to add an animation listener, and when the animation ends, mark the buttons on the (now out of view) layout to visibility=gone, enabled=false. This seems bizarre - the view is no longer on screen, but it's still responding to click events. Is this a known thing, I'm probably not setting the animation up correctly?
Thanks
----- Update --------
I refactored my animation a little. Instead of using animation.setFillAfter(true), I set the layout's visibility to GONE when the animation is complete. Now it doesn't register clicks when off-screen. Still interested to know if this is a known thing, as it'd be easier to simply not have to add an animation listener etc.
Translate Animations on lower level API( below honey comb) changes where the button is drawn, but not where the button physically exists within the container. So, you are on your own to handle this situation. For more information about this you can refer to this link. One way is to actually change the location of the button in the layout(not by animation). Here is how you can achieve this:
params = (LayoutParams) mBtn.getLayoutParams();
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 400);
animation.setDuration(2000);
animation.setAnimationListener(mAnimationListener);
mBtn.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);
}
};
Here by changing the layout params we are changing the physical position of the button.
In your case as view is going off the screen so you just need to change the visibility of the button(View.GONE) on animation end.

How can draw frame by frame animation on image view, both visible simultaneously in Android?

I want to draw animation on image view and the image position is changing on run time if user successfully tap on the image then an animation is to be draw.Here is my code
public void enter() { //To enter in to animation
setBackgroundResource(R.anim.dhakkan_animation);
frameByframe_animation = (AnimationDrawable) getBackground();
frameByframe_animation.start();
frameByframe_animation.setOneShot(true);
}
#Override
public void exit() {
//when exit from Animation
frameByframe_animation.stop();
frameByframe_animation.setVisible(false, false);
}
the problem is that when animation darws my image of image view get invisible. According to my requirement i have to show both simultaneously in background an image(image of image view) and in foreground animation play.Plese help me im totally frustated.
If you want to show imageView, when your animation goes, just do it in *.xml:
<ImageView android:id=#+id/s210 ..... android:src="#drawable/s210 />

Categories

Resources