Slide to close the activity.

Source: Internet
Author: User

Slide to close the activity.

I always think it is cool to slide the activity function and want to add this function to my project. I found a good idea on the Internet.

Use ViewDragHelper.

First, you must set the theme of the activity to make the window of the activity transparent.

<style name="MyActivityBackground" parent="MyAppTheme">        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowAnimationStyle">@android:style/Animation.Activity</item></style>

This topic inherits from MyAppTheme (this is also a custom topic, which is used by the entire application. To avoid conflicts, inherit from it ).
In particular, it should be noted that windowAnimationStyle is used to set the animation effect of activity entry and exit, and set it to the default effect.

Then you can use this topic in the registration file of the activity that requires sliding and exiting.

Below are all the code

Public class MyDragViewGroup extends FrameLayout {private ViewDragHelper mViewDragHelper; // wide private int mWidth of the custom component; // high private int mHeight of the custom component; // The current sliding distance of the component to be slide: private int currentLeft; // The sliding distance is used to determine whether the activity can be disabled by private int mSlidX; private static final int x = 40; private Context mContext; // view private View view to be slide; private Paint mPaint; public MyDragViewGroup (Context context) {super (context); initView (context );} public void bind () {// obtain the ViewGroup viewGroup = (ViewGroup) (activity) mContext of the Activity ). getWindow (). getDecorView (); view = viewGroup. getChildAt (0); viewGroup. removeView (view); // remove this view from addView (view); // Add this view to this custom component viewGroup. addView (this); // Add the custom component to decorView} @ Override protected void onSizeChanged (int w, int h, int oldw, int oldh) {super. onSizeChanged (w, h, oldw, oldh); mWidth = getMeasuredWidth (); mHeight = getMeasuredHeight (); mSlidX = (int) (mWidth * 0.3 );} private void initView (Context context) {mContext = context; mViewDragHelper = ViewDragHelper. create (this, callback); // Edge Detection mViewDragHelper. setEdgeTrackingEnabled (ViewDragHelper. EDGE_LEFT); mPaint = new Paint (); mPaint. setStrokeWidth (2); mPaint. setAntiAlias (true); mPaint. setColor (Color. GRAY);} @ Override public boolean onInterceptTouchEvent (MotionEvent ev) {return mViewDragHelper. shouldInterceptTouchEvent (ev) ;}@ Override public boolean onTouchEvent (MotionEvent event) {mViewDragHelper. processTouchEvent (event); return true;} private ViewDragHelper. callback callback = new ViewDragHelper. callback () {@ Override public boolean tryCaptureView (View child, int pointerId) {return false ;}@ Override public int clampViewPositionVertical (View child, int top, int dy) {return 0 ;}@ Override public int clampViewPositionHorizontal (View child, int left, int dx) {return left ;}@ Override public void onViewReleased (View releasedChild, float xvel, float yvel) {super. onViewReleased (releasedChild, xvel, yvel); // if the sliding distance is smaller than ms1_x, it will be slid back to the original if (view. getLeft () <ms0000x) {mViewDragHelper. smoothSlideViewTo (view, 0, 0); ViewCompat. postInvalidateOnAnimation (MyDragViewGroup. this);} // otherwise, the else {mViewDragHelper. smoothSlideViewTo (view, mWidth, 0); ViewCompat. postInvalidateOnAnimation (MyDragViewGroup. this);} invalidate () ;}@ Override public void onEdgeDragStarted (int edgeFlags, int pointerId) {mViewDragHelper. captureChildView (view, pointerId) ;}@ Override public void onViewPositionChanged (View changedView, int left, int top, int dx, int dy) {currentLeft = left; invalidate (); if (left> = mWidth) {(Activity) mContext ). finish () ;}};@ Override public void computeScroll () {if (mViewDragHelper. continueSettling (true) {ViewCompat. postInvalidateOnAnimation (this); invalidate () ;}@ Override protected void dispatchDraw (Canvas canvas) {drawShadow (canvas); super. dispatchDraw (canvas);} protected void drawShadow (Canvas canvas) {canvas. save (); Shader mShader = new LinearGradient (currentLeft-x, 0, currentLeft, 0, new int [] {Color. parseColor ("#1 edddddd"), Color. parseColor ("#6e666666"), Color. parseColor ("#9e666666")}, null, Shader. tileMode. REPEAT); mPaint. setShader (mShader); // when drawing, pay attention to the offset to the left of RectF rectF = new RectF (currentLeft-x, 0, currentLeft, mHeight); canvas. drawRect (rectF, mPaint); canvas. restore ();}}

If you know how to use ViewDragHelper, the above Code is easy to understand. DrawShadow () is used to draw shadows. You must refresh and re-paint the shadows as the activity moves.

 

It is worth noting that the ViewGroup container component is drawn. When there is no background, the dispatchDraw () method is called directly, and the draw () method is bypassed, when there is a background, the draw () method is called, while the draw () method contains the call of the dispatchDraw () method. Therefore, the dispatchDraw () method rather than the onDraw () method is often overwritten when drawing things on the ViewGroup, or a Drawable can be customized to override its draw (Canvas c) and getIntrinsicWidth (), getIntrinsicHeight (), and then set it as the background.

 

To use this function, you only need to add it to the activity.

MyDragViewGroup myDragViewGroup = new MyDragViewGroup (this); myDragViewGroup. bind (); OK.

MyDragViewGroup. bind (); puts the view of the activity into the custom ViewGroup, and then sets the whole custom ViewGroup to the activity.

Reference blog: http://blog.csdn.net/meijian531161724/article/details/50763931

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.