【问题标题】:Drawing LinearLayout view on canvas disappearing在画布上绘制 LinearLayout 视图消失
【发布时间】:2026-02-21 11:25:02
【问题描述】:

我正在使用画布在 Surface View Camera 上绘制线性布局视图(插入点)。当视图太多(例如:在我的情况下为 40)时,我遇到了一个问题,一些视图在移动相机时消失了。我在这里找不到问题。 setX 和 setY 方法有什么问题吗?您可以在下面看到我的代码,请回复。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (currentLocation == null) {
        return;
    }

    for (int i = 0; i < places.size(); i++) {
         float[] currentLocationInECEF = LocationHelper.WSG84toECEF(currentLocation);
         float[] pointInECEF = LocationHelper.WSG84toECEF(poiLoc.get(i));
         float[] pointInENU = LocationHelper.ECEFtoENU(currentLocation, currentLocationInECEF, pointInECEF);

        Matrix.multiplyMV(cameraCoordinateVector, 0, rotatedProjectionMatrix, 0, pointInENU, 0);


        if (cameraCoordinateVector[2] < 0 && insertPoint!=null && insertPoint.getChildAt(i)!=null) {
            float x = (0.5f + cameraCoordinateVector[0] / cameraCoordinateVector[3]) * canvas.getWidth();
            float y = (0.5f - cameraCoordinateVector[1] / cameraCoordinateVector[3]) * canvas.getHeight();

            insertPoint.getChildAt(i).setX(x - (insertPoint.getChildAt(i).getWidth()/2));
            insertPoint.getChildAt(i).setY(y);
            insertPoint.getChildAt(i).setVisibility(VISIBLE);
        }
    }
}

预期结果:所有视图都应显示在相机视图上。 实际结果:移动 Camera 和 onDrawCanvas 回调时,一些视图正在消失。

我正在使用这个库https://github.com/dat-ng/ar-location-based-android

【问题讨论】:

  • 你在做什么?
  • 添加一些截图
  • @pembaTamang 我已经添加了截图。我正在尝试将线性布局添加为相机上的视图,但在移动相机时视图正在变成一半甚至完全消失。
  • 看起来不错。也许你的内存不足。顺便说一句,我的同事使用它并且它可以正常工作github.com/appoly/ARCore-Location
  • 谢谢,但我猜这不是内存问题。我想在不使用任何 SDK 的情况下实现这一点。有没有其他方法可以修复相机上的视图?

标签: java android camera android-canvas surfaceview


【解决方案1】:

我终于设法解决了这个问题。我所要做的就是将 LinearLayout 替换为 RelativeLayout,它就像一个魅力。

【讨论】: