Snake is a popular slide-off library. Its configuration is simple, non-invasive, can easily achieve imitation iOS slide back effect…

The latest version

The module snake snake-compiler snake-annotations
The latest version

The latest version updates the content

  • Remove the host interface to simplify Activity configuration
  • Fix the occasional slippage problem
  • Added obfuscation configuration description

Method of use

1) Add dependencies

Compile = compile compile = compile compile = compile compile'com.youngfeng.android:snake:x.x.x'
    annotationProcessor 'com.youngfeng.android:snake-compiler:x.x.x'
}
Copy the code

Note: if using Kotlin, change the annotationProcessor to kapt

2) Initialize Snake in Application

public class SnakeApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        
        // 对Snake进行初始化
        Snake.init(this);
    }
}
Copy the code

Activity Integration Steps

Add the @enableDragtoClose annotation to the Activity class that you want to enable to slide back

@EnableDragToClose()
public class FirstActivity extends Activity
Copy the code

Fragment integration Steps

Method 1: Dynamic configuration

  1. Add the @enableDragtoClose annotation to the Fragment class that needs to be enabled to slide back
@EnableDragToClose()
public class FirstFragment extends Fragment {
Copy the code
  1. When jumping to the current Fragment, if your Fragment class inherits from Android.app. Fragment, use Snake. NewProxy (xx.class) to create an instance of your Fragment.

And if your fragments class inherits from android. Support. The v4. App. Fragments, use the Snake. NewProxySupport instance to create fragments (xx) class).

  1. In fragments, there may not be a default constructor. Or more than one constructor is used, in which case you can specify the PrimaryConstructor using PrimaryConstructor.
@EnableDragToClose()
public class FirstFragment extends Fragment {
    
    @PrimaryConstructor
    public FirstFragment(int x, int y) {
        
    }
    
    ...
}
Copy the code

In the case of the main constructor, the instance created using the Snake. NewProxy interface requires the construction parameter to be passed in. Using the above code snippet as an example, we can use it like this:

FirstFragment fragment = Snake.newProxy(FirstFragment.class, 1, 2);
Copy the code

The same is true for Snake. NewProxySupport

Method two: Use inheritance

Change your Fragment parent class to complete the slide-off integration as follows:

  • android.app.Fragment= >com.youngfeng.snake.app.Fragment
  • android.support.v4.app.Fragment= >com.youngfeng.snake.support.v4.app.Fragment

The differences between the two integration solutions

Integration solutions newProxy/newProxySupport Using inheritance
invasive There is no Changed the top-level parent class
How easy is it More complicated simple
Animation processing You have to deal with it yourself No need to deal with
Instance creation NewProxy /newProxySupport must be used You can handle it yourself

Note: In the case of integration using inheritance, the original API is completely generic. You can choose to swipe using Snake’s API, or you can swipe using methods from the parent class, it’s up to you. Even instance creation can still be handed over to newProxy/newProxySupport.

A word of advice: If your project has consistent programming specifications and clean code, I recommend using inheritance for integration. If your project is relatively messy and the overall performance is inconsistent, I recommend you to use newProxy/newProxySupport for more flexibility.

Sliding parameter configuration

Usually, after completing the above steps, you should be able to use the slide off function normally. However, some students may want to customize the sliding style. Don’t worry, Snake provides two ways to configure slide parameters.

  • Global slide configuration If you want to apply slide configuration to all pages, you can use the snake. XML file to configure the parameters. In the root directory of the project, I provide the configuration template
