Android slide up or down to end Activity

Keywords: Android xml simulator encoding

I've seen it before xiaanming The side slip is written to return, so it follows his Demo and writes the slide from top to bottom or the slide from bottom to top to end the Activity


Let's take a picture first. Because of the problem of the resolution of this computer and the reason of the simulator, let's have a look




Post code first:

Slide down:


  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.view.MotionEvent;  
  4. import android.view.ViewConfiguration;  
  5. import android.view.ViewGroup;  
  6. import android.widget.RelativeLayout;  
  7. import android.widget.Scroller;  
  8.   
  9. /*** 
  10.  *  
  11.  * Slide up to end Activity 
  12.  *  
  13.  * @author The brim can't keep out the sun 
  14.  *  
  15.  */  
  16. public class TopToBottomFinishLayout extends RelativeLayout {  
  17.       
  18.     /** 
  19.      * TopToBottomFinishLayout Parent layout of layout 
  20.      */  
  21.     private ViewGroup mParentView;  
  22.     /** 
  23.      * Minimum sliding distance 
  24.      */  
  25.     private int mTouchSlop;  
  26.     /** 
  27.      * X coordinate of pressing point 
  28.      */  
  29.     private int downX;  
  30.     /** 
  31.      * Y coordinate of pressing point 
  32.      */  
  33.     private int downY;  
  34.     /** 
  35.      * Temporary storage of X coordinate 
  36.      */  
  37.     private int tempY;  
  38.     /** 
  39.      * Sliding class 
  40.      */  
  41.     private Scroller mScroller;  
  42.     /** 
  43.      * TopToBottomFinishLayout Width of 
  44.      */  
  45.     private int viewHeight;  
  46.   
  47.     private boolean isSilding;  
  48.   
  49.     private OnFinishListener onFinishListener;  
  50.     private boolean isFinish;  
  51.   
  52.     public TopToBottomFinishLayout(Context context, AttributeSet attrs) {  
  53.         this(context, attrs, 0);  
  54.     }  
  55.   
  56.     public TopToBottomFinishLayout(Context context, AttributeSet attrs, int defStyle) {  
  57.         super(context, attrs, defStyle);  
  58.   
  59.         mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
  60.         mScroller = new Scroller(context);  
  61.     }  
  62.   
  63.     /** 
  64.      * Event interception operation 
  65.      */  
  66.     @Override  
  67.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  68.   
  69.         switch (ev.getAction()) {  
  70.         case MotionEvent.ACTION_DOWN:  
  71.             downX = (int) ev.getRawX();  
  72.             downY = tempY = (int) ev.getRawY();  
  73.             break;  
  74.         case MotionEvent.ACTION_MOVE:  
  75.             int moveY = (int) ev.getRawY();  
  76.             //Meet this condition to shield touch events of subclasses in SildingFinishLayout  
  77.             if (Math.abs(moveY - downY) > mTouchSlop  
  78.                     && Math.abs((int) ev.getRawX() - downX) < mTouchSlop) {  
  79.                 return true;  
  80.             }  
  81.             break;  
  82.         }  
  83.         return super.onInterceptTouchEvent(ev);  
  84.     }  
  85.   
  86.     @Override  
  87.     public boolean onTouchEvent(MotionEvent event) {  
  88.         switch (event.getAction()) {  
  89.         case MotionEvent.ACTION_MOVE:  
  90.             int moveY = (int) event.getRawY();//Touch point position relative to screen  
  91.             int deltaY = tempY - moveY;  
  92.             tempY = moveY;  
  93.             if (Math.abs(moveY - downY) > mTouchSlop  
  94.                     && Math.abs((int) event.getRawX() - downX) < mTouchSlop) {  
  95.                 isSilding = true;  
  96.             }  
  97.   
  98.             if (moveY - downY >= 0 && isSilding) {  
  99.                 mParentView.scrollBy(0, deltaY);  
  100.             }  
  101.             break;  
  102.         case MotionEvent.ACTION_UP:  
  103.             isSilding = false;  
  104.             if (mParentView.getScrollY() <= -viewHeight / 3) {  
  105.                 isFinish = true;  
  106.                 scrollBottom();  
  107.             } else {  
  108.                 scrollOrigin();  
  109.                 isFinish = false;  
  110.             }  
  111.             break;  
  112.         }  
  113.   
  114.         return true;  
  115.     }  
  116.   
  117.     @Override  
  118.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  119.         super.onLayout(changed, l, t, r, b);  
  120.         if (changed) {  
  121.             mParentView = (ViewGroup) this.getParent();  
  122.             viewHeight = this.getHeight();  
  123.         }  
  124.     }  
  125.   
  126.     /*** 
  127.      * Interface callback 
  128.      */  
  129.     public void setOnFinishListener(  
  130.             OnFinishListener onSildingFinishListener) {  
  131.         this.onFinishListener = onSildingFinishListener;  
  132.     }  
  133.   
  134.     /** 
  135.      * Scroll out interface 
  136.      */  
  137.     private void scrollBottom() {  
  138.         final int delta = (viewHeight + mParentView.getScrollY());  
  139.         mScroller.startScroll(0, mParentView.getScrollY(), 0, -delta + 1,  
  140.                 Math.abs(delta));  
  141.         postInvalidate();  
  142.     }  
  143.   
  144.     /** 
  145.      * Scroll to start position 
  146.      */  
  147.     private void scrollOrigin() {  
  148.         int delta = mParentView.getScrollY();  
  149.         mScroller.startScroll(0, mParentView.getScrollY(), 0, -delta,  
  150.                 Math.abs(delta));  
  151.         postInvalidate();  
  152.     }  
  153.   
  154.     @Override  
  155.     public void computeScroll() {  
  156.         if (mScroller.computeScrollOffset()) {  
  157.             mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
  158.             postInvalidate();  
  159.   
  160.             if (mScroller.isFinished() && isFinish) {  
  161.                 if (onFinishListener != null) {  
  162.                     onFinishListener.onFinish();  
  163.                 } else {  
  164.                     //Onsillingfinishlistener is not set to scroll to its location  
  165.                     scrollOrigin();  
  166.                     isFinish = false;  
  167.                 }  
  168.             }  
  169.         }  
  170.     }  
  171.   
  172.     public interface OnFinishListener {  
  173.         public void onFinish();  
  174.     }  
  175.   
  176. }  


