So if I have a translation animation like
Animation anim = new TranslateAnimation(0, 0, 0, OFFSET);
anim.setDuration(1000L);
anim.setAnimationListener(listener);
anim.setFillAfter(true);
and I apply this to a textswitcher so that given a offset, the textswitcher will move from a location, A, up or down.
My textswitcher will stay at a given location, B, after the animation due to the setfillafter. How can I then from that location, B, reverse the animation and go back to location, A?
The Overall Question
So I'm translating a textswitcher down and then up, but of course, setfillafter only translates the bitmap of the view and not the view itself, so this may not be the best approach. I've also tried to do an animation and then offset of the view, but that looks glitchy. So basically, I want a view(textswitcher) which a swipe/fling down will move it down and touching(onClick) it when it's down will move it back up (same animation reversed). How can I do this?
I know how to do the swipe/fling and onClick stuff, just not how to implement the correct animation.
Perhaps you may want to consider adding an on touch listener to see where you touched it last, If above move up with a constant animation. This is if im understanding the question correctly.
Related
I want to translate animation perform on button. Here Buttons are drag able in layout. I drag the button on any place of screen and when I remove my touch,I want to perform transform animation from current drop able points of button to original place which is initially button located on the screen. if any one have idea then well come.
You can animate any View very simply with a View Property Animator like this:
button.animate().translationX(deltaX)
.translationY(deltaY)
.setDuration(duration);
This works on API level 11 and above. If it is supposed to work before API level 11 then you need to use View Animations:
TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
animation.setDuration(duration);
button.startAnimation(animation);
If you have any further question feel free to ask.
I have a translate animation of an image from location1 to location2. However at any time during the animation, if a user click a button then I want to apply another translate animation from where the image at to location3 (even if the animation has not finished). Any way I can do that?
Thanks
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc[0]-mSrcLoc[0], 0, mDestLoc[1]-mSrcLoc[1]);
translate.setFillAfter(true);
translate.setDuration(getDuration());
ball.startAnimation(translate);
void onbuttonClicked(){
// stop previous animation and translate image to location 3 "From where it stopped"
}
why don't you use ObjectAnimators?
You can create an animator for a particular target(or obtain the view one through animate() if you're running on a version greater-equal API level 12) and then translate, resize, fade,... your object.
If for some particular reasons the first animation is cancelled, you can call cancel and then the view will persist the current status. Then, if you create another animator based on the current values you should be able to easily perform another animation.
I found out that the movement of an image between two point can be achieved with this simple code:
ImageView image_to_move = (ImageView) findViewById(R.id.image_to_move);
TranslateAnimation animation = new TranslateAnimation(50, 100, 50, 100);
animation.setDuration(5000);
image_to_move.startAnimation(animation);
The image is an ImageView set initially in position (0,0) inside a FrameLayout and the code move it from the point (50,50) to (100,100).
What if i have to move it on a path, made of several point, in the form of absolute coordinate (x,y)? For example, move from (50,50) to (75,75) then to (100,100)
There's a way to pass to the translating function an array of coordinate or i have simply to iterate the animation for every couple of points?
Btw, i saw that if i don't initially locate the image in (0,0) the function moves the image in a weird way, like the coordinate i give are not read as absolute. Am i missing something?
EDIT: The last thing happens when i pass as parameters to the TranslateAnimation something like "n92.getX()" where n92 is a button on my frame layout, from which position i want the animation to start.
DETAIL: Just to understand better the problem: I am implementing the animation of a point moving on a map, to show the user how to get from one point to another of a map. That's why i have absolute coordinate (the reachable points) that must be followed (you can't arrive from one point to another without following a precise path on the map)
to do so you need to Calculate the position on a quadratic bezier curve. try this answer it might be work for you
I want to move my image view from its current position to some fixed position on screen using translate animation.
Also I want to know how translate animation works and what parameters it accepts exactly?
My piece of code is...
RelativeLayout.LayoutParams lParams = (LayoutParams) spreadImage
.getLayoutParams();
TranslateAnimation ta
ta = new TranslateAnimation(lParams.leftMargin,
randomLeftMarginsList.get(currentSpreadIndex),
lParams.topMargin,
ta.setAnimationListener(this);
ta.setDuration(ApplicationConstant.PUZZLE_GAME_IMAGE_SPREADING_TIME);
spreadImage.startAnimation(ta);
Thanks in advance.
Translate Animation controls the position and location of a layout or button or any view on which animation is being applied. It can move an object either in x direction or y direction.
Syntax :
TranslateAnimation transAnimation= new TranslateAnimation(fromXposition, toXPosition, fromYPosition, toYPosition);
fromXposition- x coordinate from where animation should start
toXPosition- x coordinate at which animation would end
fromYPosition- y coordinate from where animation should start.
toYPosition- y coordinate at which animation would end.
1)If we want to translate only in X direction then we set fromYPosition and toYPosition as zero.
2)If we want to translate only in Y direction then we set fromXPosition and toXPosition as zero.
There is another method in which we ,create an anim folder in the res folder. In this folder we add our animation xml .We use a translate tag in which we specify the attribute values.
In the below xml
android:duration defines the time of execution of animation
android:repeatCount specifies the no. of times the animation should be repeated ,
android:fromYDelta defines y coordinate from where animation should start
android:toYDelta defines y coordinate at which animation would end.
line_translate.xml
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
<translate android:duration=”300″ android:repeatCount=”1 android:fromYDelta=”0.0″ android:toYDelta=”174.0″ />
Code:
Animation lineTranslate;
//loading xml from anim folder
Animation localAnimation = AnimationUtils.loadAnimation(this, R.anim.line_translate);
//You can now apply the animation to a view
view.startAnimation(transAnimation);
Translate Animation can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply a translate animation to a view, it would move to a new position but its click events would not get fired whereas the click events would still get fired at its previous position. This happens because the view is still at its original position.
In order to overcome this, we can use ObjectAnimation which actually moves an object. Object Animation is the only animation which actually moves an object. You can create Translate animation using ObjectAnimator.
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX);
transAnimation.setDuration(3000);//set duration
transAnimation.start();//start animation
view -this is the view on which animation is to be applied
propertyName-The property being animated.
FromX,toX-A set of values that the animation will animate between over time.
Hope this will give you nice understanding.
You just need to translate your view one position to another position. So need to use below code to achieve your task.
imgHeart.animate()
.scaleXBy(-6f)
.scaleYBy(-6f)
.alpha(.1f)
.translationX((heigthAndWidth[0] / 2) - minusWidth) // trying to make my location
.translationY(-((heigthAndWidth[1] / 2) - minusHeight))
.setDuration(1000)
.start();
You could use NineOldAndroids. it has examples on translate animations.
As we know , the android coordinate system is start from the top left corner of the android screen. The x-axis is down growth and the y-axis is right growth.But I found it's not right for the animation.
For example, I initialized the TranslateAnimation using the constructed function:
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, 200);
Does the coordinate system have changed ? I found it didn't start from the top left corner.
Then I initialized the other translateAnimation for moving up and right direction :
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, -200);
ta.setReaptMode(Animation.REVERSE);
The same behavior would be found.
I am confused about it.
I believe that constructor for TranslateAnimation uses deltas. See this. Or look at the constructor sig. : (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta). So if you want your anim. to jump up first, you could use a negative third ctor param.
More precisely:
An animation can never start until after the layout has been measured. One usually shouldn't have to worry about how this works beyond that the algorithm is mostly very good and you can take control of its strategies by setting layout parameters. In short, by the time an animation might be started, we know where you want the view to be on the screen, because you set layout parameters.
Translate animation then takes deltas from that position. So your current animation shouldn't start from the top left, but rather wherever those layout params were evaluated by onMeasure.
Some would say- how annoying. It's gonna get complicated even if you just want to do some simple up-down type animations... Well, here's an advisable development strategy; it snould make android animation development a breeeze. Set an animationListener on every animation. In onAnimationEnd, in possibly a parametized way, reset the layout parameters on the view your animating to where you expect it to be. That way, you'll get no surprising "jumps" when you re-apply an animation again. You may need to invalidate in some circumstances, or clearAnimation. The reason that this works is that the measure pass will be caused to come round again and you'll have a new offset for your TranslateAnimation. Finally, you may want to do all this resetting posted to the message queue of a view using post(Runnable runnable) in the listener, so you're off the last pass of the animation draw.
I too found android Animations can occasionally surprise you and cause jumpy behaviour. But if you do it like this, constructors taking delta params shouldn't be confusing again.