Android realizes wechat sideslip closing page effect

Recently, after the mobile phone upgraded the 5.0 System, I suddenly found that wechat had the effect of closing the current page like IOS. I wanted to add this effect to my project. Based on the principle of not making wheels repeatedly, I have been on Baidu for a long time and found that most people use custom views to implement them. However, for projects that have been basically completed, it is undoubtedly a terrible nightmare if all activities reuse custom views.

Therefore, I have implemented another method that can be implemented without customizing the view. As long as its subclass inherits from it, it can have its side sliding function. In a word, this method is only valid for mobile phones above 5.0 (wechat can only be used on 5.0 anyway). Please ignore those below 5.0!!!

principle

There is an underlying view in each activity, that is, the so-called rootview. When we load an XML layout, the system will automatically generate the rootview for you. Because it is a view, it means that you can move the root layout freely through certain code. As shown in the following code, only a few lines of code can move the layout.

This is our effect

Activity linkage

There is still a big gap. The most obvious thing is that when we move, the activity on the upper layer does not follow the linkage.

The method to solve this problem is also simple. As shown in the figure, whenever an activity is started, the system will put the activity into a stack. According to the working principle of the stack, the activities in app cover one layer, as shown in the figure above. Therefore, whenever an activity is started, the current activity can be stored in a list, so that we can take out the previous activity from the current activity for operation.

Therefore, when entering a new activity, load the current activity into the list in its oncreate method. When exiting, remove the current activity from the list in the finish overloaded method.

be careful!!! When sliding, we must consider that there may be sliding controls like listview in the activity. Therefore, we must control the distribution of events.

The code is as follows

This is the effect picture after linkage

It's a little effective now, but there is still a big gap with wechat. Next, we need to start dealing with automatic sliding

Automatic sliding

There is no need to say more about this. This mainly uses attribute animation to move

The following is the complete code

This is our final rendering

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>