Animate Views form one point to another point Android programatically - android

Im trying to animate two Buttons from a point (1st button). On Click of the first button, that button is set to INVISIBLE/GONE and two other button are shown. I am trying to pass the X and Y values of the View(1st button) as follows:
button.getX();
button.getY();
but im not getting the exact center (of 1st button).
Im trying to send those X and Y values to the animation but both the buttons (button2, button3) are coming side by side.. I want them to overlap as the point where they start animation is same. Im using the following code for the animation:
AnimatorSet animations = new AnimatorSet();
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue);
xAnim.setDuration(3000);
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue);
yAnim.setDuration(3000);
Animator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", finalAlphaValue);
alphaAnim.setDuration(alphaDuration);
//Play all the animations together
animations.play(xAnim).with(yAnim).with(alphaAnim);
I want the animation to be like this

#Deev Thanks thats really helped but the code im using has no start position
TranslateAnimation tanim_stay = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", 0) - btn_armed_stay.getWidth(),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getX()+ (btn_armed_stay.getWidth()/2),
TranslateAnimation.ABSOLUTE, -getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getY()+ (btn_armed_stay.getHeight()/2));
Im using this code for the start and end position of the animation but how to add another animation to the current Translate animation.. I want to add Alpha animation to this Translate animation..

Related

After translating a view, the area where you press to trigger a button press doesn't move. How do you move this area with the view?

I created a button and wanted it to move to the bottom of the screen after some event is triggered. So I made an TranslateAnimation object
private TranslateAnimation setupAnimation(float yOffset) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, yOffset);
animation.setDuration(1000);
animation.setFillAfter(true);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
return animation;
}
Then I pass in the TranslateAnimation object into the startAnimation() method of the view I wanted to move.
Well that works for what I want to accomplish visually, but I noticed that I can't click on where it is visibly, but I can press where the button used to be and the onClick callback will be executed.
What do I need to do, post translation, to allow the user to press the button at its new location?
A TranslateAnimation only moves the pixels on the screen, it does not change the actual position of your Button, it just looks like it is moving, so your OnClick/OnTouchListener will not animate with it.
Use ObjectAnimator or ViewPropertyAnimator to really change the property of your Button.
Here is an example using ViewPropertyAnimator to get you started :
yourButton.animate()
.translationY(yOffset)
.setInterpolator(new AccelerateDecelerateInterpolator())
.setDuration(1000);
check the Docs for other available methods.

Android Animations on button click to display other buttons with fade-in and translate animation programmatically

I want to display a buttons with animations like fade-in along with the translate animation programmatically. I have to start translate animation from a specific position on the screen. So i need to pass the x and y values for the translate animation to start from.
Im using the following code for translate animation but how to add fade-in animation to the existing translate animation?
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", maxX),
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, 100);
tanim.setDuration(3000);
I've used Animations several times before and they always left me scratching my head. Some things I would recommend you should look into are Object Animators and Animator Sets. They're really useful, as they allow you to animate any property of the given view. As long as the view has a setter method for that property, you can add it to the animator set by passing in the string name of the property to the Animator constructor. The AnimatorSet class also allows you to easily choreograph multiple animations.
In your case, you could do something roughly like the following:
AnimatorSet animations = new AnimatorSet();
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue);
xAnim.setDuration(3000);
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue);
yAnim.setDuration(3000);
Animator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", finalAlphaValue);
alphaAnim.setDuration(alphaDuration);
//Play all the animations together
animations.play(xAnim).with(yAnim).with(alphaAnim);
You can string together as many animators as you want this way, and you can also set animators to be played in a sequence using the .before() and .after() methods rather than the .with() method.

ImageView not reseting to original location after animation

I have a swipe recognizer that calls this function when a swipe is detected. The alpha animation works great, but after the animation, the image is not resetting to its original location in the center.
Currently, the image stays on the left.... I've searched all over and it seems that I'm implementing the animation correctly.
I'm using a RelativeLayout and swipeImageView is an empty imageview in the middle of my layout.
swipeImageView.bringToFront();
swipeImageView.setImageDrawable(getResources().getDrawable(R.drawable.skip4x));
ObjectAnimator nextGroupLeft = ObjectAnimator.ofFloat(swipeImageView, "x", -250f);
nextGroupLeft.setDuration(1000);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(swipeImageView, "alpha", 1f, 0f);
fadeAnim.setDuration(1000);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(nextGroupLeft, fadeAnim);
animSetXY.start();
Trying clearing the animations. When this line is executed, the animations should stop and the ImageView should return to its original state.
swipeImageView.clearAnimation();

How to combine scale and transform animation in android?

I need to translate and scale image view like on this picture from state 1 to state 2,
State 1 is center of screen, state 2 is any location on screen;
I have combined to animations
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX*5,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32)*5);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
Where "as" is AnimationSet. When I start the animation.
Everything works well, start and end position ok, but translation between them not going along "straight" line. It goes like an arc, on picture 2;
What kind of scale-animation and translate-animation need I perform and in which order to get straight transformation?
picture 1 pic1
picture 2 pic2
After changing order of animations and removing mult by 5 in translateanimation all works ok
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32));
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);

How to rotate a drawable by ObjectAnimator?

Alphaing a drawable work well like this:
if(mAlphaAnimation == null){
mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION);
mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power);
mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE);
mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE);
mAlphaAnimation.addUpdateListener(this);
}
But if I want rotate a drawable like below , it don's work.
private void createRotateAnim(float fromDegress,float toDegress,int duration){
if(mRotateAnimation == null){
mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration);
mRotateAnimation.setStartDelay(100);
mRotateAnimation.setInterpolator(new AccelerateInterpolator());
mRotateAnimation.addUpdateListener(this);
}
}
Anyone can help me to fix this issue, or these is any other way to create a rotation drawable animation .
I am sorry to my poor English.
Try with ObjectAnimator instead.
ImageView imageview = (ImageView)findViewById(R.id.image);
ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
"rotation", 0f, 360f);
imageViewObjectAnimator.setDuration(1000); // miliseconds
imageViewObjectAnimator.start();
EDIT
Since this question draw some attention let me to explain why to use ObjectAnimator instead of other Transition animators
The thing about using ObjectAnimator is that it's moving both the visible and the clickable area of the item, if you use another animation method, for example Transition Animation or some other Animators, and let's say if you want to move the Button from the bottom left of the screen to the top left, it will only move the visible area but not the Button itself, the clickable area will still be on the previous position, in this case the clickable area will still be on the bottom left instead of the top left where you moved the button.
If you do the same with ObjectAnimator, both the visible area, and the clickable area will move the the desired location.
Try this simple Rotation Animation applied to a image.
ImageView imageview = (ImageView)findViewById(R.id.myimage);
RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);
imageview.startAnimation(rotate);
This answer is just for a sake of question, it is correct that Clickable area will be different than View's current position. Please check this question for making clickable area correct. Button is not clickable after TranslateAnimation

Categories

Resources