Android 自定义LineLayout实现满屏任意拖动功能的示例代码
1.前言
在开发中,会有需求实现控件在屏幕随意拖动,这就需要自定义View,然后在OnTouchEvent事件中,处理MotionEvent.ACTION_MOVE事件,然后通过坐标点传值给onlayout方法,来实现控件的任意拖动,具体代码如下:
importandroid.content.Context;
importandroid.util.AttributeSet;
importandroid.view.Display;
importandroid.view.MotionEvent;
importandroid.view.WindowManager;
importandroid.widget.LinearLayout;
publicclassDragLineLayoutextendsLinearLayout{
privateintmWidth;
privateintmHeight;
privateintmScreenWidth;
privateintmScreenHeight;
privateContextmContext;
privateonLocationListenermLocationListener;/*listentotheRect*/
//是否拖动
privatebooleanisDrag=false;
publicbooleanisDrag(){
returnisDrag;
}
publicDragView(Contextcontext,AttributeSetattrs){
super(context,attrs);
this.mContext=context;
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
mWidth=getMeasuredWidth();
mHeight=getMeasuredHeight();
mScreenWidth=getScreenWidth(mContext);
mScreenHeight=getScreenHeight(mContext)-getStatusBarHeight();
}
publicintgetStatusBarHeight(){
intresourceId=mContext.getResources().getIdentifier("status_bar_height","dimen","android");
returnmContext.getResources().getDimensionPixelSize(resourceId);
}
publicintgetScreenWidth(Contextcontext){
WindowManagermanager=(WindowManager)context
.getSystemService(Context.WINDOW_SERVICE);
Displaydisplay=manager.getDefaultDisplay();
returndisplay.getWidth();
}
publicintgetScreenHeight(Contextcontext){
WindowManagermanager=(WindowManager)context
.getSystemService(Context.WINDOW_SERVICE);
Displaydisplay=manager.getDefaultDisplay();
returndisplay.getHeight();
}
privatefloatmDownX;
privatefloatmDownY;
@Override
publicbooleanonTouchEvent(MotionEventevent){
super.onTouchEvent(event);
if(this.isEnabled()){
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
isDrag=false;
mDownX=event.getX();
mDownY=event.getY();
break;
caseMotionEvent.ACTION_MOVE:
finalfloatmXdistance=event.getX()-mDownX;
finalfloatmYdistance=event.getY()-mDownY;
intl,r,t,b;
//当水平或者垂直滑动距离大于10,才算是拖动事件
if(Math.abs(mXdistance)>10||Math.abs(mYdistance)>10){
isDrag=true;
l=(int)(getLeft()+mXdistance);
r=l+mWidth;
t=(int)(getTop()+mYdistance);
b=t+mHeight;
//边界判断,不让布局滑出界面
if(l<0){
l=0;
r=l+mWidth;
}elseif(r>mScreenWidth){
r=mScreenWidth;
l=r-mWidth;
}
if(t<0){
t=0;
b=t+mHeight;
}elseif(b>mScreenHeight){
b=mScreenHeight;
t=b-mHeight;
}
//回调移动后的坐标点
if(mLocationListener!=null){
mLocationListener.locationRect((l+r)/2,(t+b)/2);
}
this.layout(l,t,r,b);
}
break;
caseMotionEvent.ACTION_UP:
setPressed(false);
break;
caseMotionEvent.ACTION_CANCEL:
setPressed(false);
break;
}
returntrue;
}
returnfalse;
}
publicvoidsetLocationListener(onLocationListenerLocationListener){
this.mLocationListener=LocationListener;
}
publicinterfaceonLocationListener{
voidlocationRect(floatlocationX,floatlocationY);
}
}
2.在代码中的运用
3.这样就可以在Activity加载这个xml来实现任意拖动功能
总结
到此这篇关于Android自定义LineLayout实现满屏任意拖动功能的示例代码的文章就介绍到这了,更多相关Android自定义LineLayout实现满屏任意拖动内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!