我正在尝试显示屏幕上的坐标取决于传感器数据的视图。我设法显示了该 SO 问题中解释的文本,我现在尝试显示一些 TextViews,但是在我的 TextViews 上调用 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
}