Animate an ImageView by saving starting and ending position - android

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);

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);
}

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

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.

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.

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.

ImageView will not trigger after click on the image, but will trigger after click on TextView

I have an ImageView in my xml file, i want to rotate the image when it's clicked.
I use the following code to archive this:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
img = (ImageView) findViewById(R.id.imageView1);
Animation an = new RotateAnimation(0.0f, 360.0f, img.getWidth() / 2,
img.getHeight() / 2);
an.reset();
// Set the animation's parameters
an.setDuration(1000); // duration in ms
an.setRepeatCount(0); // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true); // keep rotation after animation
//an.start();
img.setAnimation(an);
}
return true;
}
But te problem is, when i press on the image nothing happens, the image won't turn. But if i click on the image and after that click on a TextView, the image does rotate.
This is so random.
What am i doing wrong? How can i fix this?
Thanks.
Well, it seems that you are calling the onTouchEvent function for your whole activity. Thus, any touch action not "consumed" by a view inside your activity window will trigger this function. So it would be logical that you touching somewhere on your activity such as your TextView would trigger this image rotation thing.
My best guess seeing your code would be this: it would be best to implement a touch/click event listener for your ImageView itself, not for your whole Activity. Here is a code snippet that does this:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/*YOUR CUSTOM CODE AT ACTIVITY CREATION HERE*/
/*here we implement a click listener for your ImageView*/
final ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Animation an = new RotateAnimation(0.0f, 360.0f, img.getWidth() / 2, img.getHeight() / 2);
an.reset();
/* Set the animation's parameters*/
an.setDuration(1000); // duration in ms
an.setRepeatCount(0); // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true); // keep rotation after animation
//an.start();
img.setAnimation(an);
img.invalidate(); //IMPORTANT: force image refresh
}
});
}
I would second the recommendation by epichoms (using an OnClickListener). Also, make sure your ImageView can receive clicks:
final ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
img.setFocusable(true);
img.setOnClickListener(new View.OnClickListener(){
...
You can set these values in your XML layout as well:
android:clickable="true"
android:focusable="true"

Categories

Resources