ImageView not reseting to original location after animation - android

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();

Related

Animate Views form one point to another point Android programatically

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..

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.

Android: Shrink animation using animator

I am animating in the following way,
There are 2 layouts (both are relative layouts), layout1, which will be hidden initially, layout2 will be shown to the user initially.
once the animation starts layout1, will be made visible and layout2 will be scaled using the animator as follows,
Animator scalexAnimator = ObjectAnimator.ofFloat(layout2, "scaleX", 1f, 0f);
Animator scaleyAnimator = ObjectAnimator.ofFloat(layout2, "scaleY", 1f, 0f);
Animator pivotxAnimator = ObjectAnimator.ofFloat(layout2, "pivotX", <maxWidth>);
Animator pivotyAnimator = ObjectAnimator.ofFloat(layout2, "pivotY", <maxheight/2>);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(pivotxAnimator, pivotyAnimator, scalexAnimator, scaleyAnimator);
animatorSet.start();
This starts the animation and the layout2 gets shrinked,but the aspect ratio is never maintained, what I mean to say is the animation follows these steps,
Firstly the height reduces.
Next the width gets reduced.
Height reduces again.
Width again.
this goes on and it will stop once the final given ratio(0.5) is achieved.
This does not give shrinking effect, I tried with animation class as follows,
Animation shrinkAnimation = new ScaleAnimation(1,0.5f,1,05f,Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF, 0.5f);
layout2.startAnimation(shrinkAnimation);
this will work perfectly and I will get a exact shrinking effect, aspect ratio is maintained, but the problem I face in the above implementation is the position of the layout2 is changed, but the button retain their position (layout2 has 4 buttons and button click works on the old button positions, even though the user can't see any buttons there, I read in some posts that this is expected), so I had to disable the buttons manually, this is a overhead so I wanted to avoid using Animation class, and went for ObjectAnimator.
So is there a way in ObjectAnimator to get the same shrinking effect?

How to keep the process in a simple animation with some time duration?

I want to change the imageview (x,y) values by using the setX(),setY(). It is directly going like an visible/gone. I just wants to keep this in a small animation so that it will go slowly to the given cordinates.
Is there any basic animation for performing this.
You should use ObjectAnimator and AnimatorSet to achieve this.
Use Following code:
ObjectAnimator oaX = ObjectAnimator.ofFloat(view, "translationX", 0, xValue);
oaX.setDuration(1000);
ObjectAnimator oaY = ObjectAnimator.ofFloat(view, "translationY", 0, yValue);
oaY.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(oaX, oaY);
animatorSet.start();
pass your coordinate values to xValue and yValue, these are relative to your image's actual coordinates.
The easiest way to move your imageview is here:
imageview.animate().translationX(imageview.getWidth()).setDuration(1000).setInterpolator(new AccelerateDecelerateInterpolator());

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