setTransX 带来的弊端以及解决办法2种

经过修复之后处理的

private void performMove(String s, float distanceX, float distanceY) {
        float translationX = this.getTranslationX() + distanceX;
        float translationY = this.getTranslationY() + distanceY;
        //translationX是相对自身之前移动了多少,如果向上,为负值向下为正, 同样的 向左为正,那么view的位置可能不确定,qssq说,这就要用getX getY来判断了
        if (translationX < 0) {
            translationX = 0;
        }

        //        Prt.w(TAG, " ACTION_MOVE,will settransX:" + translationX + ",will transY:" + translationY + ",beforeTranslationX:" + this.getTranslationX() + ",transY:" + this.getTranslationY() + ",distnceX:" + distanceX + ",distanceY:" + distanceY);
        this.setTranslationX(translationX);//getx 是父view的左边 gety父亲的顶部距离 getTranx 是相对于原来位置的偏移
        this.setTranslationY(translationY);
        if (BuildConfig.DEBUG) {//,currentTranslationY:-1739.7078的时候看不见了
            Prt.w(TAG, "currentTraxnX:" + translationX + ",currentTranslationY:" + translationY + ",x:" + this.getX() + ",y:" + this.getY());
        }
        ViewGroup parent = (ViewGroup) getParent();

        if (this.getX() < 0) {
            this.setX(0);
        } else if (this.getX() + this.getWidth() > parent.getWidth()) {


            float fixX = parent.getWidth() - this.getWidth();//这样写不行,因为get

            Log.w(TAG, "fixX:" + fixX + ",this.Width:" + this.getWidth() + ",parentWidth:" + parent.getWidth() + ",thjs.x" + this.getX());
            this.setX(fixX);
        }

        if (this.getY() < 0) {
            this.setY(0);
        } else if (this.getY() + this.getHeight() > parent.getHeight()) {
            this.setY(parent.getHeight() - this.getHeight());
        }

        if (onMoveListener != null) {
            onMoveListener.onTransMove(this.getTranslationX(), this.getTranslationY());
        }


    }

之后还是觉得setX 更好使用,更好的确定边界越界问题。

private void performMove(String s, float distanceX, float distanceY) {
        float willX = this.getX() + distanceX;
        float willY = this.getY() + distanceY;
        //translationX是相对自身之前移动了多少,如果向上,为负值向下为正, 同样的 向左为正,那么view的位置可能不确定,qssq说,这就要用getX getY来判断了
        if (willX < 0) {
            willX = 0;
        }
        ViewGroup parent = (ViewGroup) getParent();
        if (willX < 0) {
            this.setX(0);
        } else if (willX + this.getWidth() > parent.getWidth()) {

            float fixX = parent.getWidth() - this.getWidth();//这样写不行,因为get
            Log.w(TAG, "fixX:" + fixX + ",this.Width:" + this.getWidth() + ",parentWidth:" + parent.getWidth() + ",thjs.x" + this.getX());
            willX = fixX;
        }
        if (willY < 0) {
            willY = 0;
        } else if (willY + this.getHeight() > parent.getHeight()) {
            willY = parent.getHeight() - this.getHeight();
        }
        setX(willX);
        setY(willY);

        if (onMoveListener != null) {
            onMoveListener.onTransMove(willX,willY);
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值