如何为特定视图添加动画?求Activity 1转Activity 2时Bloc滑动动画方案
Hey there! Let me break down how to get that cool slide animation where Bloc A slides up from Activity 1 and Bloc B slides down into Activity 2 when you navigate between them. I’ve got two solid approaches for you—one quick and simple, and another more flexible for targeting specific UI blocks:
overridePendingTransition This method works well if you want the animation to apply to the entire Activity window (or if your Bloc A/B are the main content of each Activity).
Step 1: Create Animation XML Files
First, define the slide animations in your res/anim folder:
Slide-up exit for Bloc A (Activity 1)
res/anim/slide_up_exit.xml<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromYDelta="0%" android:toYDelta="-100%" android:interpolator="@android:anim/decelerate_interpolator"/>Slide-down enter for Bloc B (Activity 2)
res/anim/slide_down_enter.xml<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromYDelta="-100%" android:toYDelta="0%" android:interpolator="@android:anim/decelerate_interpolator"/>
Step 2: Trigger the Animation on Navigation
In Activity 1, right after starting Activity 2, call overridePendingTransition:
Intent intent = new Intent(Activity1.this, Activity2.class); startActivity(intent); // First param: Activity 2's enter animation; Second param: Activity 1's exit animation overridePendingTransition(R.anim.slide_down_enter, R.anim.slide_up_exit);
If you need to target only Bloc A/B (not the entire Activity), use the Activity Transition API (API 21+). This lets you animate specific views independently.
Step 1: Assign Transition Names to Blocs
Add a unique transitionName to both Bloc A (Activity1) and Bloc B (Activity2) in their layouts:
Activity1's Bloc A:
<LinearLayout android:id="@+id/bloc_a" android:transitionName="bloc_slide_transition" <!-- Your layout attributes --> > <!-- Bloc A content --> </LinearLayout>
Activity2's Bloc B:
<LinearLayout android:id="@+id/bloc_b" android:transitionName="bloc_slide_transition" <!-- Your layout attributes --> > <!-- Bloc B content --> </LinearLayout>
Step 2: Create a Custom Transition Class
This class defines the slide behavior for each bloc:
public class SlideUpDownTransition extends Transition { @Override public void captureStartValues(TransitionValues transitionValues) { // Capture the view's initial Y position transitionValues.values.put("initial_y", transitionValues.view.getY()); } @Override public void captureEndValues(TransitionValues transitionValues) { View view = transitionValues.view; if (view.getId() == R.id.bloc_a) { // Bloc A slides up out of the screen transitionValues.values.put("target_y", -view.getHeight()); } else if (view.getId() == R.id.bloc_b) { // Bloc B slides down into its final position transitionValues.values.put("target_y", 0f); } } @Override public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) { if (startValues == null || endValues == null) return null; Float startY = (Float) startValues.values.get("initial_y"); Float endY = (Float) endValues.values.get("target_y"); View targetView = endValues.view; ObjectAnimator slideAnimator = ObjectAnimator.ofFloat(targetView, "y", startY, endY); slideAnimator.setDuration(500); slideAnimator.setInterpolator(new DecelerateInterpolator()); return slideAnimator; } }
Step 3: Enable Transitions and Start Activity
In Activity1, configure the transitions and launch Activity2 with scene animation:
// Enable content transitions for the window getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); // Set exit animation for Bloc A and enter animation for Bloc B getWindow().setExitTransition(new SlideUpDownTransition()); getWindow().setEnterTransition(new SlideUpDownTransition()); // Launch Activity2 with shared element transition Intent intent = new Intent(Activity1.this, Activity2.class); View blocA = findViewById(R.id.bloc_a); ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, blocA, "bloc_slide_transition"); startActivity(intent, options.toBundle());
Step 4: Enable Transitions in Your Theme
Add this to your styles.xml to ensure transitions work:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Other theme attributes --> <item name="android:windowContentTransitions">true</item> </style>
- Compatibility: For API <21, combine
overridePendingTransitionwith view animations triggered inonPause()(Activity1) andonCreate()(Activity2). - Tweak the Animation: Adjust the
durationor use interpolators likeAccelerateDecelerateInterpolatorfor smoother motion. - No Shared Element?: Skip the transitionName and shared element code—just set the enter/exit transitions on the window to animate the blocs independently.
内容的提问来源于stack exchange,提问作者Mxwan

