I am trying make an animation of an imageView from left to right and get it back from right to left but I also want to get the position of the imageview while its animation from the following code. I want to use the coordinates from origin to display in my app. Please tell me how to do this. Following is the code I am using to animate and move position of imageview.
Animation anx = new TranslateAnimation(0,100,0, 0);
anx.setDuration(2000);
anx.setFillAfter(true);
myImage.startAnimation(anx);
Is there a function like getX() and getY() to get position.
Have u tried to set animation listener and starting a runnable when the animation starts to check the image getx , gety every milliseconds u want and stop it after the animation ends
Related
i have a drawable frame animation and i also implement tween animation on it. In drawable animation my sprites it walking without moving its location. and through tween animation it is moving from x 1% to x 100%.
the code of both animation is as
Animation movement = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animationball);
anim.startAnimation(movement);
anim.setBackgroundResource(R.drawable.smurf);
AnimationDrawable walking = (AnimationDrawable) anim.getBackground();
walking.start();
it is working fine and my animation gives illusion that m sprite walking from left to right.
now i wanted to get X and Y location of this animated sprite at every pixel of its movement path.
i implements runnable on same activity class and the code of its run() method is as below
#SuppressLint("NewApi") #Override
public void run() {
tv.setText(""+ anim.getX());
}
then i start the thread in onCreate method as
Thread t = new Thread(this);
t.start();
in run() method tv is a TextView and i wanted to observe the locations through it. but it always showing 0.0
i am unable to understand it why it is not showing the x locations ?
With tween animation or AnimationDrawble and View Animation (Animation class), the animated view's "real" position is not changed even though the view is animated to a different location, for example, you can create a button and animate it to the right side of the screen, but when you click along on the button when it is moving, it will not get depressed, but if you press its initial position (even if the button is not there), the button will get depressed. If you want to also invalidate the view's position, use the new Property Animation, click HERE for more detail.
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.
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.