How to apply transition to fragments using the new Navigation Graph? - java

I have created an animation using XML for fade in and out and attached it to the fragment using the drop down in navigation drop for enter and exit but the fragments are not being animated.
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/navigation_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.cheapapps.randomquotesmachine.MainFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main">
<action
android:id="#+id/action_mainFragment_to_quoteDetailFragment"
app:destination="#id/quoteDetailFragment"
app:enterAnim="#anim/fade_in"
app:exitAnim="#anim/fade_out" />
</fragment>
<fragment
android:id="#+id/quoteDetailFragment"
android:name="com.cheapapps.randomquotesmachine.QuoteDetailFragment"
android:label="fragment_quote_detail"
tools:layout="#layout/fragment_quote_detail">
<argument
android:name="position"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>

I'm not sure how your fade_in and fade_out files look like so I'll provide you an example below:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
And:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
To apply this fade transition to your fragments, you should set enterAnim and exitAnim as you already did but together with popEnterAnim and popExitAnim as in the following example:
<action android:id="#+id/action_mainFragment_to_quoteDetailFragment"
app:destination="#id/quoteDetailFragment"
app:popEnterAnim="#anim/fade_in"
app:popExitAnim="#anim/fade_out"
app:enterAnim="#anim/fade_in"
app:exitAnim="#anim/fade_out"/>

Related

Start Activity with animation black screen

Hello I'm adding animation left to right and right to left when opening and finishing activities but when I put the overridePendingIntent it shows a black screen...
This is my left_to_right.xml animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
This is the right_to_left.xml animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
I'm starting the Activity like this :
startActivity(Intent(this, SignInActivity::class.java))
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left)
and finishing it like this :
finish()
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right)
But when doing it is showing black screen...
This is my theme
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
And on my Activities I'm using
android:theme="#style/Theme.MaterialComponents.Light.NoActionBar"
What I'm missing?
FIRST ACTIVITY:
I rebuild the situation with a MainActivity that inflates the TargetActivity (second Activity) using a Button:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_in = findViewById(R.id.button_in);
button_in.setOnClickListener(v -> {
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
});
}
}
The activity_main.xml contains the first Button:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<Button
android:id="#+id/button_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:backgroundTint="#2962FF"
android:text="To TargetActivity" />
</RelativeLayout>
SECOND ACTIVITY:
The second Activity is the TargetActivity that finish() with an animation like this:
public class TargetActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
Button button_out = findViewById(R.id.button_out);
button_out.setOnClickListener(v -> {
this.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
});
}
}
And also another Layout with the 2nd Button called activity_target.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFAB00"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TargetActivity">
<Button
android:id="#+id/button_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:textColor="#ffffff"
android:backgroundTint="#2962FF"
android:text="To MainActivity" />
</RelativeLayout>
4 ANIMATIONS:
All you need is 4 animations in your anim folder. 2 for left to right and also vice versa.
1.) slide_in_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
2.) slide_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
3.) slide_in_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
4.) slide_out:right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
Finally don't forget to add the TargetActivity in your AndroidManifest.xml:
<activity android:name=".TargetActivity"/>

Zoom in animation between activities

I am creating an Android application. I want to open the new activity with zoom in transition. I have some buttons in my current activity. When I click one button new activity will open from the button's position. I try to use the following code. But no transition will appear. I am using the following xml and java codes.
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
and zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="800"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="70%"
android:pivotY="70%"
android:toXScale="0.5"
android:toYScale="0.5" >
</scale>
</set>
And in my java code I call it like
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
With these code no effect will appear. Could someone please help me to finds out what is the issue?
Try to add the attribute: android:fillAfter="true"
Like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3" >
</scale>
</set>
place both animation xml files zoom_in.xml and zoom_out.xml in res/anim
directory and start animation like this :
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);

Center view during simple scale animation

I'm trying to do a scale in/out animation for activity/framgent transition
here are the two animations :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<scale
android:fromXScale="80%"
android:fromYScale="80%"
android:toXScale="80%"
android:toYScale="80%" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5" />
</set>
-
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:fromXDelta="100%"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0" />
</set>
Use it like that :
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_to_behind);
The problem is that the activity doesn't scale down in the center of the screen, it stick to the top/left of the screen
Any idea what wm i missing ? Or doing wrong ?
Add a pivot to your scale animation. Add this on your xml:
android:pivotX="50%"
android:pivotY="50%"
Your scale should look like:
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="80%"
android:toYScale="80%" />

Animations appear, rotate and disappear

I want to do a multiple animations on my image (appear -> rotate -> disappear). I've got this code:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="0"
android:toAlpha="100" />
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<alpha
android:duration="1"
android:fromAlpha="100"
android:toAlpha="0" />
</set>
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false" >
<rotate
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
</set>
Also in my java code:
animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
animRotate.setDuration((long) duration);
fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
s.addAnimation(animRotate);
s.addAnimation(fade_out);
image.startAnimation(s);
but unfortunately it doesn't work correctly...
you have severals errors in your animation xml files:
the duration property is in milliseconds, so 1ms is way too short for a noticeable fade in/fade out animation
the alpha property is a float between 0 et 1, 100 is way too much.
you don't need a set in your xml files if there is only one animation : just add the alpha or rotate tag as a root
So, you should have now these files:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0" />
image_rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2500"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="120" />
Then, in your code, you need a add an offset between each animation. Otherwise, all the animations will be triggered at the same time. Moreover, the fillAfter flag must be set on the root animation object (here, your AnimationSet)
Animation animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate);
Animation fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in);
Animation fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out);
AnimationSet s = new AnimationSet(false);
s.addAnimation(fade_in);
animRotate.setDuration((long) duration);
animRotate.setStartOffset(fade_in.getDuration());
s.addAnimation(animRotate);
fade_out.setStartOffset(fade_in.getDuration() + animRotate.getDuration());
s.addAnimation(fade_out);
s.setFillAfter(true);
image.startAnimation(s);

java animation android duration

This animation isn't animating it just instantly disappears no matter what duration I set
here is the java code and xml
Java Code:
Animation shrink =AnimationUtils.loadAnimation(Page.this, R.anim.shrink);
deleteMe.startAnimation(shrink);
XML File:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="true"
android:interpolator="#android:anim/linear_interpolator"
android:duration="10000">
<scale
android:duration="10000"
android:fillAfter="true"
android:fromXScale="1.0"
android:toXScale="0" >
</scale>
<alpha
android:duration="10000"
android:fromAlpha="1.0"
android:toAlpha="0">
</alpha>
</set>
Specify android:interpolator attribute of set tag with the value you desired.
This interpolator just moves the object from the start point to the end point (or rotation) at a steady rate.
for ex::
android:interpolator="#android:anim/linear_interpolator"
In one of my app i write this code to move a block from left to right and its working well
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" >
<rotate android:interpolator="#android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1500"
android:startOffset="1500"/>
<translate android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="3000"/>
<translate android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:toXDelta="100%"
android:duration="1500"
android:startOffset="1500" />
</set>

Categories