美文网首页程序员Android开发Android知识
Android 仿iOS右滑 finish 界面

Android 仿iOS右滑 finish 界面

作者: kingpengLin | 来源:发表于2016-05-23 19:25 被阅读709次

    本人用过iphone6 里面的右滑关闭界面,感觉很cool、很方便,于是就想到了安卓其实也可以集成这么爽的功能。。。

    上网搜了一下,发现小明大神博客

    Android 向右滑动销毁(finish)Activity, 随着手势的滑动而滑动的效果

    里面提供方法很实用,一个自定义帧布局即可实现效果,所以在此坐下记录,同时提供我加了判断的自定义滑动view,可自定义滑动响应区间。

    /**
     * 滑动finish Act
     */
    public class SwipeBackLayout extends FrameLayout {
        private static final String TAG = SwipeBackLayout.class.getSimpleName();
        private View mContentView;
        private int mTouchSlop;
        private int downX;
        private int downY;
        private int tempX;
        private Scroller mScroller;
        private int viewWidth;
        private boolean isSilding;
        private boolean isFinish;
        private Drawable mShadowDrawable;
        private Activity mActivity;
        private List<ViewPager> mViewPagers = new LinkedList<ViewPager>();
        public boolean isShowShadowDrawable;//是否包含边沿阴影
        private int mSlideDistance;//滑动响应距离
    
        public SwipeBackLayout(Context context) {
            this(context, null, 0);
        }
    
        public SwipeBackLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public SwipeBackLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
    
            mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
            mScroller = new Scroller(context);
            mSlideDistance = (int) ((float) (1.0f * getDisplayMetrics(context)[0]) / 2);
            if (isShowShadowDrawable) {
                mShadowDrawable = ContextCompat.getDrawable(context.getApplicationContext(), R.drawable.swipe_finish_shadow);
            }
        }
    
        /**
         * 获得屏幕分辨率
         *
         * @param context
         * @return Integer[width, height]
         */
        public int[] getDisplayMetrics(Context context) {
            WindowManager manager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            return new int[]{dm.widthPixels, dm.heightPixels};
        }
    
        public void setShadowDrawable(Drawable shadowDrawable) {
            if (shadowDrawable == null) {
                mShadowDrawable = ContextCompat.getDrawable(getContext().getApplicationContext(), R.drawable.swipe_finish_shadow);
            } else {
                mShadowDrawable = shadowDrawable;
            }
        }
    
        public void attachToActivity(Activity activity) {
            mActivity = activity;
            TypedArray a = activity.getTheme().obtainStyledAttributes(
                    new int[]{android.R.attr.windowBackground});
            int background = a.getResourceId(0, 0);
            a.recycle();
    
            ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
            ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
            decorChild.setBackgroundResource(background);
            decor.removeView(decorChild);
            addView(decorChild);
            setContentView(decorChild);
            decor.addView(this);
        }
    
        private void setContentView(View decorChild) {
            mContentView = (View) decorChild.getParent();
        }
    
        /**
         * 事件拦截操作
         */
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            //处理ViewPager冲突问题
            ViewPager mViewPager = getTouchViewPager(mViewPagers, ev);
            Log.i(TAG, "mViewPager = " + mViewPager);
    
            if (mViewPager != null && mViewPager.getCurrentItem() != 0) {
                return super.onInterceptTouchEvent(ev);
            }
    
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = tempX = (int) ev.getRawX();
                    downY = (int) ev.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int moveX = (int) ev.getRawX();
                    // 满足此条件屏蔽SildingFinishLayout里面子类的touch事件
                    if (moveX - downX > mTouchSlop
                            && Math.abs((int) ev.getRawY() - downY) < mTouchSlop) {
                        return true;
                    }
                    break;
            }
    
            return super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    int moveX = (int) event.getRawX();
                    int deltaX = tempX - moveX;
                    tempX = moveX;
                    if (downX > mSlideDistance) {
                        isSilding = false;
                    } else if (moveX - downX > mTouchSlop
                            && Math.abs((int) event.getRawY() - downY) < mTouchSlop) {
                        isSilding = true;
                    }
                    MLog.i("ACTION_MOVE mTouchSLop ==" + mTouchSlop);
    
                    if (moveX - downX >= 0 && isSilding) {
                        mContentView.scrollBy(deltaX, 0);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isSilding = false;
                    if (mContentView.getScrollX() <= -viewWidth / 4) {
                        isFinish = true;
                        scrollRight();
                    } else {
                        scrollOrigin();
                        isFinish = false;
                    }
                    break;
            }
    
            return true;
        }
    
        /**
         * 获取SwipeBackLayout里面的ViewPager的集合
         *
         * @param mViewPagers
         * @param parent
         */
        private void getAlLViewPager(List<ViewPager> mViewPagers, ViewGroup parent) {
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                if (child instanceof ViewPager) {
                    mViewPagers.add((ViewPager) child);
                } else if (child instanceof ViewGroup) {
                    getAlLViewPager(mViewPagers, (ViewGroup) child);
                }
            }
        }
    
    
        /**
         * 返回我们touch的ViewPager
         *
         * @param mViewPagers
         * @param ev
         * @return
         */
        private ViewPager getTouchViewPager(List<ViewPager> mViewPagers, MotionEvent ev) {
            if (mViewPagers == null || mViewPagers.size() == 0) {
                return null;
            }
            Rect mRect = new Rect();
            for (ViewPager v : mViewPagers) {
                v.getHitRect(mRect);
    
                if (mRect.contains((int) ev.getX(), (int) ev.getY())) {
                    return v;
                }
            }
            return null;
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            if (changed) {
                viewWidth = this.getWidth();
    
                getAlLViewPager(mViewPagers, this);
                Log.i(TAG, "ViewPager size = " + mViewPagers.size());
            }
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            if (mShadowDrawable != null && mContentView != null) {
    
                int left = mContentView.getLeft()
                        - mShadowDrawable.getIntrinsicWidth();
                int right = left + mShadowDrawable.getIntrinsicWidth();
                int top = mContentView.getTop();
                int bottom = mContentView.getBottom();
    
                mShadowDrawable.setBounds(left, top, right, bottom);
                mShadowDrawable.draw(canvas);
            }
    
        }
    
        /**
         * 滚动出界面
         */
        private void scrollRight() {
            final int delta = (viewWidth + mContentView.getScrollX());
            // 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
            mScroller.startScroll(mContentView.getScrollX(), 0, -delta + 1, 0,
                    Math.abs(delta));
            postInvalidate();
        }
    
        /**
         * 滚动到起始位置
         */
        private void scrollOrigin() {
            int delta = mContentView.getScrollX();
            mScroller.startScroll(mContentView.getScrollX(), 0, -delta, 0,
                    Math.abs(delta));
            postInvalidate();
        }
    
        @Override
        public void computeScroll() {
            // 调用startScroll的时候scroller.computeScrollOffset()返回true,
            if (mScroller.computeScrollOffset()) {
                mContentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
                postInvalidate();
    
                if (mScroller.isFinished() && isFinish) {
                    mActivity.finish();
                }
            }
        }
    
    
    }
    

    相关文章

      网友评论

        本文标题:Android 仿iOS右滑 finish 界面

        本文链接:https://www.haomeiwen.com/subject/csnarttx.html