Android TranslateAnimation move view to another view - android

I'd like to move my view from first layout to another layout, I use TranslateAnimation but it move my view to wrong place.
How I can get view's coordinates and move my view to them?
code:
private void moveViewToNextView(View view,View v2) {
int originalPos[] = new int[2];
v2.getLocationOnScreen(originalPos);
TranslateAnimation anim = new TranslateAnimation(0, originalPos[0], 0, originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter(true);
view.startAnimation(anim);
}

First of all use Object animator instead of TranslateAnimation , Object animator is only thing that moves object , translateanimation doesnt change its position on screen.
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(view, propertyName, fromX, toX);
transAnimation.setDuration(3000);
transAnimation.start();

Related

Use one animation object for multi views

I want set alpha animation to views but with one object without create many object like this fadeIn1 , fadeIn2 , fadeIn3 etc
View1 , View2 , View3
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setStartOffset(1000);
fadeIn.setDuration(1000);
view1.startAnimation(fadeIn); //I want show this after 1 second
fadeIn.setStartOffset(1500);
view2.startAnimation(fadeIn); //I want show this after 1.5 second
fadeIn.setStartOffset(2000);
view3.startAnimation(fadeIn); //I want show this after 2 second
But all views shows after 2 second together , Why ?
Use AnimatiorSet for this
Note: The below code was in kotlin.
First create an function which returns an animator by taking view
private fun getAnimation(view: View): Animator {
return ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
duration = 1000
}
}
Then after user AnimatorSet.playSequentially to play one after other.
AnimatorSet().apply {
playSequentially(listOf(getAnimation(btn_1),getAnimation(btn_2),getAnimation(btn_3)))
interpolator = AccelerateInterpolator()
start()
}
Please free to ask any query in comment section, I'll try to answer as soon as possible :)
You need AnimationSet ex:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
view.startAnimation(animation);

Touch targets do not move with object when using translate animation?

Whenever I use TranslateAnimation to move an object to a new location, for some reason, the touch target of that object remains in the old position.
How do I change this behaviour?
eg.
public static void hideViewUp (View v, int duration) {
AnimationSet animSet = new AnimationSet(true);
animSet.setFillAfter(true);
animSet.setDuration(duration);
AlphaAnimation alp = new AlphaAnimation(1.0f, 0);
TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,
-(v.getTop() + v.getHeight()));
animSet.addAnimation(translate);
animSet.addAnimation(alp);
v.startAnimation(animSet);
}
There's no need to post any sample code - the issue is indeed pretty well known and general.
TranslateAnimation and all other old types of animation change only the draw transformation matrix. These animations shouldn't be used to move items permamently.
To truly move a view to a new position, use ObjectAnimator.
Here's an example: http://www.android-app-market.com/animations-in-android-translate-animation-alpha-animation-object-animation.html

how to move a button to left or right programmatically in android

I want to animate a button by getting its co-ordinates and then increasing or decreasing them one by one so that the button can go to left and then come to the right.
Use a TranslateAnimation:
TranslateAnimation animation = new TranslateAnimation(start_x, start_y, end_x, end_y);
animation.setDuration(1000); // duartion in ms
animation.setFillAfter(false);
button.startAnimation(animation);
I'm not sure how you can get it's position, button.getTop() and button.getLeft() could work...
Not sure if this will help you but i was struck with the same problem i was able to do this using these methods, setTranslationX(float) setTranslationY(float)
you can use it Like this
Button button = (button) findViewById(your id);
button.setTranslationX(a float value);
here's the android documentation that provide more information http://developer.android.com/reference/android/view/View.html#attr_android:translationX
Solution for those who are looking for left to right animation)
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(1500); // animation duration
animation.setRepeatCount(1); // animation repeat count
animation.setFillAfter(false);
your_view .startAnimation(animation);//your_view for mine is imageView
Solution for those who are looking for repeated animation(for eg. left to right and right to left)
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(1500); // animation duration
animation.setRepeatCount(4); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left)
animation.setFillAfter(true);
your_view .startAnimation(animation);//your_view for mine is imageView
When button clicked image moves from left to right
you can use this code for fragments also.
insert this code in onCreateView for fragments or directly use button listener.
View view = inflater.inflate(R.layout.fragment_move,container,false);
mBtnMove = view.findViewById(R.id.btn_move);
mImageMove = view.findViewById(R.id.imv_move);
mBtnMove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float start_x_axis = 0; // initialize the axis value start from and end
float start_y_axis = 1000;
float end_x_axis = 0;
float end_y_axis = 0;
TranslateAnimation animation = new TranslateAnimation(start_x_axis, start_y_axis, end_x_axis, end_y_axis);
animation.setDuration(2500); // Duration of image to move from left to right
mImageMove.startAnimation(animation);
}
});
return view;

