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.
Related
I am trying to build an app that has an imageview in it. The user can drag the imageview horizontally to some position on screen using below code:
#Override
public boolean onTouchEvent(MotionEvent event){
img.getLocationOnScreen(viewCoords);
newimgpos[0]=event.getX()-(viewCoords[0]-img.getX());
img.setX(newimgpos[0]);
}
Now, I want that when user lifts up his finger off the screen, the imageview be moved back to x=0. I can directly set the x coordinate of the imageview to be zero but it looks like a very abrupt change; I want the imageview to move smoothly to x =0. Can this be done? I tried using below logic:
for(int i= (int)newimgpos[0]; i>=0; i--){
img.setX(i);
}
It moves and I can see the actual movement; still it is not smooth enough.
You need to use Animation here. Check the following thread for more info: Android translate animation - permanently move View to new position using AnimationListener
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
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 have a view in my app which I would like to animate while moving a finger on a surface view.
I don't want the view just to follow my finger but I would like to move it from its position to the new one with an animation.
For obtaining that result I intercept the movement of the finger on the surface view and start a new ObjectAnimator on the view to be animated, saying it to move to its new position. The problem is that, having all these ObjectAnimators which start on the same object in a so short time, the result is that I see the animation only when I stop to move the finger (seeing it moving to its final position).
How can I solve this problem?
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.