Here's my animation xml code
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2500"
android:startOffset="0"
android:repeatCount="1"
/>
Here's my kotlin code
private fun animateSidebarOpener() {
val rotate = AnimationUtils.loadAnimation(this, R.anim.sidebar_button_animation)
binding?.btnOpenSidebar?.animation = rotate
}
Thing is that for the animation to work, I need to start another activity and close it for the animation to work, how to make it work as soon as I call the animateSideBarOpener function
Related
I'm doing a simple animation of an icon in an ImageView that is inside an ExpandableRecyclerAdapter. When clicking on the expansion item, the animation starts but returns to the initial state, presenting a different behavior. It starts to rotate from 0F to 180F, but at the end it returns from where it was as seen below:
ImageView
I'm using AnimationUtils and two xml files -> one to rotate 180 degrees for the up arrow and -180 for it to return;
My rotation function:
fun rotateAnimation(view: View, expanded: Boolean) {
var anim = AnimationUtils.loadAnimation(view.context, R.anim.rotate_view_up)
if (expanded) {
view.startAnimation(anim)
} else {
anim = AnimationUtils.loadAnimation(view.context, R.anim.rotate_view_down)
view.startAnimation(anim)
}
}
My XML animation files are configured as seen below (arrow up):
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:interpolator="#android:anim/cycle_interpolator"
android:toDegrees="-180" />
Arrow down(ony toDegree change):
<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:interpolator="#android:anim/cycle_interpolator"
android:toDegrees="180" />
Is there any configuration I missed in the xml set?
I am trying to do an animation on an imageview, were i need to scale up and scale down the image so that it will have a shadow feel effect for another animation.
I tried adding two xml in anim folder -
scale_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false">
<scale
android:duration="1250"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXScale="1.5"
android:toYScale="1.5" />
</set>
scale_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false">
<scale
android:duration="1250"
android:fromXScale="1.5"
android:fromYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
and in my activity class
AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s);
But animation is not happening. What i am doing wrong? Anyone please help. Thanks
AnimationSet performs animation simultanuously, not sequentaly by default. So it does zoom in and zoom out at the same time. That's why you see no effect. But you can set a start offset to the animation:
AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context, R.anim.scale_up);
scaleDownAnimation.setStartOffset(1250); //this line
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s
I have this simple arrow image rotation animation which works as intended only for the first time. from second time onward It's still do the rotation but without slow animation.
Here's the code in anim xml files
Rotate 180
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fromDegrees="0"
android:toDegrees="180"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:fillAfter="true"
android:fillEnabled="true"/>
Rotate Revere
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fromDegrees="180"
android:toDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:fillAfter="true"
android:fillEnabled="true"
/>
The image view inside card view.
<ImageView
android:id="#+id/creadit_card_next_image"
android:layout_width="#dimen/next_image_size"
android:layout_height="#dimen/next_image_size"
android:layout_marginEnd="#dimen/static_menu_primary_margin"
android:layout_marginTop="16dp"
android:rotation="-90"
android:src="#drawable/ic_navigate_next"
android:tint="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Java code to trigger Animation.
private Animation rotatePlus180;
private Animation rotateMinus180;
private boolean creditDebitCardViewExpanded = true;
rotatePlus180 = AnimationUtils.loadAnimation(this, R.anim.rotate_plus_180);
rotateMinus180 = AnimationUtils.loadAnimation(this, R.anim.rotate_minus_180);
private void onClickCreditDebitCardView() {
creditDebitCardPaymentMethod.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (creditDebitCardViewExpanded) {
expandAnimation(paymentRecyclerView);
creditDebitCardViewExpanded = false;
creditCardNextImage.setAnimation(rotatePlus180);
} else {
collapseAnimation(paymentRecyclerView);
creditDebitCardViewExpanded = true;
creditCardNextImage.setAnimation(rotateMinus180);
CreditDebitLayoutContainer.setPadding(0, 0, 0, padding);
}
}
});
}
Instead of setAnimation use startAnimation
creditCardNextImage.startAnimation(rotatePlus180);
creditCardNextImage.startAnimation(rotateMinus180);
setAnimation seems to be called once you attach the animation to the view/ or when the view is added.
StartAnimation will be called all the time even if the view has already been added.
I have an animation set on an ImageButton that is Invisible, then on a callback gets set to be visible and should start to animate.
I set my animation up like this
showBuildingTapAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.main_button_tap);
and call it like this
if(showBuildingCollectionNavButton.getVisibility() == View.INVISIBLE) {
showBuildingCollectionNavButton.setVisibility(View.VISIBLE);
showBuildingCollectionNavButton.requestLayout();
showBuildingTapAnimation.start();
}
and I have also tried this:
if(showBuildingCollectionNavButton.getVisibility() == View.INVISIBLE) {
showBuildingCollectionNavButton.setVisibility(View.VISIBLE);
showBuildingCollectionNavButton.requestLayout();
showBuildingCollectionNavButton.startAnimation(AnimationUtils.loadAnimation(this.getApplicationContext(), R.anim.main_button_tap));
}
and this
if(showBuildingCollectionNavButton.getVisibility() == View.INVISIBLE) {
showBuildingCollectionNavButton.setVisibility(View.VISIBLE);
showBuildingCollectionNavButton.requestLayout();
showBuildingCollectionNavButton.startAnimation(showBuildingTapAnimation);
}
My animation itself is here:
<scale
android:duration="075"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.09"
android:toYScale="1.09"
android:pivotX="50%"
android:pivotY="50%"
/>
<scale
android:duration="075"
android:startOffset="075"
android:fromXScale="1.09"
android:fromYScale="1.09"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
/>
I am calling requestLayout() as a few of the posts here talked about issues when the asset was gone or invisible that it wasn't updating...
I think the real issue may be with the context that I am passing into the loadAnimation() call - as I am in a FragmentActivity here...
Thoughts ??
You are displaying your animations for 75 + 75 ms . They are animated but you can not observe them (especially a scale that small)
i try to rotate imageview with animation.i wrote some code.this is a my xml animation code
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" />
and this is a java code
final ImageView myImage = (ImageView)findViewById(R.id.rot);
final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);
myImage.setAnimation(myRotation);
Handler ha=new Handler();
ha.postDelayed(new Runnable() {
#Override
public void run() {
myRotation.setDuration(1000);
}
}, 1000);
myRotation.cancel();
i try cancel animation when Duration is over.but i can rotate imageview but i can't cancel aniamtion
how i can solve my problem ? if anyone knows solution please help me
You don't need to use handler for animation.
Just use this:
animation xml:
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:duration="1000"/>
In activity:
myImage.startAnimation(myRotation); //this will stop animation after 1000 miliseconds as `repeatCount` is 1.
Hope it helps.
Try these:
myImage.startAnimation(myRotation); // to start animation
myImage.cancelAnimation(); // to cancel animation