执行 invalidate() 时 TextView 未绘制
我正在尝试显示屏幕上的坐标取决于传感器数据的视图。我设法显示了 中解释的文本所以问题,我现在尝试显示一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否是特定问题的解决方案,但我确实知道这是另一个问题。
删除
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 theonDraw()
method. You only need to call invalidate once per draw. Removing it fromonDraw()
will ensure the screen only updates when you have new sensor data. The screen won't actually draw untilonDraw()
leaves as well.Alternatively, you can move the call to
invalidate()
AFTER the for-loop inonDraw()
and remove it entirely fromonSensorChanged()
. This will make the screen constantly redraw whether you have sensor data or not.看来您只是改变了视图的位置。您不必调用 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.