基于SurfaceView实现可拖动视频控件
本文实例为大家分享了基于SurfaceView的可拖动视频控件,供大家参考,具体内容如下
publicclassDragSurfaceViewextendsSurfaceViewimplementsView.OnTouchListener{
protectedintscreenWidth;
protectedintscreenHeight;
protectedintlastX;
protectedintlastY;
privateintoriLeft;
privateintoriRight;
privateintoriTop;
privateintoriBottom;
privateintdragDirection;
privatestaticfinalintTOP=0x15;
privatestaticfinalintLEFT=0x16;
privatestaticfinalintBOTTOM=0x17;
privatestaticfinalintRIGHT=0x18;
privatestaticfinalintLEFT_TOP=0x11;
privatestaticfinalintRIGHT_TOP=0x12;
privatestaticfinalintLEFT_BOTTOM=0x13;
privatestaticfinalintRIGHT_BOTTOM=0x14;
privatestaticfinalintCENTER=0x19;
privateintoffset=20;
/**
*初始化获取屏幕宽高
*/
protectedvoidinitScreenW_H(){
screenHeight=getResources().getDisplayMetrics().heightPixels-40;
screenWidth=getResources().getDisplayMetrics().widthPixels;
Log.i("DragViewTAG","DragSurfaceView.initScreenW_H:screenWidth="+screenWidth+",screenHeight="+screenHeight);
}
publicDragSurfaceView(Contextcontext){
super(context);
setOnTouchListener(this);
initScreenW_H();
}
publicDragSurfaceView(Contextcontext,AttributeSetattrs){
super(context,attrs);
setOnTouchListener(this);
initScreenW_H();
}
publicDragSurfaceView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
setOnTouchListener(this);
initScreenW_H();
}
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
intaction=event.getAction();
if(action==MotionEvent.ACTION_DOWN){
oriLeft=v.getLeft();
oriRight=v.getRight();
oriTop=v.getTop();
oriBottom=v.getBottom();
lastY=(int)event.getRawY();
lastX=(int)event.getRawX();
dragDirection=getDirection(v,(int)event.getX(),
(int)event.getY());
}
//处理拖动事件
delDrag(v,event,action);
if(action==MotionEvent.ACTION_UP){
dragDirection=0;
}
invalidate();
returntrue;
}
/**
*获取触摸点flag
*
*@paramv
*@paramx
*@paramy
*@return
*/
protectedintgetDirection(Viewv,intx,inty){
intleft=v.getLeft();
intright=v.getRight();
intbottom=v.getBottom();
inttop=v.getTop();
if(x<40&&y<40){
returnLEFT_TOP;
}
if(y<40&&right-left-x<40){
returnRIGHT_TOP;
}
if(x<40&&bottom-top-y<40){
returnLEFT_BOTTOM;
}
if(right-left-x<40&&bottom-top-y<40){
returnRIGHT_BOTTOM;
}
if(x<40){
returnLEFT;
}
if(y<40){
returnTOP;
}
if(right-left-x<40){
returnRIGHT;
}
if(bottom-top-y<40){
returnBOTTOM;
}
returnCENTER;
}
/**
*处理拖动事件
*
*@paramv
*@paramevent
*@paramaction
*/
protectedvoiddelDrag(Viewv,MotionEventevent,intaction){
switch(action){
caseMotionEvent.ACTION_MOVE:
intdx=(int)event.getRawX()-lastX;
intdy=(int)event.getRawY()-lastY;
switch(dragDirection){
caseLEFT://左边缘
left(v,dx);
break;
caseRIGHT://右边缘
right(v,dx);
break;
caseBOTTOM://下边缘
bottom(v,dy);
break;
caseTOP://上边缘
top(v,dy);
break;
caseCENTER://点击中心-->>移动
center(v,dx,dy);
break;
caseLEFT_BOTTOM://左下
left(v,dx);
bottom(v,dy);
break;
caseLEFT_TOP://左上
left(v,dx);
top(v,dy);
break;
caseRIGHT_BOTTOM://右下
right(v,dx);
bottom(v,dy);
break;
caseRIGHT_TOP://右上
right(v,dx);
top(v,dy);
break;
default:
break;
}
v.layout(oriLeft,oriTop,oriRight,oriBottom);
//if(dragDirection!=CENTER){
//v.layout(oriLeft,oriTop,oriRight,oriBottom);
//}
lastX=(int)event.getRawX();
lastY=(int)event.getRawY();
Log.i("DragViewTAG","DragSurfaceView.delDrag:ACTION_MOVEdirection="+dragDirection+",left="+oriLeft+",top="+oriTop+",right="+oriRight+",bottom="+oriBottom);
break;
caseMotionEvent.ACTION_UP:
ViewGroup.LayoutParamsnewLayoutParams=getNewLayoutParams();
if(newLayoutParams!=null){
Log.i("DragViewTAG","DragSurfaceView.delDrag:ACTION_UPwidth="+newLayoutParams.width+",height="+newLayoutParams.height);
setLayoutParams(newLayoutParams);
}else{
Log.e("DragViewTAG","DragSurfaceView.delDrag:父组件类型?");
v.layout(oriLeft,oriTop,oriRight,oriBottom);
}
break;
default:
break;
}
}
privateViewGroup.LayoutParamsgetNewLayoutParams(){
if(getLayoutParams()instanceofRelativeLayout.LayoutParams){
RelativeLayout.LayoutParamslp=(RelativeLayout.LayoutParams)getLayoutParams();
lp.leftMargin=oriLeft;
lp.topMargin=oriTop;
lp.width=oriRight-oriLeft;
lp.height=oriBottom-oriTop;
returnlp;
}elseif(getLayoutParams()instanceofFrameLayout.LayoutParams){
FrameLayout.LayoutParamslp=(FrameLayout.LayoutParams)getLayoutParams();
lp.leftMargin=oriLeft;
lp.topMargin=oriTop;
lp.width=oriRight-oriLeft;
lp.height=oriBottom-oriTop;
returnlp;
}
returnnull;
}
/**
*触摸点为中心->>移动
*
*@paramv
*@paramdx
*@paramdy
*/
privatevoidcenter(Viewv,intdx,intdy){
oriLeft+=dx;
oriTop+=dy;
oriRight+=dx;
oriBottom+=dy;
Log.i("DragViewTAG","DragSurfaceView.center:v.left="+v.getLeft()+",v.top="+v.getTop());
if(oriLeft<-offset){
Log.e("DragViewTAG","DragSurfaceView.center:左侧越界,left="+oriLeft+",offset="+offset);
oriLeft=-offset;
oriRight=oriLeft+v.getWidth();
}
if(oriRight>screenWidth+offset){
Log.e("DragViewTAG","DragSurfaceView.center:右侧越界,right="+oriRight+",screenWidth="+screenWidth+",offset="+offset);
oriRight=screenWidth+offset;
oriLeft=oriRight-v.getWidth();
}
if(oriTop<-offset){
Log.e("DragViewTAG","DragSurfaceView.center:顶部越界,top="+oriTop+",offset="+offset);
oriTop=-offset;
oriBottom=oriTop+v.getHeight();
}
if(oriBottom>screenHeight+offset){
Log.e("DragViewTAG","DragSurfaceView.center:底部越界,bottom="+oriBottom+",screenHeight="+screenHeight+",offset="+offset);
oriBottom=screenHeight+offset;
oriTop=oriBottom-v.getHeight();
}
//v.layout(left,top,right,bottom);
}
/**
*触摸点为上边缘
*
*@paramv
*@paramdy
*/
privatevoidtop(Viewv,intdy){
oriTop+=dy;
if(oriTop<-offset){
oriTop=-offset;
}
if(oriBottom-oriTop-2*offset<200){
oriTop=oriBottom-2*offset-200;
}
}
/**
*触摸点为下边缘
*
*@paramv
*@paramdy
*/
privatevoidbottom(Viewv,intdy){
oriBottom+=dy;
if(oriBottom>screenHeight+offset){
oriBottom=screenHeight+offset;
}
if(oriBottom-oriTop-2*offset<200){
oriBottom=200+oriTop+2*offset;
}
}
/**
*触摸点为右边缘
*
*@paramv
*@paramdx
*/
privatevoidright(Viewv,intdx){
oriRight+=dx;
if(oriRight>screenWidth+offset){
oriRight=screenWidth+offset;
}
if(oriRight-oriLeft-2*offset<200){
oriRight=oriLeft+2*offset+200;
}
}
/**
*触摸点为左边缘
*
*@paramv
*@paramdx
*/
privatevoidleft(Viewv,intdx){
oriLeft+=dx;
if(oriLeft<-offset){
oriLeft=-offset;
}
if(oriRight-oriLeft-2*offset<200){
oriLeft=oriRight-2*offset-200;
}
}
/**
*获取截取宽度
*
*@return
*/
publicintgetCutWidth(){
returngetWidth()-2*offset;
}
/**
*获取截取高度
*
*@return
*/
publicintgetCutHeight(){
returngetHeight()-2*offset;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。