拖拽的逻辑一定是在onTouch中去处理了,因节操播放器需处理播放进度以及UI逻辑,所以原有父类JCVideoPlayerStandard中已重写onTouch方法。现在要做的是单独封装一个类用来拓展拖拽关闭视频功能。
贴出关键代码
@Override public boolean onTouch(View v, MotionEvent event) { if (currentStatus == STATUS_RESETTING) return false; int id = v.getId(); if (id == fm.jiecao.jcvideoplayer_lib.R.id.surface_container) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mDownX = event.getRawX(); mDownY = event.getRawY(); addIntoVelocity(event); break; case MotionEvent.ACTION_MOVE: addIntoVelocity(event); int deltaY = (int) (event.getRawY() - mDownY); //手指往上滑动 if (deltaY <= DRAG_GAP_PX && currentStatus != STATUS_MOVING) return super.onTouchEvent(event); //手指往下滑动,开始缩放 if (deltaY > DRAG_GAP_PX || currentStatus == STATUS_MOVING) { moveView(event.getRawX(), event.getRawY()); return true; } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (event.getAction() == MotionEvent.ACTION_UP) { startDismissControlViewTimer(); if (mChangePosition) { int duration = getDuration(); int progress = mSeekTimePosition * 100 / (duration == 0 ? 1 : duration); bottomProgressBar.setProgress(progress); } if (!mChangePosition && !mChangeVolume) { onEvent(JCUserActionStandard.ON_CLICK_BLANK); onClickUiToggle(); } } //-------------------------------------------- if (currentStatus != STATUS_MOVING) return super.onTouchEvent(event); final float mUpX = event.getRawX(); final float mUpY = event.getRawY(); float vY = computeYVelocity();//松开时必须释放VelocityTracker资源 if (vY >= 1200 || Math.abs(mUpY - mDownY) > screenHeight / 4) { //下滑速度快,或者下滑距离超过屏幕高度的一半,就关闭 if (iAnimClose != null) { iAnimClose.onPictureRelease(); } } else { resetReviewState(mUpX, mUpY); } break; } }
//设置播放进度
else if (id == fm.jiecao.jcvideoplayer_lib.R.id.progress) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: cancelDismissControlViewTimer(); break; case MotionEvent.ACTION_UP: startDismissControlViewTimer(); break; } } return super.onTouch(v, event); }
手指下滑的时候移动控件,移动过程利用ViewHelper对控件进行缩放,缩放范围自己控制。
//移动View private void moveView(float movingX, float movingY) { if (currentShowView == null) return; currentStatus = STATUS_MOVING; float deltaX = movingX - mDownX; float deltaY = movingY - mDownY; float scale = 1f; float alphaPercent = 1f; if (deltaY > 0) { scale = 1 - Math.abs(deltaY) / screenHeight; alphaPercent = 1 - Math.abs(deltaY) / (screenHeight / 2); } ViewHelper.setTranslationX(currentShowView, deltaX); ViewHelper.setTranslationY(currentShowView, deltaY); scaleView(scale); setBackgroundColor(getBlackAlpha(alphaPercent)); }
下滑距离超过屏幕一半,可通过接口回调在onPictureRelease中处理关闭的逻辑。
写完测试,发现拖拽过程中背景是一片黑色,尝试将activity的主题设置为半透明主题,就解决啦!
By LiYing