执行 invalidate() 时 TextView 未绘制

发布于 2024-11-24 08:24:35 字数 2706 浏览 1 评论 0原文

我正在尝试显示屏幕上的坐标取决于传感器数据的视图。我设法显示了 中解释的文本所以问题,我现在尝试显示一些 TextView,但是当在我的 TextView 上调用 invalidate() 时,它们没有被绘制......

有人可以帮忙吗?

谢谢

ARView.java

public class ARView extends TextView
{ 
    public String name;
    public float azimuth;
    public float distance;
    public float inclination;
    public Location location;

    public ARView(Context context)
    {        
        super(context);
        setText(name);
    }
}

ARActivity.java

这里我将 ARView 添加到其父视图中。

public class ARActivity extends Activity{

    private ARLayout ar;
    private CBCameraView cv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            ar = new ARLayout(getApplicationContext());

            cv = new CBCameraView(this.getApplicationContext());

            FrameLayout rl = new FrameLayout(this.getApplicationContext());

            rl.addView(cv, GlobalVars.screenWidth, GlobalVars.screenHeight); 
            rl.addView(ar, GlobalVars.screenWidth, GlobalVars.screenHeight); 

            setContentView(rl);

            Location location = new Location("me");
            location.setLatitude(41.371974);
            location.setLongitude(2.166978);

            ARView fs = new ARView(this.getApplicationContext());
            fs.azimuth = 0;
            fs.inclination = 0;
            fs.location = location;
            fs.name="Bar seco";
            ar.addARView(fs);                    
    }
}

ARLayout.java

在这里我获取传感器数据,更新我的 ARViews 坐标并希望绘制它们......

public class ARLayout extends View implements LocationListener, SensorEventListener
{

    // ...

    @Override
    public void onSensorChanged(SensorEvent event) {

        // here I get sensors data and update my views

        for(int i=0; i<nArViews; i++)
        {

            ARView view = arViews.get(i);            

            int x = ...; // new x coordinate of my ARView
            int y = ...; // new y coordinate of my ARView

            view.layout(x, y, view.getBottom(), view.getRight());  // set new coordinates to my ARViews

        }

        invalidate(); // draw my ARLayout       
    }

    public void onDraw(Canvas c)
    {
        for(int i=0; i<nArViews; i++)
        {                
            ARView view = arViews.get(i);
            view.invalidate();  // I thought it would draw my ARViews, but it doesn't :(
        }
    }

    // more stuff   

}

I'm trying to display views which coordinates on screen depends on sensors data. I managed to show a text as explained in that SO question and I'm now trying to show some TextViews instead, but when calling invalidate() on my TextViews, they're not drawn...

Anybody can help?

Thanks

ARView.java

public class ARView extends TextView
{ 
    public String name;
    public float azimuth;
    public float distance;
    public float inclination;
    public Location location;

    public ARView(Context context)
    {        
        super(context);
        setText(name);
    }
}

ARActivity.java

Here I add my ARView to its parent view.

public class ARActivity extends Activity{

    private ARLayout ar;
    private CBCameraView cv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            ar = new ARLayout(getApplicationContext());

            cv = new CBCameraView(this.getApplicationContext());

            FrameLayout rl = new FrameLayout(this.getApplicationContext());

            rl.addView(cv, GlobalVars.screenWidth, GlobalVars.screenHeight); 
            rl.addView(ar, GlobalVars.screenWidth, GlobalVars.screenHeight); 

            setContentView(rl);

            Location location = new Location("me");
            location.setLatitude(41.371974);
            location.setLongitude(2.166978);

            ARView fs = new ARView(this.getApplicationContext());
            fs.azimuth = 0;
            fs.inclination = 0;
            fs.location = location;
            fs.name="Bar seco";
            ar.addARView(fs);                    
    }
}

ARLayout.java

Here I get sensors data, update my ARViews coordinates and hopefully draw them...

public class ARLayout extends View implements LocationListener, SensorEventListener
{

    // ...

    @Override
    public void onSensorChanged(SensorEvent event) {

        // here I get sensors data and update my views

        for(int i=0; i<nArViews; i++)
        {

            ARView view = arViews.get(i);            

            int x = ...; // new x coordinate of my ARView
            int y = ...; // new y coordinate of my ARView

            view.layout(x, y, view.getBottom(), view.getRight());  // set new coordinates to my ARViews

        }

        invalidate(); // draw my ARLayout       
    }

    public void onDraw(Canvas c)
    {
        for(int i=0; i<nArViews; i++)
        {                
            ARView view = arViews.get(i);
            view.invalidate();  // I thought it would draw my ARViews, but it doesn't :(
        }
    }

    // more stuff   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-12-01 08:24:36

我不知道这是否是特定问题的解决方案,但我确实知道这是另一个问题。

删除 onDraw() 方法中的 invalidate() 调用。每次抽奖只需调用 invalidate 一次。从 onDraw() 中删除它将确保屏幕仅在有新的传感器数据时更新。在 onDraw() 离开之前,屏幕不会真正绘制。

或者,您可以将调用移至 onDraw() 中的 for 循环之后的 invalidate(),并将其从 onSensorChanged() 中完全删除。无论您是否有传感器数据,这都会使屏幕不断重绘。

I don't know if this is the solution to the specific problem, but I do know this is another problem.

Remove the invalidate() call in the onDraw() method. You only need to call invalidate once per draw. Removing it from onDraw() will ensure the screen only updates when you have new sensor data. The screen won't actually draw until onDraw() leaves as well.

Alternatively, you can move the call to invalidate() AFTER the for-loop in onDraw() and remove it entirely from onSensorChanged(). This will make the screen constantly redraw whether you have sensor data or not.

乖乖 2024-12-01 08:24:36

看来您只是改变了视图的位置。您不必调用 invalidate 也不必否决 onDraw。对于这类事情,操作系统知道何时重绘。 Invalidate 和 onDraw 仅适用于自定义绘图。

It seems that you are only changing the view's positions. You don't have to call invalidate and you don't have to overrule onDraw. For those kind of things the OS knows when to redraw. Invalidate and onDraw is for custom drawing only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文