android framelayout 拖动时,界面压缩变形问题

      界面有一个布局,需要实现可以拖动的功能,布局里面有四个部分,分别还要可以点击,所以一开始参考了这个链接:http://blog.csdn.net/wangjia55/article/details/7458620,的确是可以实现,但我突然发现当这个拖动的布局拖动到界面的中间十字线的位置,都会出现压缩的原因,也百度谷歌了很久,发现没有人遇到类似的问题,使用view.layout界面又是刷新,布局会重新回到原点,这个方法也不可取,最后只能折中一下,使用view.setLayoutParams()的形式,对中间线的位置进行判断,当拖动到十字线的位置,立即改变位置,如下代码:

	/**
	 * 
	 * @Title: fourChartTouchLister
	 * @Description://4个左上角悬浮组件监听
	 * @param 
	 * @return void
	 * @throws  
	 */
	private void fourChartTouchLister() {

		firstSupspendLine.setOnTouchListener(this);
		//同时添加点击事件,才能在return true时,响应拖动事件
		firstSupspendLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});

		firstSupspendLine__buyprice.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return touchEventMethod(event, firstSupspendLine);
			}
		});

		secondSupspendLine.setOnTouchListener(this);
		//同时添加点击事件,才能在return true时,响应拖动事件
		secondSupspendLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});

		secondSupspendLine__buyprice.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return touchEventMethod(event, secondSupspendLine);
			}
		});

		thirdSupspendLine.setOnTouchListener(this);
		//同时添加点击事件,才能在return true时,响应拖动事件
		thirdSupspendLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});

		thirdSupspendLine__buyprice.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return touchEventMethod(event, thirdSupspendLine);
			}
		});
		fourSupspendLine.setOnTouchListener(this);
		//同时添加点击事件,才能在return true时,响应拖动事件
		fourSupspendLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});
		fourSupspendLine__buyprice.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return touchEventMethod(event, fourSupspendLine);
			}
		});
	}

	/**
	 * 监听左上方悬浮的控件
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {

		return touchEventMethod(event, v);

	}

	/**
	 * 父布局和子控件的touch事件需要特别处理
	 * 
	 * @param event
	 * @param v
	 * @return
	 * @author qiulinhe
	 * @createTime 2016年7月6日 上午9:54:32
	 */
	private boolean touchEventMethod(MotionEvent event, View v) {
		System.out.println("移动之后的高宽度" + webviewHeight + "===" + webviewWidth);

		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			firstX = (int) event.getX();//按下的时候开始的x的位置
			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();

			isclick = false;//当按下的时候设置isclick为false,这时候可以响应点击事件
			startTime = System.currentTimeMillis();

			break;
		case MotionEvent.ACTION_UP:

			endTime = System.currentTimeMillis();
			//当从点击到弹起小于半秒的时候,则判断为点击,如果超过则不响应点击事件
			if ((endTime - startTime) > 0.2 * 1000L) {
				isclick = true;
			} else {
				isclick = false;
			}

			break;
		case MotionEvent.ACTION_POINTER_DOWN:

			break;
		case MotionEvent.ACTION_POINTER_UP:
			break;

		case MotionEvent.ACTION_MOVE:
			isclick = true;//当按钮被移动的时候设置isclick为true

			int dx = (int) event.getRawX() - lastX;
			int dy = (int) event.getRawY() - lastY;

			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 > webviewWidth) {
				right = webviewWidth;
				left = right - v.getWidth();
			}

			//当竖向的中线,需要将位置移动,跳过中线的位置,防止布局压缩,
			//从右往左
			if ((left == webviewWidth / 2 + 20)
					|| (left > webviewWidth / 2 && (left < webviewWidth / 2 + 20) && right > webviewWidth / 2)) {
				right = (webviewWidth / 2) - 40;
				left = (webviewWidth / 2) - 40 - v.getWidth();

			} else if ((right == webviewWidth / 2 + 20)//从左往右拖动
					|| (right < webviewWidth / 2 && (right > webviewWidth / 2 - 20) && left < webviewWidth / 2)) {
				left = (webviewWidth / 2) + 20;
				right = (webviewWidth / 2) + 20 + v.getWidth();
			}

			//当竖向的中线,需要将位置移动
			if (left == webviewWidth / 2 && right > (webviewWidth / 2)) {
				right = (webviewWidth / 2);
				left = (webviewWidth / 2) - v.getWidth();

			} else if (right == webviewWidth / 2 && left < (webviewWidth / 2)) {
				left = (webviewWidth / 2);
				right = (webviewWidth / 2) + v.getWidth();
			}

			if (top < 0) {
				top = 0;
				bottom = top + firstSupspendLine.getHeight();
			}

			//当横向的中线,需要将位置移动,跳过中线的位置,防止布局压缩,
			//从下往上拖动
			if ((top == webviewHeight / 2 + 20)
					|| (top > webviewHeight / 2 && (top < webviewHeight / 2 + 20) && bottom > webviewHeight / 2)) {
				bottom = (webviewHeight / 2) - 40;
				top = (webviewHeight / 2) - 40 - v.getHeight();

			} else if ((bottom == webviewHeight / 2 + 20)//从上往下拖动
					|| (bottom < webviewHeight / 2 && (bottom > webviewHeight / 2 - 20) && top < webviewHeight / 2)) {
				top = (webviewHeight / 2) + 20;
				bottom = (webviewHeight / 2) + 20 + v.getHeight();
			}

			if (bottom > webviewHeight) {
				bottom = webviewHeight;
				top = bottom - v.getHeight();

			}
			RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();

			layoutParams.leftMargin = left;//StaticContext.IFFULLSCREEN ? left : 0;//全屏时是否支持横向拖动
			layoutParams.topMargin = top;
			layoutParams.rightMargin = right;// StaticContext.IFFULLSCREEN ? right : 0;//全屏时是否支持横向拖动
			layoutParams.bottomMargin = bottom;
			v.setLayoutParams(layoutParams);

			//			DocCaptain.getInstance().setSuspendLeft(left);
			//			DocCaptain.getInstance().setSuspendTop(top);
			//			DocCaptain.getInstance().setSuspendRight(right);
			//			DocCaptain.getInstance().setSuspendBottom(bottom);
			//			v.layout(left, top, right, bottom);

			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();
			break;
		}
		// v.invalidate();
		return isclick;
	}




     说的很笼统,如果有人遇到同样的问题,你就可以和我交流一下.上面的问题,还遇到父布局需要拖动,有父布局里面的子控件textview也需要监听点击事件和拖动事件的解决.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值