缩放时停止两个视图的重叠

时间:2019-09-05 07:13:00

标签: android scaling gesture

我有两个视图,其中一个视图位于第二个视图的正上方。我正在按比例缩放两个视图。在执行此操作时,一个视图与另一个视图重叠。我希望它们始终像最初放置时那样保持原状,例如第一个视图应始终位于第二个视图上方,并且永远不应重叠。这两个视图也可以在屏幕上拖动。我能够同时拖动它们并按​​比例缩放它们,唯一的问题是重叠。我该如何解决?

下面是我用于缩放两个视图的代码:

  public static class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
            @Override
            public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
                mScaleFactor *= scaleGestureDetector.getScaleFactor();
                mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 2.0f));
                lin_container.setScaleX(mScaleFactor);
                lin_container.setScaleY(mScaleFactor);
                lin_subtitle.setScaleX(mScaleFactor);
                lin_subtitle.setScaleY(mScaleFactor);

                return true;
            }

            @Override
            public boolean onScaleBegin(ScaleGestureDetector detector) {
                Log.e("New_coordinate", detector.getFocusX()+ " " +  detector.getFocusY());
                return super.onScaleBegin(detector);
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector detector) {
                super.onScaleEnd(detector);

                Matrix m = lin_container.getMatrix();
                RectF bbox = new RectF();
                if(m.mapRect(bbox))
                Log.e("New_coordinates", bbox.left+ " " +  bbox.top);
                lin_container.setPivotX(bbox.left);
                lin_container.setPivotY(bbox.top);
                lin_subtitle.setPivotX(bbox.left);
                lin_subtitle.setPivotY(bbox.top-100);

                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams)lin_subtitle.getLayoutParams();
                params.topMargin=(int)(mScaleFactor*lin_container.getPivotX());
                params.leftMargin=(int)(mScaleFactor*lin_container.getPivotY());



            }
        }

0 个答案:

没有答案