<? xml version="1.0" encoding="utf-8"? > <snake> <config> <! - set totrueThe root Activity can also slide to close, which is weird! It is not recommended to change the default value of this variable --> <enable_for_root_activity>false</enable_for_root_activity> <! - set totrue<only_listen_to_fast_swipe> <only_listen_to_fast_swipe>false</only_listen_to_fast_swipe> <! - The minimum detection speed of quick sliding is not recommended. <min_velocity>2000</min_velocity> <! - set totrueThe left edge shadow will be hidden when sliding, and the default value of this variable is not recommended to change --> <hide_shadow_of_edge>false</hide_shadow_of_edge> <! Shadow edge gradient start color --> <shadow_start_color>#00000000</shadow_start_color><! -- Shadow edge gradient end color --> <shadow_end_color>#50000000</shadow_end_color><! -- Like the iPhone X, quickly slide up from the bottom edge to the desktop (experimental, off by default) --> < enable_swipe_up_to_HOME >false</enable_swipe_up_to_home> <! - Whether to allow page linkage. The default value istrue -->
        <allow_page_linkage>true</allow_page_linkage>
    </config>
</snake>
Copy the code

Modify the template parameters, copy the current XML file, and place it under assets in the main project directory. The name must still be snake. XML.

  • If you only want to apply the slide parameter to a single page, you can use ** @setDragParameter ** to configure it:
@EnableDragToClose()
@SetDragParameter(minVelocity = 2000, hideShadowOfEdge = false...). public class FirstActivity extends ActivityCopy the code

Other Interfaces

Snake. EnableDragToClose () : This interface can be used if you want to dynamically turn the Slide Off feature on or off

Snake. AddDragListener () : If you want to do some extra processing during a slide, you can use this interface to listen for the entire slide.

Snake. SetCustomTouchInterceptor: if you are in use process, there have been some sliding conflict problem, you can use this interface through the custom interceptors. Note: You don’t need this interface in most cases, but you can use this interface if you need to resolve sliding conflicts.

Snake. DragToCloseEnabled () : Use this interface if you need to know if the slide close function is enabled on the current page.

Snake. EnableSwipeToHome (): This interface can be used if you want to slide out of the desktop on a page

Snake. SwipeUpToHomeEnabled () : smooth exit to the desktop function gets the current page open

Animation processing

Now that you have successfully integrated the slide-off feature, you know how to configure the slide-off parameters. However, you’ll notice that if you use the system back key, the animation of the Activity doesn’t behave as if you were sliding to close. Also, the Fragment seems to play the animation again after sliding off, which looks weird.

To this end, Snake provides several different animation implementations to work with slide-off animations to make them look exactly the same.

  • R.anim.snake_slide_in_left.xml: Enter from the left
  • R.anim.snake_slide_in_right.xml: Enter from the right
  • R.anim.snake_slide_out_left.xml: Exit from the left
  • R.anim.snake_slide_out_right.xml: Exits from the right
  • R.animator.snake_slide_in_left.xml: Enter from the left
  • R.animator.snake_slide_in_right.xml: Enter from the right
  • R.animator.snake_slide_out_left.xml: Exit from the left
  • R.animator.snake_slide_out_right.xml: Exits from the right

For details, see Demo configuration.

Using these animation configurations during Activity startup and shutdown basically solves the problem of inconsistent animation styles when sliding closed. However, the Fragment animation playback problem still exists. To make the Fragment behave consistently, you need to do one more thing:

  • rewriteonCreateAnimationoronCreateAnimatorinterface
  • implementationSnakeAnimationControllerinterface

It is recommended to do this in the Fragment base parent class. The result is as follows:

public class BaseFragment extends Fragment implements SnakeAnimationController {
    private boolean mDisableAnimation;

    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
        return Snake.wrap(super.onCreateAnimator(transit, enter, nextAnim), this);
    }

    @Override
    public void disableAnimation(boolean disable) {
        mDisableAnimation = disable;
    }

    @Override
    public boolean animationDisabled() {
        returnmDisableAnimation; }}Copy the code

Note: in the rewriteonCreateAnimatororonCreateAnimationInterface, please useSnake.wrapThe interface wraps the parent implementation. Otherwise, the Settings will be invalid. For details, see Demo

The resources

Github:github.com/yuanhoujun/…

QQ exchange group: 288177681