【问题标题】:Connecting two ImageViews with each other with a line correctly on different screen-sizes and screen-orientations在不同的屏幕尺寸和屏幕方向上正确连接两个 ImageView
【发布时间】:2021-09-18 09:50:09
【问题描述】:

我正在寻找一种用线连接两个 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) 和 (viewA.getHeight() / 2) 对其进行调整以选择 ImageView 的中心。不幸的是,对于不同的屏幕尺寸和屏幕方向,点连接不正确会一次又一次地发生,即存在偏移。我尝试了各种方法来纠正偏移量(例如,包括状态栏),不幸的是它永远不适用于所有屏幕尺寸和方向。我还尝试专门针对不同的屏幕尺寸和方向做出反应,但即使对于几乎相同的屏幕尺寸也不起作用(例如,它适用于三星 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();
    }

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

【问题讨论】:

    标签: java android android-studio screen-orientation screen-size


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      相关资源
      最近更新 更多