1.自定义View
public class CustomFrameLayout extends FrameLayout {
private Context mContext;
private ViewDragHelper mDragger;
private View mAutoBackView;
public CustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mDragger = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
@Override
public int getViewHorizontalDragRange(View child) {
//返回可拖动的子视图的水平运动范围(以像素为单位)的大小。
//对于不能垂直移动的视图,此方法应该返回0。
return getMeasuredWidth() - child.getMeasuredWidth()- DeviceUtils.dp2px(mContext,10);
}
@Override
public int getViewVerticalDragRange(View child) {
//返回可拖动的子视图的竖直运动范围(以像素为单位)的大小。
//对于不能垂直移动的视图,此方法应该返回0。
return getMeasuredHeight() - child.getMeasuredHeight()- DeviceUtils.dp2px(mContext,35);
}
@Override
public boolean tryCaptureView(View child, int pointerId) {
//返回true表view示捕获当前touch到的
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (left > getWidth() - child.getMeasuredWidth()- DeviceUtils.dp2px(mContext,5)){
//超出左侧边界处理
left = getWidth() - child.getMeasuredWidth()- DeviceUtils.dp2px(mContext,5);
} else if (left < DeviceUtils.dp2px(mContext,5)) {
//超出右侧边界处理
left = DeviceUtils.dp2px(mContext,5);
}
return left;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
if (top > getHeight() - child.getMeasuredHeight()- DeviceUtils.dp2px(mContext,5)){
//超出下边界处理
top = getHeight() - child.getMeasuredHeight()- DeviceUtils.dp2px(mContext,5);
} else if (top < DeviceUtils.dp2px(mContext,30)) {
//超出上边界处理
top = DeviceUtils.dp2px(mContext,30);
}
return top;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
super.onViewPositionChanged(changedView, left, top, dx, dy);
// SharedPreferencesUtil.saveData(mContext,(String) changedView.getTag(), left + "#" + top);
}
});
mDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
// final int count = getChildCount();
// View child = getChildAt(count-1);
// View child;
// if(getResources().getConfiguration().orientation== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
// child=findViewById(R.id.rl_music_top_startlive_processing_port);
// }else {
// child=findViewById(R.id.rl_music_top_startlive_processing_land);
// }
// String xy = (String) SharedPreferencesUtil.getData(mContext, (String) child.getTag(), "0");
// if (!xy.equals("0")) {
// String[] xys = xy.split("#");
// if (xys.length == 2) {
// child.layout(Integer.parseInt(xys[0]), Integer.parseInt(xys[1]), child.getMeasuredWidth() + Integer.parseInt(xys[0]), child.getMeasuredHeight() + Integer.parseInt(xys[1]));
// }
// }
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mDragger.shouldInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//点击位置坐标
int downX = (int) event.getX();
int downY = (int) event.getY();
//悬浮区域左上角坐标
int x = (int) mAutoBackView.getX();
int y = (int) mAutoBackView.getY();
//悬浮区域宽高
int width = mAutoBackView.getWidth();
int height = mAutoBackView.getHeight();
if ((downX >= x && downY >= y) && (downX <= (x+width) && downY <= (y+height)) ) {
//点击在悬浮区域内部进行事件拦截,否则不拦截
try {
mDragger.processTouchEvent(event);
return true;
}catch (IllegalArgumentException e){
return false;
}
} else {
return false;
}
}
@Override
public void computeScroll() {
if (mDragger.continueSettling(true)) {
invalidate();
}
}
/**
* onFinishInflate 当View中所有的子控件均被映射成xml后触发
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//根据需要控制哪个子view可被拖拽
if(getResources().getConfiguration().orientation== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
mAutoBackView=getChildAt(getChildCount()-2);
}else {
mAutoBackView=getChildAt(getChildCount()-1);
}
// mAutoBackView = getChildAt(getChildCount()-1);
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(getResources().getConfiguration().orientation== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
mAutoBackView=getChildAt(getChildCount()-2);
}else {
mAutoBackView=getChildAt(getChildCount()-1);
}
invalidate();
}
}
2.在xml布局文件中使用该自定义控件即可。