Slide up and down:


  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.view.MotionEvent;  
  4. import android.view.ViewConfiguration;  
  5. import android.view.ViewGroup;  
  6. import android.widget.RelativeLayout;  
  7. import android.widget.Scroller;  
  8.   
  9. /*** 
  10.  *  
  11.  * Slide down to end Activity 
  12.  *  
  13.  * @author The brim can't keep out the sun 
  14.  *  
  15.  */  
  16. public class BottomToTopFinishLayout extends RelativeLayout {  
  17.       
  18.     /** 
  19.      * BottomFinishLayout Parent layout of layout 
  20.      */  
  21.     private ViewGroup mParentView;  
  22.     /** 
  23.      * Minimum sliding distance 
  24.      */  
  25.     private int mTouchSlop;  
  26.     /** 
  27.      * X coordinate of pressing point 
  28.      */  
  29.     private int downX;  
  30.     /** 
  31.      * Y coordinate of pressing point 
  32.      */  
  33.     private int downY;  
  34.     /** 
  35.      * Temporary storage of X coordinate 
  36.      */  
  37.     private int tempY;  
  38.     /** 
  39.      * Sliding class 
  40.      */  
  41.     private Scroller mScroller;  
  42.     /** 
  43.      * BottomFinishLayout Width of 
  44.      */  
  45.     private int viewHeight;  
  46.   
  47.     private boolean isSilding;  
  48.   
  49.     private OnFinishListener onFinishListener;  
  50.     private boolean isFinish;  
  51.   
  52.     public BottomToTopFinishLayout(Context context, AttributeSet attrs) {  
  53.         this(context, attrs, 0);  
  54.     }  
  55.   
  56.     public BottomToTopFinishLayout(Context context, AttributeSet attrs,  
  57.             int defStyle) {  
  58.         super(context, attrs, defStyle);  
  59.   
  60.         mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
  61.         mScroller = new Scroller(context);  
  62.     }  
  63.   
  64.     /** 
  65.      * Event interception operation 
  66.      */  
  67.     @Override  
  68.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  69.   
  70.         switch (ev.getAction()) {  
  71.         case MotionEvent.ACTION_DOWN:  
  72.             downX = (int) ev.getRawX();  
  73.             downY = tempY = (int) ev.getRawY();  
  74.             break;  
  75.         case MotionEvent.ACTION_MOVE:  
  76.             int moveY = (int) ev.getRawY();  
  77.             //Meet this condition to shield touch events of subclasses in SildingFinishLayout  
  78.             if (Math.abs(downY - moveY) > mTouchSlop  
  79.                     && Math.abs((int) ev.getRawX() - downX) < mTouchSlop) {  
  80.                 return true;  
  81.             }  
  82.             break;  
  83.         }  
  84.         return super.onInterceptTouchEvent(ev);  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean onTouchEvent(MotionEvent event) {  
  89.         switch (event.getAction()) {  
  90.         case MotionEvent.ACTION_MOVE:  
  91.             int moveY = (int) event.getRawY();//Touch point position relative to screen  
  92.             int deltaY = moveY - tempY;  
  93.             tempY = moveY;  
  94.             if (Math.abs(downY - moveY) > mTouchSlop  
  95.                     && Math.abs((int) event.getRawX() - downX) < mTouchSlop) {  
  96.                 isSilding = true;  
  97.             }  
  98.   
  99.             if (downY - moveY >= 0 && isSilding) {  
  100.                 mParentView.scrollBy(0, -deltaY);  
  101.             }  
  102.             break;  
  103.         case MotionEvent.ACTION_UP:  
  104.             isSilding = false;  
  105.             System.out.println("vvv===========" + mParentView.getScrollY());  
  106.             System.out.println("/3============" + viewHeight / 3);  
  107.             if (mParentView.getScrollY() >= viewHeight / 3) {  
  108.                 isFinish = true;  
  109.                 scrollTop();  
  110.             } else {  
  111.                 scrollOrigin();  
  112.                 isFinish = false;  
  113.             }  
  114.             break;  
  115.         }  
  116.         return true;  
  117.     }  
  118.   
  119.     @Override  
  120.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  121.         super.onLayout(changed, l, t, r, b);  
  122.         if (changed) {  
  123.             mParentView = (ViewGroup) this.getParent();  
  124.             viewHeight = this.getHeight();  
  125.         }  
  126.     }  
  127.   
  128.     /*** 
  129.      * Interface callback 
  130.      */  
  131.     public void setOnFinishListener(OnFinishListener onSildingFinishListener) {  
  132.         this.onFinishListener = onSildingFinishListener;  
  133.     }  
  134.   
  135.     /** 
  136.      * Scroll out interface 
  137.      */  
  138.     private void scrollTop() {  
  139.         final int delta = (viewHeight - mParentView.getScrollY());  
  140.         mScroller.startScroll(0, mParentView.getScrollY(), 0, delta - 1,  
  141.                 Math.abs(delta));  
  142.         postInvalidate();  
  143.     }  
  144.   
  145.     /** 
  146.      * Scroll to start position 
  147.      */  
  148.     private void scrollOrigin() {  
  149.         int delta = mParentView.getScrollY();  
  150.         mScroller.startScroll(0, mParentView.getScrollY(), 0, -delta,  
  151.                 Math.abs(delta));  
  152.         postInvalidate();  
  153.     }  
  154.   
  155.     @Override  
  156.     public void computeScroll() {  
  157.         if (mScroller.computeScrollOffset()) {  
  158.             mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
  159.             postInvalidate();  
  160.             if (mScroller.isFinished() && isFinish) {  
  161.                 if (onFinishListener != null) {  
  162.                     onFinishListener.onFinish();  
  163.                 } else {  
  164.                     //Onsillingfinishlistener is not set to scroll to its location  
  165.                     scrollOrigin();  
  166.                     isFinish = false;  
  167.                 }  
  168.             }  
  169.         }  
  170.     }  
  171.   
  172.     public interface OnFinishListener {  
  173.         public void onFinish();  
  174.     }  
  175.   
  176. }  


