在不同的屏幕尺寸和屏幕方向上用一条线正确连接两个 ImageView

Connecting two ImageViews with each other with a line correctly on different screen-sizes and screen-orientations

我正在寻找用一条线连接两个 ImageView 的方法。 ImageViews 在屏幕上的位置总是不同的。我想绘制这些线作为对用户点击的反应,所以我需要一个程序化的解决方案。它还需要适用于不同尺寸的屏幕(手机、平板电脑)和屏幕方向(纵向、横向)。

到目前为止,我的尝试是创建自定义视图并使用 Canvas 连接这两点:

private Paint paint = new Paint();
private Point pointA, pointB;
 @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(6);
        canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
        super.onDraw(canvas);
    }
    public void draw() {
        invalidate();
        requestLayout();
    }

我使用 viewA.getLocationOnScreen(positionA) 获取 ImageView 的位置,然后通过添加 (viewA.getWidth() / 2) 和将其调整到 select ImageView 的中心(viewA.getHeight() / 2)。不幸的是,对于不同的屏幕尺寸和屏幕方向,点连接不正确的情况一次又一次发生,即存在偏移。我尝试过各种方法来纠正偏移量(例如,包括状态栏),不幸的是它不适用于所有屏幕尺寸和方向。我也尝试过专门针对不同的屏幕尺寸和方向做出反应,但即使对于几乎相同的屏幕尺寸它也不起作用(例如,它适用于三星 Galaxy S10,但不适用于诺基亚 7.2)。 以下只是一个示例代码,我尝试了很多不同的方法,但没有得到想要的结果:

 private int topOffset() {
        View globalView = findViewById(R.id.constraintLayoutStartBildschirm);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        topOff = dm.heightPixels - globalView.getMeasuredHeight();
        return topOff;
 }
 private int leftOffset() {
        View globalView = findViewById(R.id.constraintLayoutStartBildschirm);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        leftOff = dm.widthPixels - globalView.getMeasuredWidth();
        return leftOff;
 }
private void zeichneStriche(ImageView viewA, ImageView viewB, Linien linie) {
        int[] positionA = new int[2];
        int[] positionB = new int[2];
        viewA.getLocationOnScreen(positionA);
        viewB.getLocationOnScreen(positionB);
        int xCenterA = positionA[0] + (int) (viewA.getWidth() / 1.5 ) - leftOffset();
        int xCenterB = positionB[0] + (int) (viewA.getWidth() / 1.5 ) - leftOffset();
        int yBottemA = positionA[1] + (int) (viewA.getWidth() / 1.8) - topOffset();
        int yBottemB = positionB[1] + (int) (viewB.getWidth() / 1.8) - topOffset();

        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            xCenterA = positionA[0] + (int) (viewA.getWidth() / 2 ) - leftOffset() - 20;
            xCenterB = positionB[0] + (int) (viewB.getWidth() / 2 ) - leftOffset() - 20;
            yBottemA = positionA[1]  + viewA.getHeight() / 2 - topOffset();
            yBottemB = positionB[1]  + viewB.getHeight() / 2 - topOffset();
        }

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        float yInches= metrics.heightPixels/metrics.ydpi;
        float xInches= metrics.widthPixels/metrics.xdpi;
        double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);

        if((getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) && diagonalInches>=7.5) {
            xCenterA = positionA[0] + (int) (viewA.getWidth() / 2 ) - leftOffset() + 50;
            xCenterB = positionB[0] + (int) (viewB.getWidth() / 2 ) - leftOffset() + 50;
            yBottemA = positionA[1]  + viewA.getHeight() / 2 - topOffset() +5;
            yBottemB = positionB[1]  + viewB.getHeight() / 2 - topOffset() + 5;
        }
        Point anfang = new Point(xCenterA, yBottemA);
        Point ende = new Point(xCenterB, yBottemB);
        linie.setPointA(anfang);
        linie.setPointB(ende);
        linie.draw();
    }

有没有一种解决方案可以确保它至少适用于大多数屏幕尺寸和方向? 提前致谢

使用View.getGlobalVisibleRect (Rect r) 解决问题。好一个@Nulldroid