On button click I would like to make an image move either down or right. The issue I am running into is that the movement of the image only occurs once. In onClick() method I am just calling either one of my move methods. Here are the move methods.
private void moveLeftToRight() {
animation = new TranslateAnimation(0.0f, 200.0f, 0.0f, 0.0f);
animation.setDuration(1500); // animation duration
animation.setFillAfter(true);
iv1.startAnimation(animation); // start animation
}
private void moveUpToDown() {
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 200.0f);
animation.setDuration(1500); // animation duration
animation.setFillAfter(true);
iv1.startAnimation(animation); // start animation
}
I can call either one of these one time for the first time. Thereafter the animation methods do not work. I am wondering if I need to reset or something. Any ideas? Thanks in advance.
When you pass your coordinates to TranslateAnimation those coordinates are relative to 0,0 and not the views coordinates.
To fix this we will get the views coordinates with view.bottom, view.left etc.
Then add the values you'd like to transform by to those values, pass those to TranslateAnimation, and your translation will work as expected.
Related
I have ScaleAnimation which change size of some View from small to big and then back to small (with RepeatMode = Reverse). It is repeated infinitely.
Each time animation has been repeated, I need to do some operation. I use onAnimationRepeat callback which is called when Animation reaches smallest or biggest state. But how to resolve which one of them? In other words how to get current value of ScaleAnimation?
Animation animation = new ScaleAnimation(
1f, 5f,
1f, 5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationRepeat(Animation arg0)
{
//I NEED TO FIND OUT HERE WHETHER THE VIEW IS IN BIG OR SMALL STATE
//I can use below function but it works only on API >= 29
someView.getAnimationMatrix();
}
//other methods
});
someView.StartAnimation(animation);
I found getAnimationMatrix() method, which returns matrix of current animation values, but it is only available from API 29 and I need to support also lower Android versions.
I'm animating a view and I want to reset the view to the original position after animation ended.
This is what I have:
rl2 is a relativeLayout
rl2.animate().translationX(-60).translationY(117).setDuration(2000);
I tried setting this but its not working:
rl2.clearAnimation();
clearAnimation(); does not reset your animations, it just stops them and removes them from the animation queue. To undo your animations you need to actually undo them. So, for your code block you will need to call rl2.animate().translationX(0).translationY(0).setDuration(2000); to move the view back to its original position.
As #Chris Stillwell mentioned on his answer, But you can move View back to it's original position after translation animation by
rl2.animate().translationX(0).translationY(0);
I know this question was asked long time ago but someone may still look for answer.
I use the Animation class for this kind of thing; there is a setFillAfter method in this class, so you can set if you want the animation to apply its transformation after it ends.
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
1f, 2f, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(false); // no needed to keep the result of the animation
anim.setDuration(1000);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
gangsterImage.startAnimation(anim);
If you're going to remove view don't use animation.removeAllListeners(), the function cannot continue that's why you will see some error. If you want to disappear a view, use View.GONE
in Kotlin like this:
animation.doOnEnd {
binding.mainLayout.visibility = View.GONE}
But by this way you couldn't remove any animation! So it' better rest them to the initial position.
binding.tvM2.animate().translationX(0f).translationY(0f).rotation(0f)
just use anim.setFillAfter(false); to reset automatically after animation ends.
use Tween Animation code:
#Override
public void onClick(View v) {
RotateAnimation rotateAnimation = new RotateAnimation (0,90,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
rotateAnimation.setDuration (2000);
rotateAnimation.setFillAfter (true);
image2.startAnimation (rotateAnimation);
}
of cause use Tween Animation can do that,but Tween Animation hava a defect.
For example the following gif click event does not move.
So I want use property Animation to do that,but I found property Animation can only rotate around the Center.
how can i use property Animation to rotate view around the point?
I think from your question that you want rotation around the mouse pointer.
Try this, I think it should work but I haven't had a chance to try it yet.
...
Animation rotateAnimation = new RotateAnimation(0.0f, 360.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.0f);
...
I have three animations like scale, translate and scale. I play those animations in order. First two is working fine but last scale animation reset the position of view to original. If I remove last scale animation, it's working fine the view stay the new position after translate animation. Do you have any idea about this behavior?
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
ScaleAnimation scaleAnimation1 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
scaleAnimation1.setDuration(500);
scaleAnimation1.setFillAfter(true);
TranslateAnimation moveAnim = new TranslateAnimation(0, -x, 0, -y);
moveAnim.setDuration(1000);
moveAnim.setStartOffset(500);
moveAnim.setFillAfter(true);
ScaleAnimation scaleAnimation2 = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
scaleAnimation2.setDuration(500);
scaleAnimation2.setStartOffset(1000);
scaleAnimation2.setFillAfter(true);
scaleAnimation2.setFillBefore(true);
animationSet.addAnimation(scaleAnimation1);
animationSet.addAnimation(moveAnim);
animationSet.addAnimation(scaleAnimation2);
scaleAnimation2.setFillAfter(true); - If fillAfter is true, the transformation that this animation performed will persist when it is finished
scaleAnimation2.setFillBefore(true);- If fillBefore is true, this animation will apply its transformation before the start time of the animation.
These two properties cannot work simultaneously, and the one which set later is effective.
So, remove scaleAnimation2.setFillBefore(true); may solve your problem.
I am trying to move a view inside a layer, something like an Horizontal Scroll, I have done it with motion events and I need to add an animation. The view should follow the finger on the screen all the time and when it's released it should, from that position move to the edge of the screen. Now I managed to do all of this but I have problems with the animation coordinates since it takes on only float values and I need it to take 2 digit values after the 0.
Here is my animation attempt:
public static Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, temp/1000,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
return inFromRight;
}
temp should be the value where the finger is and it's usually something between 100-300, so I divide it by 1000 to get 0.36 lets say but because the animation will accept temp only as float I get only 0.3. Any ideas how I can make it to get 0.36?
Try (float)temp/1000.0 instead.