Android right slide return example code

Similar to the right sliding return of wechat, the right sliding action is intercepted by dispatchtouchevent() in baseactivity, the animation is realized by settranslationx(), and the view is added in decorview as the left shadow during sliding.

Progressive steps:

Note: the code in the step will be a little different from the final one in order not to be associated with the subsequent steps

Background transparency

Jump animation of activity

According to the needs of the project, rewrite the jump and destroy methods of activities such as startactivity (intent intent), startactivityforresult (intent intent, int requestcode), finish()

Intercept right slide action

All touch events are distributed to the view through activity. Dispatchtouchevent (motionevent EV). When the finger slides 50 ~ 100px right in the x-axis direction, judge whether it is the right sliding action (required by the product manager)

Root layout displacement animation

Set the root layout offset distance according to the finger sliding distance, and judge whether to return with the sliding distance and the speed when the finger is lifted

Add left shadow

The topmost view of the activity is decorview, which is a FrameLayout with only one LinearLayout. The LinearLayout contains a title bar and a custom layout (setcontentview). In the previous step, the LinearLayout is offset by sliding the finger. Now add a view in the decorview, set the background as a shadow, and move with the LinearLayout

Associate lower activity sliding

Get lower level activity

Exit and enter animation of lower level activity

During jump, call the exit and enter animation of the lower activity

When the upper activity slides, it is associated with the lower sliding

complete

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
分享
二维码
< <上一篇
下一篇>>