Android ScaleAnimation and TranslateAnimation, how to avoid ScaleAnimation movement

I have an AnimationSet with inside a ScaleAnimation and a TranslateAnimation like this:
TranslateAnimation:
TranslateAnimation goTopFromRight =
new TranslateAnimation(0, -(right.getLeft()-top.getLeft()),
0,-(right.getTop()-top.getTop()));
ScaleAnimation:
ScaleAnimation = setSizeForTop = new ScaleAnimation(1, 2, 1, 2);
and AnimationSet:
bringToLeftFromTopAnimationSet = new AnimationSet(true);
bringToTopFromRightAnimationSet.addAnimation(goTopFromRight);
bringToTopFromRightAnimationSet.addAnimation(setSizeForTop);
The problem is that when i try to use only the ScaleAnimation, my item goes to the position i want, but whe I am using the ScaleAnimation with the TranslateAnimation in the AnimationSet, my item translates more than i need, as if ScaleAnimation introduces some supplementary movements abd I don't know how to delete them.
Thank you for your help.
The correct solution is to change order of animations. Scale must go first:
bringToTopFromRightAnimationSet.addAnimation(setSizeForTop);
bringToTopFromRightAnimationSet.addAnimation(goTopFromRight);
To avoid the ScaleAnimation movement when using the ScaleAnimation and TranslateAnimation, you have to multiply the TranslateAnimation parametres with those from ScaleAnimation like this:
TranslateAnimation goRightFromTop;
ScaleAnimation sizeNotUp;
goRightFromTop = new TranslateAnimation(0,(-( invisibleCenterImageView.getLeft() - imageViewRight.getLeft() ))*(1/0.6667f), 0, (-( invisibleCenterImageView.getTop() - imageViewRight.getTop()))*(1/0.6667f));
sizeNotUp = new ScaleAnimation(1,0.6667f,1,0.6667f,Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
And then, surprise you deleted the ScaleAnimation movement.
I remember having weird problems with nested animations as well.
Have you tried setting the pivot point manually? See public ScaleAnimation (float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) from http://developer.android.com/reference/android/view/animation/ScaleAnimation.html for it.
It could work.
My solution is to add a container outside your view, and apply the scale animation to your view while applying translate animation to the container.
Then you can make more complex animation even via XML~

How can I apply the changes to the position of a view after an animation?

I applyed a TranslateAnimation to an EditText with the FillAfter=true in order to keep its position at the end of the animation.
The animation works properly but the problem is that I can't enter to the edittext anymore.
I think it's due to the fact that the animation affects only the rendering without modifying the actual view coordinates.
Is there any possibility to maintain the final coordinates with the edittext normally working?
Thanks,
Daniele
Unfotunately the animation only renders the raw pixels of the animated element, but not its "android-intern" position. The best solution (that i am able to come up with) is to use an AnimationListener and set the position of the animated element correctly after the animation has finished. Here is my code to slideDown a searchWrapper:
public void toggleSearchWrapper() {
AnimationSet set = new AnimationSet(true);
// slideDown Animation
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f
);
animation.setDuration(300);
animation.setFillEnabled(false);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(final Animation anim) {
};
#Override
public void onAnimationRepeat(final Animation anim) {
};
#Override
public void onAnimationEnd(final Animation anim) {
// clear animation to prevent flicker
searchWrapper.clearAnimation();
// set new "real" position of wrapper
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
searchWrapper.setLayoutParams(lp);
}
});
set.addAnimation(animation);
// set and start animation
searchWrapper.startAnimation(animation);
}

Categories

Resources