本文实例为大家分享了基于SurfaceView的可拖动视频控件,供大家参考,具体内容如下
公共类DragSurfaceView SurfaceView延伸实现视图。OnTouchListener { 保护int screenWidth; 保护int screenHeight; 保护int lastX; 保护int lastY; 私人int oriLeft; 私人int oriRight; 私人int oriTop; 私人int oriBottom; 私人int dragDirection; 私有静态最终int顶级=0连接; 私有静态最终int=0 x16时; 私有静态最终int=0 x17底部; 私有静态最终int右=0 x18; 私有静态最终int LEFT_TOP=0 x11; 私有静态最终int RIGHT_TOP=0 x12; 私有静态最终int LEFT_BOTTOM=0 * 13; 私有静态最终int RIGHT_BOTTOM=0 x14; 私有静态最终int中心=0 x19; 私人int抵消=20;/* * *初始化获取屏幕宽高 */保护无效initScreenW_H () { .getDisplayMetrics screenHeight=getresource () ()。heightPixels - 40; .widthPixels .getDisplayMetrics screenWidth=getresource () (); 日志。我(“DragViewTAG”、“DragSurfaceView。initScreenW_H: screenWidth=" + screenWidth +”, screenHeight=" + screenHeight); } 公共DragSurfaceView(上下文语境){ 超级(上下文); setOnTouchListener(这个); initScreenW_H (); } 公共DragSurfaceView(上下文语境,AttributeSet attrs) { 超级(上下文,attrs); setOnTouchListener(这个); initScreenW_H (); } 公共DragSurfaceView(上下文语境、AttributeSet attrs int defStyleAttr) { 超级(上下文、attrs defStyleAttr); setOnTouchListener(这个); initScreenW_H (); } @Override 公共布尔,左=" + oriLeft + ",顶级=" + oriTop +”,右=" + oriRight +”,底部=" + oriBottom); 打破; 案例MotionEvent.ACTION_UP: ViewGroup。LayoutParams newLayoutParams=getNewLayoutParams (); 如果(newLayoutParams !=null) { 日志。我(“DragViewTAG”、“DragSurfaceView.delDrag: ACTION_UP宽度=" + newLayoutParams。宽度+”,身高=" + newLayoutParams.height); setLayoutParams (newLayoutParams); 其他}{ 日志。e (“DragViewTAG”、“DragSurfaceView.delDrag:父组件类型?”); v。布局(oriLeft、oriTop oriRight oriBottom); } 打破; 默认值: 打破; } } 私人ViewGroup。LayoutParams getNewLayoutParams () { 如果(getLayoutParams()运算符RelativeLayout.LayoutParams) { RelativeLayout。LayoutParams lp=(RelativeLayout.LayoutParams) getLayoutParams (); lp。leftMargin=oriLeft; lp。页面顶栏=oriTop; lp。宽度=oriRight-oriLeft; lp。身高=oriBottom-oriTop; 返回资讯; }else if (getLayoutParams()运算符FrameLayout.LayoutParams) { FrameLayout。LayoutParams lp=(FrameLayout.LayoutParams) getLayoutParams (); lp。leftMargin=oriLeft; lp。页面顶栏=oriTop; lp。宽度=oriRight - oriLeft; lp。身高=oriBottom - oriTop; 返回资讯; } 返回null; }/* * *触摸点为中心→在移动 * * @param v * @param dx * @param dy */私人空间中心(int视图v, int dx, dy) { oriLeft +=dx; oriTop +=dy; oriRight +=dx; oriBottom +=dy; 日志。我(“DragViewTAG”、“DragSurfaceView。中心:v.left=" + v.getLeft () +”, v.top=" + v.getTop ()); 如果(oriLeft & lt;抵消){ 日志。e (“DragViewTAG”、“DragSurfaceView。中心:左侧越界,左=" + oriLeft +”,抵消=" +偏移量); oriLeft=抵消; oriRight=oriLeft + v.getWidth (); } 如果(oriRight比;screenWidth +偏移量){ 日志。e (“DragViewTAG”、“DragSurfaceView。中心:右侧越界,对吧=" + oriRight +”, screenWidth=" + screenWidth +”,抵消=" +偏移量); oriRight=screenWidth +偏移量; oriLeft=oriRight - v.getWidth (); } 如果(oriTop & lt;抵消){ 日志。e (“DragViewTAG”、“DragSurfaceView。中心:顶部越界顶级=? oriTop +”,抵消=" +偏移量); oriTop=抵消; oriBottom=oriTop + v.getHeight (); } 如果(oriBottom比;screenHeight +偏移量){ 日志。e (“DragViewTAG”、“DragSurfaceView。中心:底部越界,底部=" + oriBottom +”, screenHeight=" + screenHeight +”,抵消=" +偏移量); oriBottom=screenHeight +偏移量; oriTop=oriBottom - v.getHeight (); }//v。布局(左,上,右,下); }/* * *触摸点为上边缘 * * @param v * @param dy */私人空间顶部(查看v, int dy) { oriTop +=dy; 如果(oriTop & lt;抵消){ oriTop=抵消; } 如果(oriBottom - oriTop - 2 *抵消& lt;200){ oriTop=oriBottom - 2 *抵消- 200; } }/* * *触摸点为下边缘 * * @param v * @param dy */私人空间底部(查看v, int dy) { oriBottom +=dy; 如果(oriBottom比;screenHeight +偏移量){ oriBottom=screenHeight +偏移量; } 如果(oriBottom - oriTop - 2 *抵消& lt;200){ oriBottom=200 + oriTop + 2 *抵消; } }/* * *触摸点为右边缘 * * @param v * @param dx */私人空间权利(查看v, int dx) { oriRight +=dx; 如果(oriRight比;screenWidth +偏移量){ oriRight=screenWidth +偏移量; } 如果(oriRight - oriLeft - 2 *抵消& lt;200){ oriRight=oriLeft + 2 *抵消+ 200; } }/* * *触摸点为左边缘 * * @param v * @param dx */私人的(查看v, int dx) { oriLeft +=dx; 如果(oriLeft & lt;抵消){ oriLeft=抵消; } 如果(oriRight - oriLeft - 2 *抵消& lt;200){ oriLeft=oriRight - 2 *抵消- 200; } }/* * *获取截取宽度 * * @return */公共int getCutWidth () { 返回getWidth() - 2 *抵消; }/* * *获取截取高度 * * @return */公共int getCutHeight () { 返回获得()- 2 *抵消; } }基于SurfaceView实现可拖动视频控件