视频拖动容器控件的实现

需要包裹1个view视频view ,为第一个child.

videoView = getChildAt(0);

为了防止重新绘制过程中导致被还原,需要做的事情是:

 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 if(videoView!=null){

            if (!isNotFirst) {
                initPointPosition.x = videoView.getLeft();
                initPointPosition.y = videoView.getTop();
                Prt.d(TAG, "first position:x:" + initPointPosition.x + ",y:" + initPointPosition.y);
                isNotFirst = true;
            }

            videoView.layout(initPointPosition.x, initPointPosition.y,
                    initPointPosition.x + videoView.getMeasuredWidth(),
                    initPointPosition.y + videoView.getMeasuredHeight());
        }

}

//如果是第一次就把初始化position赋值给initPointPosition




/**
 * Created by luozheng on 2016/10/13.  qssq666.cn Administrator
 */

public class VideoDragContainer extends FrameLayout {

    private static final String TAG = "VideoDragContainer";
    private ViewDragHelper mDragHelper;
    private int mWidth;
    private int mHeight;
    private View videoView;
    private Point initPointPosition = new Point();
    private boolean isNotFirst;
    private float mPercent = 1f;

    public VideoDragContainer(Context context) {
        super(context);
        init(context);
    }

    public VideoDragContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VideoDragContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public VideoDragContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        mDragHelper = ViewDragHelper.create(this, 1f, new ViewDragHelper.Callback() {
                    @Override
                    public boolean tryCaptureView(View child, int pointerId) {
//                        return child instanceof VideoContainer;
//                        return child instanceof true;
                        return true;
                    }

                    @Override
                    public int clampViewPositionHorizontal(View child, int left, int dx) {
                        Prt.i(TAG, "clampViewPositionHorizontal left:" + left + ",dx:" + dx);
                        if (left < 0) {
                            return 0;
                        } else if (left > (mWidth - child.getWidth())) {//往右边滑动不可超过直播的宽度
                            return mWidth - child.getWidth();
                        } else {
                            return left;
                        }

                      /*  final int leftBound = getPaddingLeft();
                        final int rightBound =mWidth - videoView.getWidth();
                        final int newLeft = Math.min(Math.max(left, leftBound), rightBound);
                        return newLeft;*/
                    }

                    @Override
                    public int clampViewPositionVertical(View child, int top, int dy) {
                        //撤销
                        if (top < 0) {
                            return 0;
                        } else if (top > (mHeight - child.getHeight())) {//往右边滑动不可超过直播的宽度
                            return mHeight - child.getHeight();
                        } else {
                            return top;
                        }
                    }

                    @Override
                    public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
//                 changedView.offsetTopAndBottom(-dy);//自己撤销回去?拉多少撤销多少 如果高度 不变的话 clampViewPositionVertical return  top就可以了


                    }

                    /**
                     * 手指释放的时候回调
                     */
                    @Override
                    public void onViewReleased(View releasedChild, float xvel, float yvel) {
                        if (releasedChild == videoView) {
                            int mY = releasedChild.getTop();
                            if (releasedChild.getTop() < 0) {
                                mY = 0;
                            }
                            if (releasedChild.getBottom() > getHeight()) {
                                mY = getHeight() - releasedChild.getMeasuredHeight();
                            }
                            if (releasedChild.getRight() - releasedChild.getMeasuredWidth() / 2 > getWidth() / 2) {
                                initPointPosition.x = (int) (getWidth() - videoView.getWidth() * mPercent);
                                initPointPosition.y = mY;
                                mDragHelper.settleCapturedViewAt(initPointPosition.x, initPointPosition.y);

                            } else {
                                initPointPosition.x = (int) (0 - videoView.getWidth() * (1 - mPercent));
                                initPointPosition.y = mY;
                                mDragHelper.settleCapturedViewAt(initPointPosition.x, initPointPosition.y);

                            }

                            invalidate();
                        }


                    }


                    /**
                     * 不进行复写的话 没法拖动 直接被onInte return false;
                     * @param child
                     * @return
                     */
                    @Override
                    public int getViewHorizontalDragRange(View child) {
                        return getMeasuredWidth() - child.getMeasuredWidth();
                    }


                    @Override
                    public int getViewVerticalDragRange(View child) {
                        return getMeasuredHeight() - child.getMeasuredHeight();
                    }
                /*    @Override
                    public int getViewVerticalDragRange(View child) {
                        return getMeasuredHeight() - child.getMeasuredHeight();
                    }*/
                }

        );

        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.DIRECTION_ALL);

    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(videoView!=null){

            if (!isNotFirst) {
                initPointPosition.x = videoView.getLeft();
                initPointPosition.y = videoView.getTop();
                Prt.d(TAG, "first position:x:" + initPointPosition.x + ",y:" + initPointPosition.y);
                isNotFirst = true;
            }

            videoView.layout(initPointPosition.x, initPointPosition.y,
                    initPointPosition.x + videoView.getMeasuredWidth(),
                    initPointPosition.y + videoView.getMeasuredHeight());
        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        final int action = MotionEventCompat.getActionMasked(event);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            return false;
        }
        return mDragHelper.shouldInterceptTouchEvent(event);
//        return mDragHelper.shouldInterceptTouchEvent(event);
    /*    if (mDragHelper.shouldInterceptTouchEvent(event)) {
            return true;
        } else {
            return super.onInterceptTouchEvent(event);
        }*/
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
//            mDragHelper.cancel();
            return false;
        }
        mDragHelper.processTouchEvent(event);
        return true;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        videoView = getChildAt(0);
        post(new Runnable() {
            @Override
            public void run() {
                mWidth = getWidth();
                mHeight = getHeight();
            }
        });
    }
}

你可能感兴趣的:(视频拖动容器控件的实现)