Activity:

  1.  public class FromTopActivity extends Activity {  
  2.   
  3.   
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {  
  6. super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.activity_from_top);  
  8. TopToBottomFinishLayout bottomFinishLayout = (TopToBottomFinishLayout) findViewById(R.id.layout);  
  9. bottomFinishLayout.setOnFinishListener(new OnFinishListener() {  
  10.   
  11.   
  12. @Override  
  13. public void onFinish() {  
  14. finish();  
  15. }  
  16. });  
  17. }  
  18.   
  19.   
  20. }  

layout:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <com.example.mytest.TopToBottomFinishLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:background="#5DC890"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:gravity="center"  
  13.         android:text="TestActivity"  
  14.         android:textColor="#ff8840" />  
  15.   
  16. </com.example.mytest.TopToBottomFinishLayout>  



To realize this, it is very simple. In onTouchEvent, judge the distance of finger movement. If it is greater than the threshold value set by yourself, scroll to the top or the bottom, otherwise return to the initial position. So here we use the Scroller, scroller.startScroll After that, postInvalidate refreshes the interface.

Finally, rewrite computeScroll to determine whether the Scroller has finish ed and whether the distance of finger movement is greater than the threshold value set by itself.


Finally, remember that AndroidManifest.xml Set the theme of Activity to Theme.Translucent Otherwise, no effect will be seen


Demo:http://download.csdn.net/detail/qq_18612815/9615890


Reproduced from: http://blog.csdn.net/qq_18612815/article/details/52353078

The ape is 9527



Posted by k3Bobos on Tue, 19 May 2020 08:58:24 -0700