这是主要代码: 保证其params发生改变,相对于父布局的位置就能达到位置移动到原来的位置
// 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷新时)则移动的view会回到原来的位置
RelativeLayout.LayoutParams lpFeedback = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpFeedback.leftMargin = v.getLeft();
lpFeedback.topMargin = v.getTop();
lpFeedback.setMargins(v.getLeft(), v.getTop(), 0, 0);
v.setLayoutParams(lpFeedback);
思路是:当view的位置发生改变时,也要相应改变其layoutparams,否则父布局重绘时,由于view的layoutparams没发生改变导致重绘的时候会回到原点,所以只要改变其params就好了
- /**
- *
- * @description 设置意见反馈,用以灰度发布
- * @author zhongwr
- * @params
- * @update 2016年1月12日 下午5:36:07
- */
- private ImageView getFeedBackView() {
- ImageView ivFeedback = new ImageView(BaseActivity.this);
- ivFeedback.setImageResource(R.drawable.fuli_feedback);
- RelativeLayout.LayoutParams lpFeedback = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- lpFeedback.setMargins(0, 0, 20, 218);
- lpFeedback.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- lpFeedback.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- ivFeedback.setLayoutParams(lpFeedback);
- ivFeedback.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {// 意见反馈
- FeedBackActivity.startInstance(BaseActivity.this);
- }
- });
- return ivFeedback;
- }
- private boolean isIntercept = false;
- /** 按下时的位置控件相对屏幕左上角的位置X */
- private int startDownX;
- /** 按下时的位置控件距离屏幕左上角的位置Y */
- private int startDownY;
- /** 控件相对屏幕左上角移动的位置X */
- private int lastMoveX;
- /** 控件相对屏幕左上角移动的位置Y */
- private int lastMoveY;
- /**
- *
- * @description 拖动意见反馈位置
- * @author zhongwr
- * @params
- * @update 2016年1月14日 下午5:08:26
- */
- private void setFeedBackViewDragTouch(ImageView ivFeedBack) {
- ivFeedBack.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- int action = event.getAction();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- startDownX = lastMoveX = (int) event.getRawX();
- startDownY = lastMoveY = (int) event.getRawY();
- break;
- case MotionEvent.ACTION_MOVE:
- int dx = (int) event.getRawX() - lastMoveX;
- int dy = (int) event.getRawY() - lastMoveY;
- int left = v.getLeft() + dx;
- int top = v.getTop() + dy;
- int right = v.getRight() + dx;
- int bottom = v.getBottom() + dy;
- if (left < 0) {
- left = 0;
- right = left + v.getWidth();
- }
- if (right > mScreenWidth) {
- right = mScreenWidth;
- left = right - v.getWidth();
- }
- if (top < 0) {
- top = 0;
- bottom = top + v.getHeight();
- }
- if (bottom > mScreenHeight) {
- bottom = mScreenHeight;
- top = bottom - v.getHeight();
- }
- v.layout(left, top, right, bottom);
- lastMoveX = (int) event.getRawX();
- lastMoveY = (int) event.getRawY();
- break;
- case MotionEvent.ACTION_UP:
- int lastMoveDx = Math.abs((int) event.getRawX() - startDownX);
- int lastMoveDy = Math.abs((int) event.getRawY() - startDownY);
- if (0 != lastMoveDx || 0 != lastMoveDy) {
- isIntercept = true;
- } else {
- isIntercept = false;
- }
- // 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷新时)则移动的view会回到原来的位置
- RelativeLayout.LayoutParams lpFeedback = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- lpFeedback.leftMargin = v.getLeft();
- lpFeedback.topMargin = v.getTop();
- lpFeedback.setMargins(v.getLeft(), v.getTop(), 0, 0);
- v.setLayoutParams(lpFeedback);
- break;
- }
- return isIntercept;
- }
- });
- }
demo:demo